Advertisement
Guest User

Arduino LED Dice

a guest
Oct 16th, 2017
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. // Arduino LED Dice
  2. // Made by Aleksandar Tsvetkov
  3. // Using button debouncing logic from the Internet!
  4. // Available on Instructables, just search it :)
  5.  
  6. const int pins[6] = {2, 4, 6, 8, 10, 12};
  7. int buttonPin = 13, buttonStatus, randNum, lastNum, i, lastDebounceTime, debounceDelay = 150;
  8.  
  9. void draw(int randN);
  10.  
  11. void setup() {
  12.   for (i = 0; i <= 5; i++) {
  13.     pinMode(pins[i], OUTPUT);
  14.   }
  15.   pinMode(buttonPin, INPUT);
  16. }
  17.  
  18. void loop() {
  19.   buttonStatus = digitalRead(buttonPin);
  20.  
  21.   if (buttonStatus == HIGH && (millis() - lastDebounceTime) > debounceDelay) {
  22.     do {
  23.       randNum = random(1, 6);
  24.     } while (lastNum == randNum);
  25.    
  26.     draw(randNum);
  27.     lastDebounceTime = millis();
  28.     lastNum = randNum;
  29.   }
  30.   delay(100);
  31. }
  32.  
  33. void draw(int randN) {
  34.   for (i = 0; i <= 5; i++) {
  35.     digitalWrite(pins[i], LOW);
  36.   }
  37.   for (i = 0; i <= randN; i++) {
  38.     digitalWrite(pins[i], HIGH);
  39.   }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement