Elektronik Zar Yapımı | Arduino ve Led Kullanarak Zar Yap

Malzeme Listesi
Elektronik Zar Yapımı Bağlantı Şeması

Arduino Kodları
#define DEBUG 0
// 6 consecutive digital pins for the LEDs
intfirst=2;
intsecond=3;
intthird=4;
intfourth=5;
intfifth=6;
intsixth=7;
// pin for the button switch
intbutton=12;
// value to check state of button switch
intpressed=0;
voidsetup(){
// set all LED pins to OUTPUT
for(inti=first;i<=sixth;i++){
pinMode(i,OUTPUT);
}
// set buttin pin to INPUT
pinMode(button,INPUT);
// initialize random seed by noise from analog pin 0 (should be unconnected)
randomSeed(analogRead(0));
// if we're debugging, connect to serial
#ifdef DEBUG
Serial.begin(9600);
#endif
}
voidbuildUpTension(){
// light LEDs from left to right and back to build up tension
// while waiting for the dice to be thrown
// left to right
for(inti=first;i<=sixth;i++){
if(i!=first){
digitalWrite(i-1,LOW);
}
digitalWrite(i,HIGH);
delay(100);
}
// right to left
for(inti=sixth;i>=first;i--){
if(i!=sixth){
digitalWrite(i+1,LOW);
}
digitalWrite(i,HIGH);
delay(100);
}
}
voidshowNumber(intnumber){
digitalWrite(first,HIGH);
if(number>=2){
digitalWrite(second,HIGH);
}
if(number>=3){
digitalWrite(third,HIGH);
}
if(number>=4){
digitalWrite(fourth,HIGH);
}
if(number>=5){
digitalWrite(fifth,HIGH);
}
if(number==6){
digitalWrite(sixth,HIGH);
}
}
intthrowDice(){
// get a random number in the range [1,6]
intrandNumber=random(1,7);
#ifdef DEBUG
Serial.println(randNumber);
#endif
returnrandNumber;
}
voidsetAllLEDs(intvalue){
for(inti=first;i<=sixth;i++){
digitalWrite(i,value);
}
}
voidloop(){
// if button is pressed - throw the dice
pressed=digitalRead(button);
if(pressed==HIGH){
// remove previous number
setAllLEDs(LOW);
buildUpTension();
intthrownNumber=throwDice();
showNumber(thrownNumber);
}
}

Güneş Paneli ile Taşınabilir Şarj İstasyonu Yapımı

Li-ion ve Li-Po Bataryalar Arasındaki Farklar: Hangi Batarya Hangi Proje İçin Uygun?

MPU-9250: 9 Eksenli İvme Sensörü - Kapsamlı Başlangıç Rehberi

ULN2003 Entegresi Nedir ?

Seri Dönüştürücü Kartları (RS232, RS485, TTL) Nedir ve Nerelerde Kullanılır?

.png)