Advertisement
vawrinekm20

6/12/17: Switch

Jun 12th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. /*Matt Vawrinek
  2. * 6/09/17
  3. * Dice Mini Project
  4. */
  5.  
  6. const int pinLed1 = 2;
  7. const int pinLed2 = 3;
  8. const int pinLed3 = 4;
  9. const int pinButt = 5;
  10. boolean buttPressed = 0; //this is to tell whether the button is pressed//
  11. boolean ledOn = 0; //this is for the LED//
  12. boolean prevButt = 0;
  13. int counter = 0; //keeping track of how many times you press the button//
  14.  
  15. void setup()
  16. {
  17. pinMode (pinLed1, OUTPUT);
  18. pinMode (pinLed2, OUTPUT);
  19. pinMode (pinLed3, OUTPUT);
  20. pinMode (pinButt, INPUT);
  21.  
  22. Serial.begin(9600);
  23.  
  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. }
  35.  
  36. void loop()
  37. {
  38. // put your main code here, to run repeatedly:
  39.  
  40.  
  41. buttPressed = debounce(prevButt);
  42. if (prevButt == LOW && buttPressed == HIGH)
  43. {
  44. counter++; //++ means add one//
  45. }
  46.  
  47. switch(counter)
  48. {
  49. case 1:
  50. digitalWrite(pinLed1, HIGH);
  51. digitalWrite(pinLed2, LOW);
  52. digitalWrite(pinLed3, LOW);
  53. //do something//
  54. break;
  55.  
  56. case 2:
  57. digitalWrite(pinLed1, HIGH);
  58. digitalWrite(pinLed2, HIGH);
  59. digitalWrite(pinLed3, LOW);
  60. //do something else//
  61. break;
  62.  
  63. case 3:
  64. digitalWrite(pinLed1, HIGH);
  65. digitalWrite(pinLed2, HIGH);
  66. digitalWrite(pinLed3, HIGH);
  67. break;
  68.  
  69. case 4:
  70. digitalWrite(pinLed1, LOW);
  71. digitalWrite(pinLed2, LOW);
  72. digitalWrite(pinLed3, LOW);
  73. counter = 0;
  74. break;
  75. }
  76.  
  77.  
  78. prevButt = buttPressed; //compare end with start, new value becomes the old value//
  79.  
  80. }
  81.  
  82.  
  83.  
  84.  
  85. boolean debounce(boolean last)
  86. {
  87. boolean current = digitalRead(pinButt);
  88. if (last != current) // != means not equal to//
  89. {
  90. delay(5);
  91. current = digitalRead(pinButt);
  92. }
  93. return current;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement