Advertisement
inagantid20

Branching Version 2 LED Dice- Switch Case Statement

Jun 13th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. /*
  2. * Divya Inaganti
  3. * June 9, 2017
  4. * LED Dice Mini Project
  5. * Branching Vesrion 2
  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. switch (counter)
  46. {
  47. case 1:
  48. digitalWrite(pinLed1, HIGH);
  49. digitalWrite(pinLed2, LOW);
  50. digitalWrite(pinLed3, LOW);
  51. break;
  52. case 2:
  53. digitalWrite(pinLed1, HIGH);
  54. digitalWrite(pinLed2, HIGH);
  55. digitalWrite(pinLed3, LOW);
  56. break;
  57. case 3:
  58. digitalWrite(pinLed1, HIGH);
  59. digitalWrite(pinLed2, HIGH) ;
  60. digitalWrite(pinLed3, HIGH);
  61. break;
  62. case 4:
  63. digitalWrite(pinLed1, LOW);
  64. digitalWrite(pinLed2, LOW) ;
  65. digitalWrite(pinLed3, LOW);
  66. break;
  67.  
  68. // else if(lastButton == HIGH && buttonDown == LOW)
  69. //{
  70. //Serial.print (0);
  71. //}
  72.  
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement