Guest User

Untitled

a guest
Feb 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <SimpleTimer.h>
  2.  
  3. SimpleTimer timer;
  4.  
  5. int outPin = 9; // output pin (relay)
  6. int inPin = 8; // choose the input pin (for a pushbutton)
  7. int outVal = LOW;
  8. int val = 0; // variable for reading the pin status
  9. int prevVal = 0;
  10.  
  11. long TIMEOUT = 60000UL; // 1 minute
  12. //long TIMEOUT = 420000UL; // 7 minutes
  13.  
  14. void turnOn() {
  15. outVal = HIGH;
  16. digitalWrite(outPin, outVal);
  17. }
  18.  
  19. void turnOff() {
  20. outVal = LOW;
  21. digitalWrite(outPin, outVal);
  22. }
  23.  
  24. bool alreadyOn() {
  25. return outVal == HIGH;
  26. }
  27.  
  28. void setup() {
  29. pinMode(outPin, OUTPUT);
  30. pinMode(inPin, INPUT);
  31. turnOff();
  32. }
  33.  
  34. void loop() {
  35. prevVal = val;
  36. val = digitalRead(inPin);
  37. if (val == LOW && prevVal == HIGH) {
  38. if (alreadyOn()) {
  39. turnOff();
  40. } else {
  41. turnOn();
  42. timer.setTimeout(TIMEOUT, turnOff);
  43. }
  44. }
  45. timer.run();
  46. }
Add Comment
Please, Sign In to add comment