Advertisement
inagantid20

Random Program LED Dice Activity

Jun 13th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. /*
  2. * Divya Inaganti
  3. * June 9, 2017
  4. * LED Dice Mini Project
  5. * Branching if - Random
  6. */
  7. const int pinLed1 = 2;
  8. const int pinLed2 = 7;
  9. const int pinLed3 = 4;
  10. const int pinButton = 8;
  11. boolean buttonDown = 0;
  12. boolean ledOn = 0;
  13. boolean lastButton = 0;
  14. int counter = 0;
  15. void setup() {
  16. // put your setup code here, to run once:
  17. pinMode (pinLed1, OUTPUT);
  18. pinMode (pinLed2, OUTPUT);
  19. pinMode (pinLed3, OUTPUT);
  20. pinMode (pinButton, INPUT);
  21.  
  22. Serial. begin (9600);
  23. randomSeed(analogRead(A0));
  24. digitalWrite (pinLed1, HIGH);
  25. digitalWrite (pinLed2, HIGH);
  26. digitalWrite (pinLed3, HIGH);
  27. delay(500);
  28. digitalWrite (pinLed1, LOW);
  29. digitalWrite (pinLed2, LOW);
  30. digitalWrite (pinLed3, LOW);
  31.  
  32. }
  33.  
  34. void loop() {
  35. // put your main code here, to run repeatedly:
  36. buttonDown = digitalRead(pinButton);
  37. //Serial.print (buttonDown);
  38.  
  39.  
  40. buttonDown = debounce(lastButton); // will go and read the Button
  41.  
  42. if (lastButton == LOW && buttonDown == HIGH) // Checks if the button has been pressed
  43. {
  44. counter = random (1,5);
  45. Serial.println(counter);
  46. }
  47. switch (counter)
  48. {
  49. case 1:
  50. digitalWrite(pinLed1, HIGH);
  51. digitalWrite(pinLed2, LOW);
  52. digitalWrite(pinLed3, LOW);
  53. break;
  54. case 2:
  55. digitalWrite(pinLed1, HIGH);
  56. digitalWrite(pinLed2, HIGH);
  57. digitalWrite(pinLed3, LOW);
  58. break;
  59. case 3:
  60. digitalWrite(pinLed1, HIGH);
  61. digitalWrite(pinLed2, HIGH) ;
  62. digitalWrite(pinLed3, HIGH);
  63. break;
  64. case 4:
  65. digitalWrite(pinLed1, LOW);
  66. digitalWrite(pinLed2, LOW) ;
  67. digitalWrite(pinLed3, LOW);
  68. counter = 0;
  69. break;
  70. default:
  71.  
  72.  
  73. // else if(lastButton == HIGH && buttonDown == LOW)
  74. //{
  75. //Serial.print (0);
  76. //}
  77.  
  78. }
  79. lastButton = buttonDown;
  80.  
  81. }
  82.  
  83. boolean debounce(boolean last)
  84.  
  85. {
  86. boolean current = digitalRead(pinButton);
  87. if (last != current)
  88. {
  89. delay(5);
  90. current = digitalRead(pinButton);
  91. }
  92. return current;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement