Advertisement
digitalfx

homecontro.pde

Mar 8th, 2011
2,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. /*
  2. serial recieve 1 - toggle garage door
  3. serial recieve 2 - turn lights on
  4. serial recieve 3 - turn lights off
  5. serial recieve 4 - toggle lights
  6. serial send 0 - garage door open
  7. serial send 1 - garage door closed
  8. serial send 2 - lights on
  9. serial send 3 - lights off
  10. */
  11. int garagePin = 3; //pin tied to garage door
  12. int lightPin = 4; //pin for on/off
  13. int statusPin = 8; //input from garage door
  14. int inputNumber = 0; //declare for serial input
  15. int buttonState = 0; //variable for button state
  16. int buttonDelay = 0; //variable for debounce of button
  17. int oldbuttonState = 0; //variable for old button state
  18. int lightState = 0; //set the state of the lights to off
  19.  
  20. //setup pin mode and serial data
  21. void setup() {
  22.     pinMode(garagePin, OUTPUT);
  23.     pinMode(lightPin, OUTPUT);
  24.     pinMode(statusPin, INPUT);  
  25.     Serial.begin(9600);                       //set 9600 baud
  26. }
  27. void loop() {                                 //just loop forever
  28.     if (Serial.available() > 0) {             // look for any number greater than 0
  29.         inputNumber = Serial.read();          //store that number
  30.     }
  31.  
  32.     if (inputNumber == 1) {                   // toggle the garage door
  33.         digitalWrite(garagePin, HIGH);
  34.         delay(1000);                          //delay to make sure garage door opener sees the change
  35.         digitalWrite(garagePin, LOW);
  36.         inputNumber = 0;
  37.     }
  38.  
  39.     if (inputNumber == 2) {                   //turn on the lights
  40.         digitalWrite(lightPin, HIGH);
  41.         inputNumber = 0;
  42.     }
  43.    
  44.      if (inputNumber == 3) {                  //turn off the lights
  45.         digitalWrite(lightPin, LOW);
  46.         inputNumber = 0;
  47.     }
  48.      
  49.      if (inputNumber == 4) {                 //toggle lights on or off depending on current state and send change via serial
  50.        lightState = digitalRead(lightPin);
  51.        if (lightState == LOW) {
  52.          digitalWrite(lightPin, HIGH);      
  53.          Serial.println("2");
  54.          inputNumber = 0;
  55.        }
  56.         else {
  57.          digitalWrite(lightPin, LOW);
  58.          Serial.println("3");
  59.          inputNumber = 0;
  60.         }
  61.       }
  62.  
  63. //code for the status of the garage door
  64.  
  65.     buttonState = digitalRead(statusPin);     //read status of button
  66.     delay(10);                                //10 mils wait
  67.     buttonDelay = digitalRead(statusPin);     //read it again
  68.     if (buttonState == buttonDelay) {         //make sure they are the same (debounce)
  69.       if (buttonState != oldbuttonState) {    //compare old button state to new to avoid flooding serial port with useless data
  70.        if (buttonState == HIGH) {
  71.          Serial.println("0");                 //send a 0 via serial to computer to indicate the garage door is open
  72.        }
  73.        else {
  74.          Serial.println("1");                 //send a 1 via serial to computer to indicate the garage door is closed
  75.        }
  76.       }
  77.      oldbuttonState = buttonState;            //save button state for the next loop
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement