Ceci est une ancienne révision du document !
This month we’ll finish our first major Arduino contraption. It’s what I call the laser trip-wire. At the moment, we can arm and disarm the system, and our LDR can detect the loss of the laser. Let’s add to it with another component: the buzzer.
Enter the code to arm it and when the beam is broken the buzzer will sound until either the beam is restored, or the code is entered to disarm it.
But first, 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 added some code to set things up:
const int buzzer = A2; buzzer is on pin A2 int notes[]={ possible notes to play
NOTE_A4, NOTE_B4, NOTE_C3 };
and:
pinMode (buzzer, OUTPUT); the buzzer analogWrite(armedLed, 0); start as off
I also added a new IF statement to check (via the LDR) if the laser is on/off:
if (sensorHit < 700 && armed == 1){ beep when armed & beam broken tone(buzzer,notes[3],200); beam broken play note 3
}
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[1],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. Are there any similar projects you’d like me to try? Drop me an email at: ronnie@fullcirclemagazine.org. Remember: I’m a beginner, don’t ask me to design the next NASA rover! Full code is at: http://pastebin.com/yVeZuAY2 With a demonstration of the system at: https://www.youtube.com/watch?v=efA9lwmE5zA