Advertisement
belrey10

Lesson #8: LED Dice

Nov 2nd, 2024 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  Created 5 Jan 2017
  11.  By Esther van der Stappen
  12.  
  13.  This example code is in the public domain. Credit: https://create.arduino.cc/projecthub/EvdS/led-dice-885cf1?ref=similar&ref_id=21382&offset=4
  14.  
  15.  */
  16.  
  17. // set to 1 if we're debugging
  18. #define DEBUG 0
  19.  
  20. // 6 consecutive digital pins for the LEDs
  21. int first = 2;
  22. int second = 3;
  23. int third = 4;
  24. int fourth = 5;
  25. int fifth = 6;
  26. int sixth = 7;
  27.  
  28. // pin for the button switch
  29. int button = 12;
  30. // value to check state of button switch
  31. int pressed = 0;
  32.  
  33. void setup() {
  34.   // set all LED pins to OUTPUT
  35.   for (int i=first; i<=sixth; i++) {
  36.     pinMode(i, OUTPUT);
  37.   }
  38.   // set buttin pin to INPUT
  39.   pinMode(button, INPUT);
  40.  
  41.   // initialize random seed by noise from analog pin 0 (should be unconnected)
  42.   randomSeed(analogRead(0));
  43.  
  44.   // if we're debugging, connect to serial
  45.   #ifdef DEBUG
  46.     Serial.begin(9600);
  47.   #endif
  48.  
  49. }
  50.  
  51. void buildUpTension() {
  52.   // light LEDs from left to right and back to build up tension
  53.   // while waiting for the dice to be thrown
  54.   // left to right
  55.   for (int i=first; i<=sixth; i++) {
  56.     if (i!=first) {
  57.       digitalWrite(i-1, LOW);
  58.     }
  59.     digitalWrite(i, HIGH);
  60.     delay(100);
  61.   }
  62.   // right to left
  63.   for (int i=sixth; i>=first; i--) {
  64.     if (i!=sixth) {
  65.       digitalWrite(i+1, LOW);
  66.     }
  67.     digitalWrite(i, HIGH);
  68.     delay(100);
  69.   }
  70. }
  71.  
  72. void showNumber(int number) {
  73.   digitalWrite(first, HIGH);
  74.   if (number >= 2) {
  75.     digitalWrite(second, HIGH);
  76.   }
  77.   if (number >= 3) {
  78.     digitalWrite(third, HIGH);    
  79.   }
  80.   if (number >= 4) {
  81.     digitalWrite(fourth, HIGH);    
  82.   }
  83.   if (number >= 5) {
  84.     digitalWrite(fifth, HIGH);    
  85.   }
  86.   if (number == 6) {
  87.     digitalWrite(sixth, HIGH);    
  88.   }
  89. }
  90.  
  91. int throwDice() {
  92.   // get a random number in the range [1,6]
  93.   int randNumber = random(1,7);
  94.  
  95.   #ifdef DEBUG
  96.     Serial.println(randNumber);
  97.   #endif
  98.  
  99.   return randNumber;
  100. }
  101.  
  102. void setAllLEDs(int value) {
  103.   for (int i=first; i<=sixth; i++) {
  104.     digitalWrite(i, value);
  105.   }
  106. }
  107.  
  108. void loop() {
  109.   // if button is pressed - throw the dice
  110.   pressed = digitalRead(button);
  111.  
  112.   if (pressed == HIGH) {
  113.     // remove previous number
  114.     setAllLEDs(LOW);
  115.    
  116.     buildUpTension();
  117.     int thrownNumber = throwDice();
  118.     showNumber(thrownNumber);
  119.   }
  120.  
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement