britneybeatey

Branching If

Jun 12th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. /*
  2. * Britney Beatey
  3. * Version 2
  4. * Dice Prototype
  5. */
  6. const int pinLed1 = 7;
  7. const int pinLed2 = 5;
  8. const int pinLed3 = 3;
  9. const int pinButt = 10;
  10. boolean buttDown = 0;
  11. boolean ledOn = 0;
  12. boolean lastButt = 0;
  13. int counter = 0;
  14.  
  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 (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. void loop()
  35. {
  36. // put your main code here, to run repeatedly:
  37.  
  38. buttDown = debounce(lastButt); // will go and read the Button
  39. if (lastButt == LOW && buttDown == HIGH) // Checks if the button has been pressed
  40. {
  41. counter = counter + 1;
  42. Serial.println(counter);
  43. }
  44. if (counter == 1)
  45. {
  46. digitalWrite (pinLed1, HIGH);
  47. digitalWrite (pinLed2, LOW);
  48. digitalWrite (pinLed3, LOW);
  49. }
  50. else if (counter == 2)
  51. {
  52. digitalWrite (pinLed1, HIGH);
  53. digitalWrite (pinLed2, HIGH);
  54. digitalWrite (pinLed3, LOW);
  55. }
  56. else if (counter == 3)
  57. {
  58. digitalWrite (pinLed1, HIGH);
  59. digitalWrite (pinLed2, HIGH);
  60. digitalWrite (pinLed3, HIGH);
  61. }
  62. else
  63. {
  64. digitalWrite (pinLed1, LOW);
  65. digitalWrite (pinLed2, LOW);
  66. digitalWrite (pinLed3, LOW);
  67. counter = 0;
  68. }
  69. lastButt = buttDown;
  70.  
  71. }
  72.  
  73. boolean debounce(boolean last)
  74. {
  75. boolean current = digitalRead(pinButt);
  76. if (last != current)
  77. {
  78. delay(5);
  79. current = digitalRead(pinButt);
  80. }
  81. return current;
  82. }
Add Comment
Please, Sign In to add comment