20
ARDUINO CONTROL Oleh Al Aziz Berlian Siregar (11410387) Dwi Ermawati

Arduino

Embed Size (px)

Citation preview

Page 1: Arduino

ARDUINO

CONTROLOleh

Al Aziz Berlian Siregar (11410387)

Dwi Ermawati

Page 2: Arduino

Terdiri Dari

ArraysFor Loop IterationIf Statement ConditionalSwitch CaseSwitch Case2WhileStatementConditional

Page 3: Arduino

ARRAY

• Array adalah kumpulan nilai yang dapat di akses dengan index number.

• Nilai yang terdapat dalam array dapat dipanggil dengan cara menuliskan nama array dan index number.

• Array dengan index 0 merupakan nilai pertama dari array.

• Array perlu dideklarasikan dan diberi nilai sebelum digunakan.

Page 4: Arduino

PENGGUNAAN ARRAY PADA ARDUINO

Memberi nomor pada tiap pin dan mengulang nomor pin secara berurutan. Menghidupkan LED-LED secara berurutan kemudian secara terbalik.

Hardware :• Arduino Board• (6) 220 ohm resistors• (6) LEDs• hook-up wire• breadboard

Page 5: Arduino

int timer = 100; // The higher the number, the slower the timing.int ledPins[] = { 2, 7, 4, 6, 5, 3 }; // an array of pin numbers to which LEDs are attachedint pinCount = 6; // the number of pins (i.e. the length of the array)

