Membuat Dadu Menggunakan LED dan Arduino
Penjelasan
Pada proyek kali ini kita akan membuat dadu menggunakan 6 buah LED yang disusun sedemikian rupa (lihat di skematik rangkaian). Untuk mengacak dadu kita menggunakan input push button.
Skematik Rangkaian
Komponen
- Arduino
- 6x Resistor 220 Ohm atau bebas maksimal 1k Ohm
- Resistor 10k Ohm
- 6x LED
- Push button
Program
long number;
void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(2, INPUT);
}
void loop(){
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
digitalWrite(8,LOW);
digitalWrite(7,LOW);
if(digitalRead(2) == 0){
number = random(1,7);
Serial.println(number);
if(number == 1){
digitalWrite(10,HIGH);
delay(2000);
}
else if(number == 2){
digitalWrite(11,HIGH);
digitalWrite(9,HIGH);
delay(2000);
}
else if(number == 3){
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
delay(2000);
}
else if(number == 4){
digitalWrite(13,HIGH);
digitalWrite(11,HIGH);
digitalWrite(9,HIGH);
digitalWrite(7,HIGH);
delay(2000);
}
else if(number == 5){
digitalWrite(13,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
digitalWrite(7,HIGH);
delay(2000);
}
else if(number == 6){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
digitalWrite(8,HIGH);
digitalWrite(7,HIGH);
delay(2000);
}
else{
digitalWrite(13,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
digitalWrite(8,LOW);
digitalWrite(7,LOW);
delay(2000);
}
}
}
Sekian dan terima kasih. Semoga dapat membantu.
Jangan lupa untuk share ke teman - teman kalian dan tulis komentar kalian di bawah.
0 Comments