Zufallsgenerator (Würfel)
Bild des fertigen Shields
http://www.erik-bartmann.de/images/programmierung/arduino2/projektkapitel9/wuerfelshield004.jpg
Code:
int FIRSTLED=6; int pattern[6][7]={ {0,0,0,1,0,0,0}, //1 {0,1,0,0,0,1,0}, //2 {1,0,0,1,0,0,1}, //3 {1,1,0,0,0,1,1}, //4 {1,1,0,1,0,1,1}, //5 {1,1,1,0,1,1,1} //6 }; void setup() { int i; for (i=FIRSTLED;i<=FIRSTLED+6;++i) pinMode(i,OUTPUT); pinMode(2,INPUT); } void ShowNumber(int num) { int i; for (i=0;i<=6;++i) digitalWrite(i+FIRSTLED,pattern[num][i]?HIGH:LOW); } void loop(){ int i,j; unsigned randomnumber; digitalWrite(6,HIGH); digitalWrite(7,HIGH); digitalWrite(8,HIGH); digitalWrite(9,HIGH); digitalWrite(10,HIGH); digitalWrite(11,HIGH); digitalWrite(12,HIGH); delay(200); do { i=(millis()/200)%6; ShowNumber(i); } while (digitalRead(2)==LOW); while(1){ randomnumber=0; do { ++randomnumber; i=(millis()/70)%7; for (j=0;j<=6;++j) digitalWrite(j+FIRSTLED,j==i?HIGH:LOW); } while (digitalRead(2)==HIGH); randomnumber=randomnumber%6; ShowNumber(randomnumber); while (digitalRead(2)==LOW) {}; }
}
Material zum Nachbauen:
- Shield
- 7 LED's
- 8 Widerstände
- Schalter
- Arduino
Erstellt: Julius Meyer (15.06.2015)
Letzte Änderung: Julius Meyer (08.07.2015)