Hardware components | ||||||
| × | 2 | ||||
| × | 1 | ||||
| × | 9 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
| ||||||
Hand tools and fabrication machines | ||||||
|
How many times have you gotten to class or work and suddenly could not remember whether or not you locked your door at home before you left? It can lead to needless stress and anxiety all day hoping that your home is secured from strangers with bad intentions. We decided to automate a deadbolt lock so that it could be remotely activated from your phone. with this being said we realized that people also need to be able to lock and unlock there door when they approach it. This lead to the addition of push buttons in order to engage or disengage the lock. If you are unsure as to whether or not the door is locked there are indicator lights on the device to alert you one way or another as to the status of your lock.
Lock and Unlock
C/C++This is the code used to activate the servo motor to engage or disengage the deadbolt lock. This code also allows the photon to communicate with another photon so that they are both on the same page and understand where the lock is at.
int valLock = D0;
Servo serv;
//int lock;
int nLock;
int boardLed;
int valUnlock = 90;
int button = D1;
void setup() {
pinMode(boardLed,OUTPUT);
pinMode(valLock,OUTPUT);
pinMode(button, INPUT_PULLUP);
}
void loop() {
//Lock function
{int lock(String) ;
//Moves servo to lock position
serv.write(valLock);
//Lock number changes to 1
nLock = 1;
//Publishes "Locked"
Particle.publish("Reese_door_lock","Locked");
//Switches the external LED off
digitalWrite(boardLed,LOW);
}
}
void cycle(){
//Unlock function
{int unlock(String) ;
//Moves servo to unlock position
serv.write(valUnlock);
//Lock number changes to 0
nLock = 0;
//Publishes "Unlocked"
Particle.publish("Reese_door_lock","Unlocked");
//Switches the board LED on
digitalWrite(boardLed,HIGH);
}
//If the button is pressed, the locked switches between locked and unlocked
if (analogRead(button)<100) {
//Lock sequence
if (nLock==0) {
//Moves servo to locked position
serv.write(valLock);
//Changes lock number to 1
nLock = 1;
//Publishes "Locked"
Particle.publish("Reese_door_lock","Locked");
//Switches the board LED off
digitalWrite(boardLed,LOW);
delay(1000);
}
//Unlock sequence
else if (nLock==1) {
//Moves servo to unocked position
serv.write(valUnlock);
//Changes lock number to 0
nLock = 0;
//Publishes "Unlocked"
Particle.publish("Reese_door_lock","Unlocked");
//Switches the board LED on
digitalWrite(boardLed,HIGH);
delay(1000);
}
}}
void myHandler(const char *event, const char *data) {
//When the "Lock" event data is published with the sucribed with "rosco_door_lock", the lock switches bewtween lock and unlock
if (strcmp(data,"Lock")==0) {
//Lock sequence
if (nLock==0) {
//Moves servo to lock position
serv.write(valLock);
//Changes lock number to 1
nLock = 1;
//Publishes "Locked"
Particle.publish("Reese_door_lock","Locked");
//Switches board LED off
digitalWrite(boardLed,LOW);
}
//Unlock sequence
else if (nLock==1) {
//Moves servo to unlock position
serv.write(valUnlock);
//Changes lock number to 0
nLock = 0;
//Publishes "Unlocked"
Particle.publish("Reese_door_lock","Unlocked");
//Switches the board LED on
digitalWrite(boardLed,HIGH);
}
}
}
Controller
C/C++This code takes works with communication from the other photon to activate the correct corresponding LED indicator light, as well as communicating with the servo based photon to lock or unlock when commanded via a push button or an application.
//Power to switch is on pin A0
int power = A0;
//Button on pin A1
int button = A1;
//Lock LED is on pin D0
int ledLock = D0;
//Unlock LED is on pin D1
int ledUnlock = D1;
//Board LED is on pin D7
int boardLed = D7;
//Assigns variables to lock(1) or unlock(0)
int nLock = 0;
void setup() {
//Describes outputs and input of pins
pinMode(power,OUTPUT);
pinMode(button,INPUT);
pinMode(boardLed,OUTPUT);
pinMode(ledLock,OUTPUT);
pinMode(ledUnlock,OUTPUT);
//Suscribes to other photon
Particle.subscribe("Reese_door_lock", myHandler);
//Powers the pin connected to the switch
analogWrite(power,4095);
//both LEDs turn on when switch is powered
digitalWrite(ledLock,HIGH);
digitalWrite(ledUnlock,HIGH);
//Flashes board LED when setup is complete
digitalWrite(boardLed,HIGH);
delay(300);
digitalWrite(boardLed,LOW);
delay(300);
digitalWrite(boardLed,HIGH);
delay(300);
digitalWrite(boardLed,LOW);
delay(300);
digitalWrite(boardLed,HIGH);
delay(300);
digitalWrite(boardLed,LOW);
//Lock LED turns off to leave the Unlock LED on
digitalWrite(ledLock,LOW);
}
void loop() {
//If the button is pressed, "Lock" is published and the board LED flashes 3 times
if (analogRead(button)<100) {
Particle.publish("rosco_door_lock","Lock");
digitalWrite(boardLed,HIGH);
delay(100);
digitalWrite(boardLed,LOW);
delay(100);
digitalWrite(boardLed,HIGH);
delay(100);
digitalWrite(boardLed,LOW);
delay(100);
digitalWrite(boardLed,HIGH);
delay(100);
digitalWrite(boardLed,LOW);
delay(1000);
}
}
void myHandler(const char *event, const char *data) {
//When "Unlocked" is recieved from "mbenne_door_lock", the unlocked LED turns on and the locked LED turns off
if (strcmp(data,"Locked")==0) {
digitalWrite(ledUnlock,LOW);
digitalWrite(ledLock,HIGH);
}
//When "Locked" is recieved from "mbenne_door_lock", the locked LED turns on and the unlocked LED turns off
else if(strcmp(data,"Unlocked")==0) {
digitalWrite(ledLock,LOW);
digitalWrite(ledUnlock,HIGH);
}
}
Comments