Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. int switchPin = 7; // switch is connected to pin 2
  2. int led1Pin = 12;
  3. int led2pin = 11;
  4.  
  5. int val; // variable for reading the pin status
  6. int val2; // variable for reading the delayed status
  7. int buttonState; // variable to hold the button state
  8. int Mode = 0; // What mode is the light in?
  9.  
  10. void setup() {
  11. pinMode(switchPin, INPUT); // Set the switch pin as input
  12. pinMode(led1Pin, OUTPUT);
  13. pinMode(led2pin, OUTPUT);
  14. buttonState = digitalRead(switchPin); // read the initial state
  15. }
  16.  
  17. void loop(){
  18. val = digitalRead(switchPin); // read input value and store it in val
  19. delay(10); // 10 milliseconds is a good amount of time
  20. val2 = digitalRead(switchPin); // read the input again to check for bounces
  21. if (val == val2) { // make sure we got 2 consistant readings!
  22. if (val != buttonState) { // the button state has changed!
  23. if (val == LOW) { // check if the button is pressed
  24. if (Mode == 0) {
  25. Mode = 1;
  26. } else {
  27. if (Mode == 1) {
  28. Mode = 2;
  29. } else {
  30. if (Mode == 2) {
  31. Mode = 1;
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. buttonState = val; // save the new state in our variable
  39.  
  40.  
  41. // Now do whatever the lightMode indicates
  42. if (Mode == 0) { // all-off
  43. digitalWrite(led1Pin, LOW);
  44. digitalWrite(led2pin, LOW);
  45. }
  46.  
  47. if (Mode == 1) {
  48. digitalWrite(led1Pin, HIGH);
  49. digitalWrite(led2pin, LOW);
  50. }
  51.  
  52. if (Mode == 2) {
  53. digitalWrite(led1Pin, LOW);
  54. digitalWrite(led2pin, HIGH);
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement