Devices & Components
Arduino Uno Rev3
16x2 LCD display with I²C interface
10kohm Potentiometer
Breadboard
PIR Sensor
LED Light Bulb, Cool White
IR Remote
Grove - Buzzer - Piezo
IR receiver sensor
Software & Tools
Tinkercad
Project description
Code
Alarm with IR sensor code
cpp
Final
1#include <LiquidCrystal.h> 2#include <IRremote.h> 3 4 5// Map the IR code to the corresponding remote button. 6// The buttons are in this order on the remote: 7// 0 1 2 8// 4 5 6 9// 8 9 10 10// 12 13 14 11// 16 17 18 12// 20 21 22 13// 24 25 26 14 15//Pass to the alarm system 16const int PASS[3]={17,17,12}; 17//Keeping track of the most recent 3 inputs 18int userInput[3]; 19int userInputLength=3; 20int inputIndex=0; 21 22//Pin for IR sensor 23const int IR = 7; 24 25//Pin for PIR Sensor 26const int PIR = 6; 27 28//PIR sensor is disabled by default 29bool pirEnabled = false; 30 31//Input value from PIR sensor, HIGH == motion detected 32int pirState; 33 34//Pin for Piezo 35const int ALARM = 5; 36 37//Pin for lightbulb 38const int LIGHT = 4; 39 40//Pins for LCD screen 41const int RS = 8; 42const int E = 9; 43const int DB4 = 10; 44const int DB5 = 11; 45const int DB6 = 12; 46const int DB7 = 13; 47 48int button = 0; 49 50 51LiquidCrystal lcd(RS, E, DB4, DB5, DB6, DB7); 52void setup() 53{ 54 lcd.begin(16,2); 55 IrReceiver.begin(IR); 56 pinMode(ALARM, OUTPUT); 57 pinMode(LIGHT, OUTPUT); 58 pinMode(PIR, INPUT); 59 lcd.print("ALARM: OFF"); 60 61} 62 63void loop() 64{ 65 //Detects motion 66 pirState = digitalRead(PIR); 67 //Reads the IR and converts to int 68 button = readInfrared(); 69 if (pirEnabled&&pirState==HIGH){ 70 beep(600,100); 71 digitalWrite(LIGHT, HIGH); 72 delay(200); 73 } 74 else 75 digitalWrite(LIGHT,LOW); 76 if (button>0) 77 processCode(button); 78} 79 80int mapCodeToButton(unsigned long code) { 81 // For the remote used in the Tinkercad simulator, 82 // the buttons are encoded such that the hex code is of the format: 0xiivvBF00 83 // Where the vv is the button value, and ii is the bit-inverse of vv. 84 // For example, the power button is 0xFF00BF000 85 86 // Check for codes from this specific remote 87 if ((code & 0x0000FFFF) == 0x0000BF00) { 88 // No longer need the lower 16 bits. Shift the code by 16 89 // to make the rest easier. 90 code >>= 16; 91 // Check that the value and inverse bytes are complementary. 92 if (((code >> 8) ^ (code & 0x00FF)) == 0x00FF) { 93 return code & 0xFF; 94 } 95 } 96 return -1; 97} 98 99int readInfrared() { 100 int result = -1; 101 // Check if we've received a new code 102 if (IrReceiver.decode()) { 103 // Get the infrared code 104 unsigned long code = IrReceiver.decodedIRData.decodedRawData; 105 // Map it to a specific button on the remote 106 result = mapCodeToButton(code); 107 // Enable receiving of the next value 108 IrReceiver.resume(); 109 } 110 return result; 111} 112 113void processCode(int code) { 114 //Keeping track of the last userInputLength inputs 115 userInput[inputIndex] = code; 116 inputIndex++; 117 //Once we have userInputLength inputs, compare to password 118 if (inputIndex == userInputLength) { 119 if (checkPassword()) 120 onPasswordCorrect(); 121 else 122 onPasswordWrong(); 123 124 inputIndex = 0; // reset for next attempt 125 } 126} 127bool checkPassword() { 128 //Check two indexes of size 3 to compare password and userInput 129 for (int i = 0; i < userInputLength; i++) { 130 if (userInput[i] != PASS[i]) return false; 131 } 132 return true; 133} 134 135void onPasswordCorrect() { 136 lcd.setCursor(7,0); 137 //If Alarm is disabled turn off 138 if (pirEnabled){ 139 lcd.print("OFF"); 140 pirEnabled = false; 141 } 142 //If Alarm is not off, then turn on 143 else{ 144 lcd.print("ON "); 145 pirEnabled = true; 146 } 147 //Update LCD screen to inform of correct passcode 148 lcd.setCursor(0,1); 149 lcd.print("PASS ACCEPTED "); 150 delay(1500); 151 lcd.setCursor(0,1); 152 lcd.print(" "); 153 154} 155 156void onPasswordWrong() { 157 //Update LCD screen to inform of incorrect passcode 158 lcd.setCursor(0,1); 159 lcd.print("WRONG PASS "); 160 delay(1000); 161 lcd.setCursor(0,1); 162 lcd.print(" "); 163} 164 165//Function to make tone to avoid tone() fuction as it will conflict with the IRremote 166void beep(int freq, int duration) { 167 unsigned long start = millis(); 168 while(millis() - start < duration) { 169 digitalWrite(ALARM, HIGH); 170 delayMicroseconds(500000/freq); 171 digitalWrite(ALARM, LOW); 172 delayMicroseconds(500000/freq); 173 } 174}
Downloadable files
Electrical Design File
Final
Unit Deliverable.brd
Schematic View
Final
Unit Deliverable.pdf
Documentation
Full Wiring Picture
Picture of my project with all the wiring included
Unit Deliverable.png

Comments
Only logged in users can leave comments