Arduino Bluetooth Door Lock
SOURCE CODE:
//CREATED BY ROBOTIC NATION //https://www.youtube.com/channel/UCdNnHNkhaRYr-3nhQqY7_dw
#include <Servo.h> #define LED 13 //Define 13 pin for LED bool state = LOW; //The initial state of the function is defined as a low level char getstr; //Defines a function that receives the Bluetooth character int pos = 0; //create servo position variable Servo servo;
void setup() { pinMode(LED, OUTPUT); //set led to an output servo.attach(9); //attach servo to pin 9 Serial.begin(9600); //begin the serial monitor }
//Control LED sub function void stateChange() { state = !state; digitalWrite(LED, state); } //Tells servo to unlock void unlock(){ for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree servo.write(pos); // tell servo to go to position in variable 'pos' delay(15); digitalWrite(LED, HIGH); } } //Tells servo to lock void lock(){ for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees servo.write(pos); // tell servo to go to position in variable 'pos' delay(15); digitalWrite(LED, LOW); } } void loop() { //The Bluetooth serial port to receive the data in the function getstr = Serial.read(); if(getstr == 'a'){ stateChange(); } if(getstr == 'u'){ unlock(); Serial.write("Door is Unlocked"); } if(getstr == 'l'){ lock(); Serial.write("Door is Locked"); } }