Description:
In this lab, I decided to create a burglar alarm using an FSR, lights and the peizzo. I placed my wallet on the FSR and if someone tried to lift it, the alarm would go off with sound and lights. I decided to include lights as well to grab my attention in a noisy place.
Components:
1 Piezo
1 Arduino Uno Board
1 Bread board
1 FSR
Connecting wires
1 220 Ohm resistor
1 10K Ohm resistor
3 LEDs
Code:
/* Burglar Alarm
* -----------
*
* Program to set off the burglar alarm when an object over the FSR is displaced or tried to be displaced
* Modified by Priya Iyer on 10/15/2013 for creating a burglar alarm using an FSR
*/
int ledPin1 = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int fsrPin=0;
int speakerOut = 7;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
int fsrVal=10;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(speakerOut, OUTPUT);
}
void loop() {
fsrVal = analogRead(fsrPin);
if(fsrVal==0){
digitalWrite(speakerOut, LOW);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin1, statePin);
digitalWrite(ledPin2, statePin);
digitalWrite(ledPin3, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}
}
- Login to post comments