Transcript
Aha, gotcha! Now you get to fight evil forces with
Arduino. Time to put a stop to all those nasty thiefs with a burglar alarm.
The device is made with familiar components. There is an LDR, a piezo and an LED. I'll show you the basic setup and if you have the time, you can add
more features and also figure out how to hide the alarm. Let's check out the code
quickly. You will write most of the code yourself in this exercise. First you need
to create a variable to store the sensor values. Remember the semicolons. Then you
move on to the setup part. Looks like you need to use the Serial.begin command.
Here you have to set the pin mode for the piezo and the LED pins. The loop part
looks like this. First you check the sensor data with the analogRead command,
and store the value to the variable. And here you'll print the value to the
serial monitor so you can check what it is. The if statement comes next.
Put everything you want to happen when the alarm goes off between the curly
brackets. Play a note with the tone command and turn the light on. Use the analogWrite command with the LED. Just give it a good value between 0 and 255. After the
delay comes the second part of this alarm. Here is a new note and the light
goes dim. One more delay to the end. These are just suggestions of course Make the
sounds and light effects you like the best. At this point you should test how
the alarm works - and maybe it doesn't. Fixing it is easy. Open the serial
monitor, look at the sensor values and change the condition of the if statement
so it works like it should. You also need to make sure the LED is
always off then the valuable treasure is on the LDR sensor. That you do on the
last line here, outside the if statement.