ش | ی | د | س | چ | پ | ج |
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
R1 = about 150 Ohm , R2 = about 10 kOhm // // Electronic dice (version 5.12.2012) // // leds are in pins 2-8 and start/stop button in pin 9 // // led-pin relationships: 6 4 // 7 5 3 // 8 2 // // if you change the oder of leds you maybe need to change also pin numbers in the // turnOnNumber() subroutine // // Note. we do not use pins 0 and 1 because they are reserved for transmit and receive // // The dice changes its state when start/stop button is released. // int mode = 0; // mode tells what we are doing, 0=init state int nbr = 1; // nbr tells which value we show in the init mode(=0), start from 1 -> 7 int timer = 10; // timer tells how much we must waite before next nbr in init mode int butPin = 9; // button pin void setup() { int i; for (i=2; i<9; i++) pinMode(i,OUTPUT); // led pins pinMode(butPin,INPUT); // start/stop button turnOffAll(); } void loop() { int n; switch (mode) { case 1: // waiting for button release in init state => start if (digitalRead(butPin) == HIGH) mode = 2; case 0: // still in init state n = int(random(1,7)); // just for avoiding to start always the same sequence timer--; if (timer <=0) { timer = 10; turnOnNumber(nbr); nbr++; if (nbr > 7) nbr = 1; } if (digitalRead(butPin) == LOW) mode = 1; // button push, next wait for button release break; case 2: // just running if (digitalRead(butPin) == LOW) mode = 3; // wait for button release n = int(random(1,7)); turnOnNumber(n); break; case 3: // waiting for button release in run state => stop if (digitalRead(butPin) == HIGH) mode = 4; n = int(random(1,7)); turnOnNumber(n); break; case 4: // stop now and just show the last number timer = 50; // show the number 5 seconds (=50*0.1s) nbr = 1; // then behave as in init state mode = 0; break; default: break; } delay(100); // wait 100 ms i.e. 0.1 second } // ***** set up leds corresponding the number ***** void turnOnNumber(int n) { if (n<1 || n>7) return; // bad number if not between 1-7 turnOffAll(); // first clear the previous situation switch(n) // then setup the new one { int i; case 1: digitalWrite(5, HIGH); break; case 2: digitalWrite(2, HIGH); digitalWrite(6, HIGH); break; case 3: digitalWrite(4, HIGH); digitalWrite(5, HIGH); digitalWrite(8, HIGH); break; case 4: digitalWrite(2, HIGH); digitalWrite(4, HIGH); digitalWrite(6, HIGH); digitalWrite(8, HIGH); break; case 5: digitalWrite(2, HIGH); digitalWrite(4, HIGH); digitalWrite(5, HIGH); digitalWrite(6, HIGH); digitalWrite(8, HIGH); break; case 6: digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); digitalWrite(6, HIGH); digitalWrite(7, HIGH); digitalWrite(8, HIGH); break; case 7: turnOnAll(); break; default: break; } return; } // ***** turn off all leds ***** void turnOffAll() { int i; for (i=2; i<9; i++) digitalWrite(i, LOW); return; } // ***** turn on all leds ***** void turnOnAll() { int i; for (i=2; i<9; i++) digitalWrite(i, HIGH); return; } |