SlideShare a Scribd company logo
1 of 20
ARDUINO
CONTROL
Oleh
Al Aziz
Berlian Siregar (11410387)
Dwi Ermawati
Terdiri Dari
Arrays
For Loop Iteration
If Statement Conditional
Switch Case
Switch Case2
WhileStatementConditional
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.
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
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);
}
}
FOR LOOP ITERATION
• Operator for digunakan dalam blok
pengulangan tertutup
• Statement
For ( initialization; condition;
expression ) { //doSomethig; }
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
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 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; }
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
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);
}
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.
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
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 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:;
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
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);
}
}
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();
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
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;
}
}

More Related Content

Similar to ARDUINO-KONTROL

Automatic egg incubator
Automatic egg incubatorAutomatic egg incubator
Automatic egg incubatorRaosan Lillahi
 
Projek arduino uno with sound sensor
Projek arduino uno with sound sensorProjek arduino uno with sound sensor
Projek arduino uno with sound sensorBernaz Bullah
 
Project arduino uno with sound sensor
Project arduino uno with sound sensorProject arduino uno with sound sensor
Project arduino uno with sound sensoranahadijah
 
Projek arduino uno with sound sensor
Projek arduino uno with sound sensorProjek arduino uno with sound sensor
Projek arduino uno with sound sensorMuhammad Alwan
 
Project arduino uno with sound sensor
Project arduino uno with sound sensorProject arduino uno with sound sensor
Project arduino uno with sound sensorMuhammad Rezza
 
Arduino coding.ppt
Arduino coding.pptArduino coding.ppt
Arduino coding.pptdidikmaarif
 
Modul arduino iii
Modul arduino iiiModul arduino iii
Modul arduino iiisutono stn
 
TB1_Aplikasi Volt Meter menggunakan Arduino UNO.pptx
TB1_Aplikasi Volt Meter menggunakan Arduino UNO.pptxTB1_Aplikasi Volt Meter menggunakan Arduino UNO.pptx
TB1_Aplikasi Volt Meter menggunakan Arduino UNO.pptxBeeSiiJeje
 
17 proyek arduino
17 proyek arduino17 proyek arduino
17 proyek arduinoJilun
 
Tugas akhir muhamad iqbal ramdhani arduino sensor gas
Tugas akhir muhamad iqbal ramdhani arduino sensor gasTugas akhir muhamad iqbal ramdhani arduino sensor gas
Tugas akhir muhamad iqbal ramdhani arduino sensor gasMuhamadIqbalRamdhani
 
Latihan Arduino.pptx
Latihan Arduino.pptxLatihan Arduino.pptx
Latihan Arduino.pptxVidi34
 
Mikrokontroler io tombol dan led
Mikrokontroler io tombol dan ledMikrokontroler io tombol dan led
Mikrokontroler io tombol dan ledJodit Sulistyo
 
Arduino dasar untuk orang biasa
Arduino dasar untuk orang biasaArduino dasar untuk orang biasa
Arduino dasar untuk orang biasaGo Asgard
 
Project instrumentasi kelompok1
Project instrumentasi kelompok1Project instrumentasi kelompok1
Project instrumentasi kelompok1yana cahyana
 
Basic avr-microcontroller-tutorial v3
Basic avr-microcontroller-tutorial v3Basic avr-microcontroller-tutorial v3
Basic avr-microcontroller-tutorial v3Mabekni Yulianto
 
Basic avr-microcontroller-tutorial 8535
Basic avr-microcontroller-tutorial 8535Basic avr-microcontroller-tutorial 8535
Basic avr-microcontroller-tutorial 8535LAZY MAGICIAN
 
Laporan praktikum mikrokontroler dengan led
Laporan praktikum mikrokontroler dengan ledLaporan praktikum mikrokontroler dengan led
Laporan praktikum mikrokontroler dengan ledSawah Dan Ladang Ku
 
Laporan praktikum mikrokontroler dengan led
Laporan praktikum mikrokontroler dengan led Laporan praktikum mikrokontroler dengan led
Laporan praktikum mikrokontroler dengan led Wesnu Prajati
 

Similar to ARDUINO-KONTROL (20)

Automatic egg incubator
Automatic egg incubatorAutomatic egg incubator
Automatic egg incubator
 
Projek arduino uno with sound sensor
Projek arduino uno with sound sensorProjek arduino uno with sound sensor
Projek arduino uno with sound sensor
 
Project arduino uno with sound sensor
Project arduino uno with sound sensorProject arduino uno with sound sensor
Project arduino uno with sound sensor
 
Projek arduino uno with sound sensor
Projek arduino uno with sound sensorProjek arduino uno with sound sensor
Projek arduino uno with sound sensor
 
Project arduino uno with sound sensor
Project arduino uno with sound sensorProject arduino uno with sound sensor
Project arduino uno with sound sensor
 
Arduino coding.ppt
Arduino coding.pptArduino coding.ppt
Arduino coding.ppt
 
Modul arduino iii
Modul arduino iiiModul arduino iii
Modul arduino iii
 
TB1_Aplikasi Volt Meter menggunakan Arduino UNO.pptx
TB1_Aplikasi Volt Meter menggunakan Arduino UNO.pptxTB1_Aplikasi Volt Meter menggunakan Arduino UNO.pptx
TB1_Aplikasi Volt Meter menggunakan Arduino UNO.pptx
 
17 proyek arduino
17 proyek arduino17 proyek arduino
17 proyek arduino
 
ARDUINO_BASIC_TRAINING.ppt
ARDUINO_BASIC_TRAINING.pptARDUINO_BASIC_TRAINING.ppt
ARDUINO_BASIC_TRAINING.ppt
 
