Devices & Components
Breadboard 150x90
Arduino Uno Rev3
Box 525 1% precision resistors - 17 values
Grove - PIR Motion Sensor
Transistor NPN (BC547 or similar)
Piezo speaker
Micro servo SG90
RGB led 8mm
DC Motor, 12 V
push buttons
Software & Tools
tinkercad
Project description
Code
Project code
cpp
1//this arduino with serve as a home detection device 2//it will unlock if it detects motion 3//when motion is sensed, rgb light will turn green 4//when there is no motion, rgb light will turn purple 5//dc motor will spin when motion is detected 6//dc motor will not spin if there isn't any motion detected 7//there will be an emergency button and it will 8//play an alarm and turn rgb red 9#include <LiquidCrystal.h> 10#include <Servo.h> 11 12//lcd 13const int RS = 2; 14const int E = 3; 15const int DB4 = 4; 16const int DB5 = 5; 17const int DB6 = 6; 18const int DB7 = 7; 19const int RED_LED = 9; 20const int GREEN_LED = 10; 21const int BLUE_LED = 11; 22 23LiquidCrystal lcd(RS, E, DB4, DB5, DB6, DB7); 24 25//PIR sensor 26const int PIR = 8; 27int pirState; 28//Servo 29const int SERVO_PIN = A0; 30Servo servo; 31const int LAG = 200; 32int doorState = 1; 33//DC motor 34const int DC_MOTOR = 12; 35const int OFF = 0; 36const int ON = 255; 37//Piezo 38const int ALARM = 13; 39//button 40const int BUTTON = A1; 41void updateDoorState() { 42 //First row (row 0) of lcd displays "CODE:" 43 lcd.setCursor(0, 0); 44 lcd.print("Motion:"); 45 46 //Second row (row 1) of LCD displays "DOOR:" 47 //with the state of the lock. 48 //When locked, servo move to 0 degrees 49 lcd.setCursor(0, 1); 50 lcd.print("DOOR: "); 51 if (pirState == LOW) { 52 servo.write(0); 53 delay(LAG); 54 lcd.print("LOCKED "); 55 } 56 else { 57 servo.write(90); 58 delay(LAG); 59 lcd.print("UNLOCKED"); 60 } 61 62} 63 64 65void setup() 66{ 67 Serial.begin(9600); 68 lcd.begin(16,2); 69 pinMode(RED_LED, OUTPUT); 70 pinMode(GREEN_LED, OUTPUT); 71 pinMode(BLUE_LED, OUTPUT); 72 pinMode(PIR, INPUT); 73 pinMode(DC_MOTOR, OUTPUT); 74 pinMode(ALARM, OUTPUT); 75 pinMode(BUTTON, INPUT); 76 servo.attach(SERVO_PIN); 77 pirState = LOW; 78 updateDoorState(); 79 80 81} 82 83void loop() 84{ 85 lcd.setCursor(7, 0); 86 pirState = digitalRead(PIR); 87 if (pirState == HIGH) { 88 lcd.print("Sensed "); 89 displayColor(0, 255, 0); 90 analogWrite(DC_MOTOR, ON); 91 92 } 93 else{ 94 lcd.print("none "); 95 displayColor(255, 0, 255); 96 analogWrite(DC_MOTOR, OFF); 97 } 98 99 //lcd.print(""); 100 //delay(1000); 101 updateDoorState(); 102 delay(1000); 103 //if button is pressed, noise will play to alert people 104 //in case of an emergency 105 if(digitalRead(BUTTON)){ 106 lcd.clear(); 107 lcd.setCursor(0,0); 108 lcd.print("Emergency Alert"); 109 tone(ALARM, 2700, 2000); 110 displayColor(255, 0, 0); 111 delay(2000); 112 } 113 114 115} 116 117//function for red, green, and blue colors 118void displayColor(int r, int b, int g) 119{ 120 analogWrite(RED_LED, r); 121 analogWrite(GREEN_LED, g); 122 analogWrite(BLUE_LED, b); 123 124}
Comments
Only logged in users can leave comments