Professional Documents
Culture Documents
CONTROL
Oleh
Al Aziz Berlian Siregar (11410387) Dwi Ermawati
Terdiri Dari
Arrays For Loop Iteration If Statement Conditional Switch Case Switch Case2 WhileStatementConditional
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.
ARRAY
Memberi nomor pada tiap pin dan mengulang nomor pin secara berurutan. Menghidupkan LEDLED secara berurutan kemudian secara terbalik.
Hardware : Arduino Board (6) 220 ohm resistors (6) LEDs hook-up wire breadboard
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 attached int pinCount = 6; // the number of pins (i.e. the length of the array)
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);
void setup() { } int thisPin; // the array elements are numbered // loop from the highest pin to the lowest: from 0 to (pinCount - 1). // use a for loop to initialize each pin for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { as an output: // turn the pin on: for (int thisPin = 0; thisPin < digitalWrite(ledPins[thisPin], HIGH); pinCount; thisPin++) { delay(timer); pinMode(ledPins[thisPin], // turn the pin off: OUTPUT); digitalWrite(ledPins[thisPin], LOW); } } } }
Operator for digunakan pengulangan tertutup Statement For ( initialization; expression ) { //doSomethig; }
dalam
blok
condition;
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
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); } }
If Operator adalah sebuah kondisi seperti nilai analog sudah berada di bawah nilai yang kita kehendaki atau belum bentuk: If ( someVariable ?? value ) { //DoSomething; } Operator ifelse 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; }
IF STATEMENT CONDITIONAL
Membaca keadaan potensiometer (input analog) dan menyalakan LED hanya jika LED berjalan di atas ambang batas tertentu. Mencetak nilai analog tanpa memperhatikan levelnya. Hardware Arduino Board (1) Potentiometer or variable resistor (1) 220 ohm resistor (1) LED hook-up wire
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);
}
dirancang khusus untuk menangani pengambilan keputusan yang melibatkan sejumlah atau banyak alternatif, tetapi penggunaannya hanya untuk memeriksa data yang bertipe karakter atau integer.
SWITCH CASE
Switch memungkinkan kita untuk memilih diantara beberapa pilihan diskrit. Tutorial ini menunjukkan cara berpindah diantara empat keadaan yang diinginkan dari sebuah foto resistor: benarbenar gelap, redup, sedang, dan terang.
Hardware Arduino Board (1) photocell, or analog sensor (1) 10k ohm resistors breadboard hook-up wire
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; } }
Switch digunakan untuk memilih diantara seperangkat nilainilai diskrit dari variabel. Sehingga akan menjalankan program terpilih. Bentuk Umum switch (inByte) { case 'a': digitalWrite(2, HIGH); break case b': digitalWrite(4, HIGH); break; default:;
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.
Hardware Arduino Board (5) LEDs (5) 220 ohm resistors breadboard hook-up wire
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); } }
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();
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.
Hardware Arduino Board (1) digital pushbutton or switch (1) photocell, or analog sensor (2) 10k ohm resistors breadboard
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; }