Tugas akhir muhamad iqbal ramdhani arduino sensor gas
Tugas akhir muhamad iqbal ramdhani arduino sensor gasTugas akhir muhamad iqbal ramdhani arduino sensor gas
Tugas akhir muhamad iqbal ramdhani arduino sensor gas
 
ARDUINO_BASIC_TRAINING.ppt
ARDUINO_BASIC_TRAINING.pptARDUINO_BASIC_TRAINING.ppt
ARDUINO_BASIC_TRAINING.ppt
 
Latihan Arduino.pptx
Latihan Arduino.pptxLatihan Arduino.pptx
Latihan Arduino.pptx
 
Mikrokontroler io tombol dan led
Mikrokontroler io tombol dan ledMikrokontroler io tombol dan led
Mikrokontroler io tombol dan led
 
Arduino dasar untuk orang biasa
Arduino dasar untuk orang biasaArduino dasar untuk orang biasa
Arduino dasar untuk orang biasa
 
Project instrumentasi kelompok1
Project instrumentasi kelompok1Project instrumentasi kelompok1
Project instrumentasi kelompok1
 
Basic avr-microcontroller-tutorial v3
Basic avr-microcontroller-tutorial v3Basic avr-microcontroller-tutorial v3
Basic avr-microcontroller-tutorial v3
 
Basic avr-microcontroller-tutorial 8535
Basic avr-microcontroller-tutorial 8535Basic avr-microcontroller-tutorial 8535
Basic avr-microcontroller-tutorial 8535
 
Laporan praktikum mikrokontroler dengan led
Laporan praktikum mikrokontroler dengan ledLaporan praktikum mikrokontroler dengan led
Laporan praktikum mikrokontroler dengan led
 
Laporan praktikum mikrokontroler dengan led
Laporan praktikum mikrokontroler dengan led Laporan praktikum mikrokontroler dengan led
Laporan praktikum mikrokontroler dengan led
 

More from jhcid

wireless-communication-architecture
 wireless-communication-architecture wireless-communication-architecture
wireless-communication-architecturejhcid
 
a-presentation-on-wireless-communication
 a-presentation-on-wireless-communication a-presentation-on-wireless-communication
a-presentation-on-wireless-communicationjhcid
 
sonar
sonarsonar
sonarjhcid
 
satellite-communications
 satellite-communications satellite-communications
satellite-communicationsjhcid
 
ppt-on-satellite
ppt-on-satelliteppt-on-satellite
ppt-on-satellitejhcid
 
satellite-communications
satellite-communicationssatellite-communications
satellite-communicationsjhcid
 
satellite-communication
 satellite-communication satellite-communication
satellite-communicationjhcid
 
satellite-communication-ppt
satellite-communication-pptsatellite-communication-ppt
satellite-communication-pptjhcid
 
radar
 radar radar
radarjhcid
 
radar
 radar radar
radarjhcid
 
radar-principles
radar-principlesradar-principles
radar-principlesjhcid
 
radar
radarradar
radarjhcid
 
cellular-communication
cellular-communicationcellular-communication
cellular-communicationjhcid
 
cellular communication
cellular communicationcellular communication
cellular communicationjhcid
 
-introduction-to-cellular-mobile-communications
-introduction-to-cellular-mobile-communications-introduction-to-cellular-mobile-communications
-introduction-to-cellular-mobile-communicationsjhcid
 
cellular-communications
 cellular-communications cellular-communications
cellular-communicationsjhcid
 
arduino-1
 arduino-1 arduino-1
arduino-1jhcid
 
arduino
 arduino arduino
arduinojhcid
 
arduino-ppt
 arduino-ppt arduino-ppt
arduino-pptjhcid
 
microprocessor and microcntroller
microprocessor and microcntrollermicroprocessor and microcntroller
microprocessor and microcntrollerjhcid
 

More from jhcid (20)

wireless-communication-architecture
 wireless-communication-architecture wireless-communication-architecture
wireless-communication-architecture
 
a-presentation-on-wireless-communication
 a-presentation-on-wireless-communication a-presentation-on-wireless-communication
a-presentation-on-wireless-communication
 
sonar
sonarsonar
sonar
 
satellite-communications
 satellite-communications satellite-communications
satellite-communications
 
ppt-on-satellite
ppt-on-satelliteppt-on-satellite
ppt-on-satellite
 
satellite-communications
satellite-communicationssatellite-communications
satellite-communications
 
satellite-communication
 satellite-communication satellite-communication
satellite-communication
 
satellite-communication-ppt
satellite-communication-pptsatellite-communication-ppt
satellite-communication-ppt
 
radar
 radar radar
radar
 
radar
 radar radar
radar
 
radar-principles
radar-principlesradar-principles
radar-principles
 
radar
radarradar
radar
 
cellular-communication
cellular-communicationcellular-communication
cellular-communication
 
cellular communication
cellular communicationcellular communication
cellular communication
 
-introduction-to-cellular-mobile-communications
-introduction-to-cellular-mobile-communications-introduction-to-cellular-mobile-communications
-introduction-to-cellular-mobile-communications
 
cellular-communications
 cellular-communications cellular-communications
cellular-communications
 
arduino-1
 arduino-1 arduino-1
arduino-1
 
arduino
 arduino arduino
arduino
 
arduino-ppt
 arduino-ppt arduino-ppt
arduino-ppt
 
microprocessor and microcntroller
microprocessor and microcntrollermicroprocessor and microcntroller
microprocessor and microcntroller
 

ARDUINO-KONTROL

  • 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; }
  • 7. 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
  • 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; } }