Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 31st, 2013  |  syntax: C++  |  size: 1.86 KB  |  views: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. const int button = 4;         // connected to trigger button
  3. const int doorSensor = 5;     // connected to door sensor switch (HIGH is closed, LOW is open)
  4. const int doorLock =  6;      // connected to door lock relay trigger input (HIGH is unlocked, LOW is locked)
  5.  
  6. int buttonState = 0;          // variable for reading the pushbutton status
  7. int doorState = 0;            // variable for reading the door status (open/closed)
  8.  
  9. void setup(){
  10.   pinMode (doorSensor, INPUT);     // set pin to input
  11.   pinMode (button, INPUT);         // set pin to input    
  12.   pinMode (doorLock, OUTPUT);      // set pin to output
  13. }
  14.  
  15. void loop(){
  16.   doorState = digitalRead(doorSensor);    // read state of door sensor
  17.   buttonState = digitalRead(button);      // read state of button
  18.    
  19.   if (buttonState == HIGH) {              // if button is pressed
  20.     unlockDoor();                         // perform unlock function    
  21.   }else{                                  // if button is not pressed
  22.     digitalWrite(doorLock, LOW);          // keep door locked
  23.   }
  24. }
  25.  
  26. void unlockDoor(){  
  27.   digitalWrite(doorLock,HIGH);                // unlock door
  28.   delay(2000);                                // wait 2 seconds
  29.    
  30.   if (doorState == LOW){                      // if door is open after 2 second delay
  31.  
  32.     do {
  33.        doorState = digitalRead(doorSensor);   // Re-check sensor
  34.        digitalWrite(doorLock,HIGH);           // keep door unlocked
  35.     }while (doorState == LOW);                // while door is open
  36.    
  37.                                               // when doorState == HIGH
  38.     delay(500);                               // wait 1/2 second
  39.     digitalWrite(doorLock, LOW);              // lock door
  40.    
  41.   }else if (doorState == HIGH){               // if door is closed after 2 second delay
  42.     digitalWrite(doorLock,LOW);               // lock door
  43.   }
  44. }