Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. /*
  2.  Arduino Dice :)
  3.  
  4.  This example shows how to simulate throwing a dice with 6 LEDs.
  5.  
  6.  The circuit:
  7.  * 6 LEDs attached to consecutive digital pins (with 220 Ohm resistors)
  8.  * Button switch connected to digital pin (see circuit on https://www.arduino.cc/en/Tutorial/Button)
  9.  
  10.  */
  11.  
  12. // set to 1 if we're debugging
  13. #define DEBUG 0
  14.  
  15. // 6 consecutive digital pins for the LEDs
  16. int first = 3;
  17. int second = 4;
  18. int third = 5;
  19. int fourth = 6;
  20. int fifth = 7;
  21. int sixth = 8;
  22.  
  23. // pin for the button switch
  24. int button = 2;
  25. // value to check state of button switch
  26. int pressed = 0;
  27.  
  28. void setup() {
  29.   // set all LED pins to OUTPUT
  30.   for (int i=first; i<=sixth; i++) {
  31.     pinMode(i, OUTPUT);
  32.   }
  33.   // set buttin pin to INPUT
  34.   pinMode(button, INPUT);
  35.  
  36.   // initialize random seed by noise from analog pin 0 (should be unconnected)
  37.   randomSeed(analogRead(0));
  38.  
  39.   // if we're debugging, connect to serial
  40.   #ifdef DEBUG
  41.     Serial.begin(9600);
  42.   #endif
  43.  
  44. }
  45.  
  46. void buildUpTension() {
  47.   // light LEDs from left to right and back to build up tension
  48.   // while waiting for the dice to be thrown
  49.   // left to right
  50.   for (int i=first; i<=sixth; i++) {
  51.     if (i!=first) {
  52.       digitalWrite(i-1, LOW);
  53.     }
  54.     digitalWrite(i, HIGH);
  55.     delay(100);
  56.   }
  57.   // right to left
  58.   for (int i=sixth; i>=first; i--) {
  59.     if (i!=sixth) {
  60.       digitalWrite(i+1, LOW);
  61.     }
  62.     digitalWrite(i, HIGH);
  63.     delay(100);
  64.   }
  65. }
  66.  
  67. void showNumber(int number) {
  68.   digitalWrite(first, HIGH);
  69.   if (number >= 2) {
  70.     digitalWrite(second, HIGH);
  71.   }
  72.   if (number >= 3) {
  73.     digitalWrite(third, HIGH);    
  74.   }
  75.   if (number >= 4) {
  76.     digitalWrite(fourth, HIGH);    
  77.   }
  78.   if (number >= 5) {
  79.     digitalWrite(fifth, HIGH);    
  80.   }
  81.   if (number == 6) {
  82.     digitalWrite(sixth, HIGH);    
  83.   }
  84. }
  85.  
  86. int throwDice() {
  87.   // get a random number in the range [1,6]
  88.   int randNumber = random(1,7);
  89.  
  90.   #ifdef DEBUG
  91.     Serial.println(randNumber);
  92.   #endif
  93.  
  94.   return randNumber;
  95. }
  96.  
  97. void setAllLEDs(int value) {
  98.   for (int i=first; i<=sixth; i++) {
  99.     digitalWrite(i, value);
  100.   }
  101. }
  102.  
  103. void loop() {
  104.   // if button is pressed - throw the dice
  105.   pressed = digitalRead(button);
  106.  
  107.   if (pressed == HIGH) {
  108.     // remove previous number
  109.     setAllLEDs(LOW);
  110.    
  111.     buildUpTension();
  112.     int thrownNumber = throwDice();
  113.     showNumber(thrownNumber);
  114.   }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement