Advertisement
coltonspastebin

LED Dice

Dec 14th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. const int button = 12;
  2. const int pin1 = 3;//middle led
  3. const int pin2 = 4;//middle layer outer two
  4. const int pin3 = 6;//bottom layer
  5. const int pin4 = 5;//top layer
  6. int randi;
  7. int readValue = 0;
  8. boolean lastbutton = false;
  9. boolean currentbutton = false;
  10.  
  11. void setup()
  12. {
  13. randomSeed(A0);//allows me to give a range instead of being limited to one number
  14. pinMode (button, INPUT);
  15. pinMode (pin1, OUTPUT);
  16. pinMode (pin2, OUTPUT);
  17. pinMode (pin3, OUTPUT);
  18. pinMode (pin4, OUTPUT);
  19. Serial.begin (9600);
  20. }
  21. boolean debounce (boolean lastbutton)
  22. {
  23. boolean currentbutton = digitalRead (button);
  24. if (lastbutton != currentbutton)
  25. {
  26. delay (5);
  27. currentbutton = digitalRead (button);
  28. }
  29. return currentbutton;
  30. }
  31.  
  32. void loop()
  33. {
  34. currentbutton = debounce (lastbutton);//this is used so that the LED will stay on without having to hold down the button
  35. if (currentbutton == true && lastbutton == false)
  36. {
  37. randi = random (1,7);//this gives me a random number from 1-7 (1 is included but 7 is not): could also do random (6)+1
  38. Serial.println (randi);
  39. digitalWrite (pin1, HIGH);
  40. digitalWrite (pin2, HIGH);
  41. digitalWrite (pin3, HIGH);
  42. digitalWrite (pin4, HIGH);
  43. delay (50);
  44. digitalWrite (pin1, LOW);
  45. digitalWrite (pin2, LOW);
  46. digitalWrite (pin3, LOW);
  47. digitalWrite (pin4, LOW);
  48. delay (10);
  49. digitalWrite (pin1, HIGH);
  50. digitalWrite (pin2, HIGH);
  51. digitalWrite (pin3, HIGH);
  52. digitalWrite (pin4, HIGH);
  53. delay (50);
  54. digitalWrite (pin1, LOW);
  55. digitalWrite (pin2, LOW);
  56. digitalWrite (pin3, LOW);
  57. digitalWrite (pin4, LOW);
  58. }
  59. switch (randi)//this gives me cases that correlate to the number generated by the rani
  60. {
  61. case 1://if number is 1 only the middle LED will turn on
  62. digitalWrite (pin1, HIGH);
  63. break;
  64. case 2://if number is 2 the middle outside LED's will turn on
  65. digitalWrite (pin2, HIGH);
  66. break;
  67. case 3://if number is 3 the middle row LED's will turn on
  68. digitalWrite (pin1, HIGH);
  69. digitalWrite (pin2, HIGH);
  70. break;
  71. case 4: //if number is 4 the top and bottom layers of LED's will turn on
  72. digitalWrite (pin3, HIGH);
  73. digitalWrite (pin4, HIGH);
  74. break;
  75. case 5://if number is 5 the top and bottom as well as the middle LED will turn on
  76. digitalWrite (pin3, HIGH);
  77. digitalWrite (pin4, HIGH);
  78. digitalWrite (pin1, HIGH);
  79. break;
  80. case 6://if number is 6 every LED will be on EXCEPT for the middle LED
  81. digitalWrite (pin3, HIGH);
  82. digitalWrite (pin4, HIGH);
  83. digitalWrite (pin2, HIGH);
  84. break;
  85. default://if there is no number then no LED will be turned on
  86. digitalWrite (pin1, LOW);
  87. digitalWrite (pin2, LOW);
  88. digitalWrite (pin3, LOW);
  89. digitalWrite (pin4, LOW);
  90. break;
  91.  
  92. }
  93. lastbutton = currentbutton;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement