Advertisement
manvi_m

LED stays on until button is pressed again works

Jun 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. /*
  2. * Manvi Mittal
  3. * Dice Prototype
  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;
  11. boolean ledOn = 0;
  12. boolean prevButt = 0;
  13.  
  14. void setup()
  15. {
  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. buttPressed = digitalRead (pinButt);
  38. //Serial.print (buttPressed);
  39.  
  40. buttPressed = debounce (prevButt);
  41. if (prevButt == LOW && buttPressed == HIGH)
  42. {
  43. ledOn = !ledOn;
  44. Serial.print (ledOn);
  45. }
  46. digitalWrite (pinLed1, ledOn);
  47. digitalWrite (pinLed2, ledOn);
  48. digitalWrite (pinLed3, ledOn);
  49.  
  50. prevButt = buttPressed;
  51. }
  52.  
  53.  
  54.  
  55. boolean debounce(boolean last)
  56. {
  57. boolean current = digitalRead(pinButt);
  58. if (last != current)
  59. {
  60. delay(5);
  61. current = digitalRead(pinButt);
  62. }
  63. return current;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement