Advertisement
kowalyshyn_o

LightSwitch

Apr 10th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo myServo;
  4. const int topSwitch = 2;
  5. const int btmSwitch = 8;
  6. const int servoPin = 12;
  7.  
  8. int topSwitchPinState = HIGH;
  9. int btmSwitchPinState = HIGH;
  10.  
  11. int prevtopSwitchState = HIGH;
  12. int prevbtmSwitchState = HIGH;
  13.  
  14. void setup() {
  15.   Serial.begin(9600);
  16.   myServo.attach(servoPin);
  17.   myServo.write(0);
  18.  
  19.   pinMode(topSwitch,INPUT_PULLUP);
  20.   pinMode(btmSwitch,INPUT_PULLUP);
  21. }
  22.  
  23. void loop() {
  24.  
  25.   delay(10);
  26.  
  27.   if (digitalRead(topSwitch) == LOW) {
  28.     Serial.println("Top Button Pushed");
  29.     unsigned long startTime = millis();
  30.     unsigned long endTime = startTime + 5000;   // five extra seconds
  31.     Serial.println("Start time: "+String(startTime));
  32.     Serial.println("End time: "+String(endTime));
  33.  
  34.     while (1) {
  35.      
  36.        delay(10);
  37.      
  38.        if (millis() >= endTime) {
  39.         myServo.write(90);
  40.         Serial.println("Out of Time: Reset Switch");
  41.         break;
  42.        }
  43.      
  44.       if (digitalRead(btmSwitch) == LOW) {
  45.         myServo.write(90);
  46.         Serial.println("Bottom Switch Pressed: Reset");
  47.         break;  
  48.       }
  49.      
  50.     } // end while
  51.   } // end if
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement