2. Terdiri Dari
Arrays
For Loop Iteration
If Statement Conditional
Switch Case
Switch Case2
WhileStatementConditional
3. 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.
4. 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
5. 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 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);
}
}
6. FOR LOOP ITERATION
• Operator for digunakan dalam blok
pengulangan tertutup
• Statement
For ( initialization; condition;
expression ) { //doSomethig; }
8. 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);
}
}
9. 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; }
10. PENGGUNAAN IF STATEMENT
CONDITIONAL PADA ARDUINO
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
11. 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);
}
12. 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.
13. 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.
Hardware
Arduino Board
(1) photocell, or analog sensor
(1) 10k ohm resistors
breadboard
hook-up wire
14. 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;
}
}
15. SWITCH CASE 2 (WITH
SERIAL)
Switch digunakan untuk memilih diantara seperangkat nilai-
nilai 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:;
16. 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.
Hardware
Arduino Board
(5) LEDs
(5) 220 ohm resistors
breadboard
hook-up wire
17. 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);
}
}
18. 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();
19. 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.
Hardware
Arduino Board
(1) digital pushbutton or switch
(1) photocell, or analog sensor
(2) 10k ohm resistors
breadboard
20. 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;
}
}