Skip to content

Instantly share code, notes, and snippets.

@jonmarkgo
Created February 17, 2014 22:51
Show Gist options
  • Save jonmarkgo/9060847 to your computer and use it in GitHub Desktop.
Save jonmarkgo/9060847 to your computer and use it in GitHub Desktop.
#include <Servo.h>
int pirPin = 2; //digital 2
int servoPin = 12; //digital 12
int lock = 0;
int unlock = 180;
Servo myservo;
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
digitalWrite(pirPin, HIGH); //internal pull-up to activate PIR
myservo.attach(servoPin);
myservo.write(lock);
}
void loop(){
int pirVal = digitalRead(pirPin);
if(pirVal == LOW){ //was motion detected
Serial.println("Motion Detected");
if (myservo.read() >= 90) {
Serial.println("locking");
myservo.write(lock);
}
else {
Serial.println("unlocking");
myservo.write(unlock);
}
delay(3000); //pause to let the servo move
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment