For this dandy little project you'll need:
- Arduino - I'm using an Arduino Uno
- a 3 x 4 keypad
- a 16 x 2 LCD screen
- a laser module/sensor
- a light dependent resistor (LDR)
- a buzzer
- 3 x 220 ohm resistors
- 1 x 10K resistor
- 1 x 10k potentiometer (optional, it's to adjust the LCD screen brightness)
- an LED
- and a ton of wires!
Step 1: LCD + Keypad
So, first thing I did was to get the LCD and keypad working together.
For this I pretended that it was some sort of arm/disarm (or entry/exit) thing.
My code for this part is at: http://pastebin.com/YndLneqm.
Getting the LCD wired up was tricky as most wiring diagrams for it don't show the last two pins wired up and these are required for the back light. Check my snazzy Fritzing wiring diagram to see how I wired up my LCD screen, pot (for adjusting brightness) and keypad. The pins for them all are also mentioned in my code.
NOTE: pin 1 isn't used as whenever I wired something to pin 1 I experienced weirdness. Not sure why. I expect it's something to do with the pins data uses?
If you don't have the keypad.h library you'll need to grab it from here: http://playground.arduino.cc/code/Keypad
Step 2: What's the Password?
I thought it’d be nice if the Arduino could take the keypad input and check it against a known password. Well, there just happens to be a handy dandy library called Password. You can grab it from: http://playground.arduino.cc/Code/Password.
Merging the code wasn’t going so well for me until I found this example: https://sites.google.com/site/arduinomega2560projects/home/level-3/keymembrane-and-lcd-password-lock This is pretty much what I was wanting to achieve but with my 3 x 4 keypad.
I got stumped with why it wouldn’t accept my password. After much trial and error I found that in the line:
case ' ': guessPassword(); break;
the “case ‘ ‘“ statement is effectively the ‘enter’ key for the password. So, changing the ‘ ‘ to ‘#’ worked a treat. After that I tidied up the before and after screens.
Now it says ‘Enter code:’. You enter a four digit code and press the hash key (#) to finish. The LCD screen will either say ‘INVALID PASSWORD’, pause, and return to the ‘Enter code:’ screen, or it will say ‘VALID PASSWORD’, pause, display ‘Welcome!’, pause, then return to the ‘Enter code:’ screen.
My code thus far is at: http://pastebin.com/V0f9GBFf.
Step 3: IF/ELSE ARMED/DISARMED
I’ve added to it using a simple IF/ELSE statement to do one of two things.
I’ve added an LED and an integer called ‘armed’. Initially ‘armed’ equals zero, and 'armedLed' equals A0. I’m now having to tweak the rules and use the analog pins for digital components as I’m quickly running out of pins!
Basically here’s what’s happening: if the password is correct and armed=0 (in other words: the system is off) then it will clear the screen, display ARMED!, change armed to equal 1 and light the red LED. Finally it will display “Code to disarm:”. Otherwise, armed must equal 1 (system is on) so display DISARMED!, make armed=0 and display “Code to arm:”.
After getting success with armed/disarmed I went and dug out a laser sensor from my cheapo 37-in-1 sensor box and a photoresistor (a Light Dependent Resistor, LDR for short). The LDR goes to A1 with a 220 ohm resistor with one leg and taking its other leg to ground. The laser sensor goes to the positive LED leg and to ground. Now, when the system is armed the laser flicks on and is made to point at the LDR.
To test that the LDR is actually registering anything I’ve introduced the serial code (using 9600 baud rate). In the main loop I have the code do an analog read from pin A1 and print the result to the IDE serial window. In other words, the number that the LDR is returning via it’s analog pin. With no laser and at room lighting the serial window shows a stream of numbers averaging 400-450. When I cover the LDR it drops to as low as 100-150. With the laser on, and pointing directly at the LDR, the serial output shoots up to a touch over 1,000. Almost the maximum it can reach. So, with the laser on I place an obstruction to the beam and the LDR registers the loss of the laser.
Source Code: http://pastebin.com/AjFDajHm
Step 4: The Great Finale!
We need the buzzer to play something and this is where we add another library called ‘pitches’. Rather than creating a new file and pasting in stuff I discovered a new way of adding a library. Click the little down arrow at the top right of the IDE window and choose to create a new tab. Paste your library text in there and voila! You’re done.
I acquired this ‘pitches’ text, and new tab idea, from: http://arduino.cc/en/Tutorial/Tone
I found (via the serial window) that the LDR registers about 600 when the LDR is not being hit by the laser. So, in my IF statement and in the main loop, I’m saying ‘if the system is armed and the LDR is registering less than 700 the laser must be broken so sound tone 3 via the buzzer’. I also added to the ‘wrong guess’ code:
tone(buzzer,notes,200); //boop! wrong code.
which just plays tone 1, a ‘boop’ noise, to let you know you got the code wrong.
You could of course use a repeating while statement to play several tones to have a more elaborate alarm/jingle.
Full code is at: http://pastebin.com/yVeZuAY2 With a demonstration of the system at: https://www.youtube.com/watch?feature=player_embedded&v=efA9lwmE5zA.