Pada percobaan ini kita akan membangun 5 buah tombol pilihan yang terpasang pada pin A0, A1, A2, A3 dan A4. Jika ke 5 buah (semua) tombol ini tidak ditekan, maka lampu LED 6 akan otomatis menyala, Jika tombol A0 ditekan, maka lampu pada LED 11 yang akan menyalah, jika tombol A1 ditekan, maka lampu pada LED 10 yang akan menyala dan seterusnya hingga tombol ke A4 yang menyalakan lampu LED 7.
Inilah ode Program Arduinonya :
int F; int tombol1 = A0; int tombol2 = A1; int tombol3 = A2; int tombol4 = A3; int tombol5 = A4; int led; void setup () { pinMode(tombol1, INPUT); pinMode(tombol2, INPUT); pinMode(tombol3, INPUT); pinMode(tombol4, INPUT); pinMode(tombol5, INPUT); for(led=2; led<13; led++) {pinMode(led, OUTPUT);} } void loop() { if(digitalRead(tombol1) == 1) {F = 1;} else if(digitalRead(tombol2) == 1) {F = 2;} else if(digitalRead(tombol3) == 1) {F = 3;} else if(digitalRead(tombol4) == 1) {F = 4;} else if(digitalRead(tombol5) == 1) {F = 5;} else {F = 0;} switch (F) { case 1 : digitalWrite(7, LOW); digitalWrite(2, HIGH); break; case 2 : digitalWrite(7, LOW); digitalWrite(3, HIGH); break; case 3 : digitalWrite(7, LOW); digitalWrite(4, HIGH); break; case 4 : digitalWrite(7, LOW); digitalWrite(5, HIGH); break; case 5 : digitalWrite(7, LOW); digitalWrite(6, HIGH); break; default : digitalWrite(7, HIGH); digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW); break; } }
atau bisa juga menggunakan sketch program arduino seperti di bawah ini :
int F; int tombol1 = A0; int tombol2 = A1; int tombol3 = A2; int tombol4 = A3; int tombol5 = A4; int led; void setup () { pinMode(tombol1, INPUT); pinMode(tombol2, INPUT); pinMode(tombol3, INPUT); pinMode(tombol4, INPUT); pinMode(tombol5, INPUT); for(led=2; led<13; led++) {pinMode(led, OUTPUT);} } void loop() { if(digitalRead(tombol1) == 1) {digitalWrite(7, LOW); digitalWrite(2, HIGH); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW);} else if(digitalRead(tombol2) == 1) {digitalWrite(7, LOW); digitalWrite(2, LOW); digitalWrite(3, HIGH); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW);} else if(digitalRead(tombol3) == 1) {digitalWrite(7, LOW); digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, HIGH); digitalWrite(5, LOW); digitalWrite(6, LOW);} else if(digitalRead(tombol4) == 1) {digitalWrite(7, LOW); digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, HIGH); digitalWrite(6, LOW);} else if(digitalRead(tombol5) == 1) {digitalWrite(7, LOW); digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, HIGH);} else {digitalWrite(7, HIGH); digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW); } }
Terima kasih semoga ada faedahnya !
ConversionConversion EmoticonEmoticon