void setup() { int thisPin; // the array elements are numbered from 0 to (pinCount - 1). // use a for loop to initialize each pin as an output: for (int thisPin = 0; thisPin < pinCount; thisPin++) { pinMode(ledPins[thisPin], OUTPUT); }}

void loop() { // loop from the lowest pin to the highest: for (int thisPin = 0; thisPin < pinCount; thisPin++) { // turn the pin on: digitalWrite(ledPins[thisPin], HIGH); delay(timer); // turn the pin off: digitalWrite(ledPins[thisPin], LOW);

}

// loop from the highest pin to the lowest: for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { // turn the pin on: digitalWrite(ledPins[thisPin], HIGH); delay(timer); // turn the pin off: digitalWrite(ledPins[thisPin], LOW); }}

Page 6: Arduino

FOR LOOP ITERATION

• Operator for digunakan dalam blok pengulangan tertutup

• StatementFor ( initialization; condition;

expression ) { //doSomethig; }

Page 7: Arduino

PENGGUNAAN FORLOOPITERATION PADA

ARDUINO

Untuk menghidupkan serangkaian LED yang melekat pada pin 2 dan pin 7 pada arduino dan mengaktifkan urutan pin yang jumlahnya tidak berdekatan dan selalu berurutan

Hardware• Sama seperti hardware

pada Array

Page 8: Arduino

int timer = 100; // The higher the number, the slower the timing.

void setup() { // use a for loop to initialize each pin as an output: for (int thisPin = 2; thisPin < 8; thisPin++) { pinMode(thisPin, OUTPUT); }}

void loop() { // loop from the lowest pin to the highest: for (int thisPin = 2; thisPin < 8; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); }

// loop from the highest pin to the lowest: for (int thisPin = 7; thisPin >= 2; thisPin--) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); }}

Page 9: Arduino

IF STATEMENT CONDITIONAL

If Operator adalah sebuah kondisi seperti nilai analog sudah berada di bawah nilai yang kita kehendaki atau belum bentuk:

If ( someVariable ?? value ) { //DoSomething; }

Operator if…else adalah sebuah kondisi apabila tidak sesuai dengan kondisi yang pertama maka akan mengeksekusi baris program yang ada di else. bentuk:

If ( inputPin == HIGH ) { //Laksanakan rencana A; } Else { //Laksanakan rencana B; }

Page 10: Arduino

PENGGUNAAN IF STATEMENT CONDITIONAL PADA

ARDUINO

Membaca keadaan potensiometer (input analog) dan menyalakan LEDhanya jika LED berjalan di atas ambang batas tertentu. Mencetak nilai analogtanpa memperhatikan levelnya.

HardwareArduino Board(1) Potentiometer or variable resistor(1) 220 ohm resistor(1) LEDhook-up wire

Page 11: Arduino

void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize serial communications: Serial.begin(9600);}

void loop() { // read the value of the potentiometer: int analogValue = analogRead(analogPin);

// if the analog value is high enough, turn on the LED: if (analogValue > threshold) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin,LOW); }

// print the analog value: Serial.println(analogValue);

}

Page 12: Arduino

SWITCH CASE

Switch case merupakan pernyataan yang dirancang khusus untuk menangani pengambilan keputusan yang melibatkan sejumlah atau banyak alternatif, tetapi penggunaannya hanya untuk memeriksa data yang bertipe karakter atau integer.

Page 13: Arduino

PENGGUNAAN SWITCH CASE PADA ARDUINO

Switch memungkinkan kita untuk memilih diantara beberapa pilihan diskrit. Tutorial ini menunjukkan cara berpindah diantara empat keadaan yang diinginkan dari sebuah foto resistor: benar-benar gelap, redup, sedang, dan terang.HardwareArduino Board(1) photocell, or analog sensor(1) 10k ohm resistorsbreadboardhook-up wire

Page 14: Arduino

void setup() { // initialize serial communication: Serial.begin(9600); }

void loop() { // read the sensor: int sensorReading = analogRead(A0); // map the sensor range to a range of four options: int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

// do something different depending on the // range value:

switch (range) { case 0: // your hand is on the sensor Serial.println("dark"); break; case 1: // your hand is close to the sensor Serial.println("dim"); break; case 2: // your hand is a few inches from the sensor Serial.println("medium"); break; case 3: // your hand is nowhere near the sensor Serial.println("bright"); break; }

}

Page 15: Arduino

SWITCH CASE 2 (WITH SERIAL)

Switch digunakan untuk memilih diantara seperangkat nilai-nilai diskrit dari variabel. Sehingga akan menjalankan program terpilih.

Bentuk Umumswitch (inByte) {

case 'a': digitalWrite(2, HIGH); break

case ‘b': digitalWrite(4, HIGH); break; default:;

Page 16: Arduino

PENGGUNAAN SWITCH CASE 2 PADA ARDUINO

Menggunakan saklar untuk mengaktifkan salah satu LED yang berbeda berdasarkan bit data yang diterima secara urut. Sketsa mengikuti masukan yang urut, dan menyala pada LED yang berbeda untuk karakter a, b, c, d, atau e.HardwareArduino Board(5) LEDs(5) 220 ohm resistorsbreadboardhook-up wire

Page 17: Arduino

void setup() { // initialize serial communication: Serial.begin(9600); // initialize the LED pins: for (int thisPin = 2; thisPin < 7; thisPin++) { pinMode(thisPin, OUTPUT); } }

void loop() { // read the sensor: if (Serial.available() > 0) { int inByte = Serial.read(); // do something different depending on the character received. // The switch statement expects single number values for each case; // in this exmaple, though, you're using single quotes to tell // the controller to get the ASCII value for the character. For // example 'a' = 97, 'b' = 98, and so forth:

switch (inByte) { case 'a': digitalWrite(2, HIGH); break; case 'b': digitalWrite(3, HIGH); break; case 'c': digitalWrite(4, HIGH); break; case 'd': digitalWrite(5, HIGH); break; case 'e': digitalWrite(6, HIGH); break; default: // turn all the LEDs off: for (int thisPin = 2; thisPin < 7; thisPin++) { digitalWrite(thisPin, LOW); } }

Page 18: Arduino

CONDITIONALS - WHILE STATEMENT

Pernyataan While adalah menjalankan program-program saat tombol atau perintah sampai perintah selesai diberikan.

  // while the button is pressed, take calibration readings:  while (digitalRead(buttonPin) == HIGH) {    calibrate();

Page 19: Arduino

PENGGUNAAN WHILE STATEMENT PADA ARDUINO

Untuk membuat semua yang ada diprogram berhenti sementara kondisi yang diberikan adalah benar. Ini dapat dilakukan dengan menggunakan while loop. Contoh ini menunjukkan bagaimana menggunakan while loop  untuk mengkalibrasi nilai sensor analog.HardwareArduino Board(1) digital pushbutton or switch(1) photocell, or analog sensor(2) 10k ohm resistorsbreadboard

Page 20: Arduino

void setup() { // set the LED pins as outputs and the switch pin as input: pinMode(indicatorLedPin, OUTPUT); pinMode (ledPin, OUTPUT); pinMode (buttonPin, INPUT);}

void loop() { // while the button is pressed, take calibration readings: while (digitalRead(buttonPin) == HIGH) { calibrate(); } // signal the end of the calibration period digitalWrite(indicatorLedPin, LOW);

// read the sensor: sensorValue = analogRead(sensorPin);

// apply the calibration to the sensor reading sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

// in case the sensor value is outside the range seen during calibration sensorValue = constrain(sensorValue, 0, 255);

// fade the LED using the calibrated value: analogWrite(ledPin, sensorValue);}

void calibrate() { // turn on the indicator LED to indicate that calibration is happening: digitalWrite(indicatorLedPin, HIGH); // read the sensor: sensorValue = analogRead(sensorPin);

// record the maximum sensor value if (sensorValue > sensorMax) { sensorMax = sensorValue; }

// record the minimum sensor value if (sensorValue < sensorMin) { sensorMin = sensorValue; }}