inagantid20

Branching Version 1 LED Dice- Counter Activity

Jun 13th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. /*
  2. * Divya Inaganti
  3. * June 9, 2017
  4. * LED Dice Mini Project
  5. * Branching Version 1
  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. digitalWrite (pinLed1, HIGH);
  24. digitalWrite (pinLed2, HIGH);
  25. digitalWrite (pinLed3, HIGH);
  26. delay(500);
  27. digitalWrite (pinLed1, LOW);
  28. digitalWrite (pinLed2, LOW);
  29. digitalWrite (pinLed3, LOW);
  30.  
  31. }
  32.  
  33. void loop() {
  34. // put your main code here, to run repeatedly:
  35. buttonDown = digitalRead(pinButton);
  36. //Serial.print (buttonDown);
  37.  
  38.  
  39. buttonDown = debounce(lastButton); // will go and read the Button
  40.  
  41. if (lastButton == LOW && buttonDown == HIGH) // Checks if the button has been pressed
  42. {
  43. counter = counter + 1;
  44. }
  45. // else if(lastButton == HIGH && buttonDown == LOW)
  46. //{
  47. //Serial.print (0);
  48. //}
  49. if (counter == 1)
  50. {
  51. digitalWrite(pinLed1, HIGH);
  52. digitalWrite(pinLed2, LOW);
  53. digitalWrite(pinLed3, LOW);
  54. }
  55. else if (counter == 2)
  56. {
  57. digitalWrite(pinLed1, HIGH);
  58. digitalWrite(pinLed2, HIGH);
  59. digitalWrite(pinLed3, LOW);
  60. }
  61. else if (counter ==3)
  62. {
  63. digitalWrite(pinLed1, HIGH);
  64. digitalWrite(pinLed2, HIGH);
  65. digitalWrite(pinLed3, HIGH);
  66. }
  67. else
  68. {
  69. digitalWrite(pinLed1, LOW);
  70. digitalWrite(pinLed2, LOW);
  71. digitalWrite(pinLed3, LOW);
  72. counter = 0;
  73. }
  74. lastButton = buttonDown;
  75.  
  76. }
  77.  
  78. boolean debounce(boolean last)
  79.  
  80. {
  81. boolean current = digitalRead(pinButton);
  82. if (last != current)
  83. {
  84. delay(5);
  85. current = digitalRead(pinButton);
  86. }
  87. return current;
  88. }
Add Comment
Please, Sign In to add comment