I based this on the Alarm System Example from the Franzis Tutorial Kit for the Arduino. The example as described, with the potentiometer to control the threshold before setting off the alarm didn't work for me. So I simplified it to remove the variable resistor used to control the sensitivity.
Here is a basic schematic of the circuit:
And in action:
In detail - I shine the Laser on the Light Dependent Resistor when starting up the Arduino. The value is read off analog port 0. If this changes by more than the threshold (Santa puts his hand in the beam!) which is set to 100 then the alarm sounds and lasts for 2 and a half seconds.
You can comment out the line delay(2500); to stop the alarm sounding immediately when you remove your (or Santa's) hand.
The Serial.Print lines were used for debugging and aren't necessary. Note also that the baseValue is reset every 10s (1000 * 10ms). This is intended to account for changes in ambient light. However in practice using the Laser makes this effectively redundant since it dominates over the background.
The code for the Arduino is given below:
//
Light Alarm
int
LED = 13;
int
LDR = 0;
int
Speaker = 8;
int
cnt = 0;
int baseValue,threshold = 100;
void
setup()
{
Serial.begin(9600);
pinMode(LED,OUTPUT);
pinMode(Speaker,OUTPUT);
baseValue = analogRead(LDR);
}
void
loop()
{
cnt++;
if(cnt > 1000)
{
// Reset the base value of the LDR after a
period of time
cnt = 0;
baseValue = analogRead(LDR);
}
// Outside the bounds, sound the alarm
if (baseValue > (analogRead(LDR)+threshold) || baseValue < (analogRead(LDR) - threshold))
{
digitalWrite(LED,HIGH); // Use the onboard LED for visual indication
tone(Speaker,500);
delay(2500);
noTone(Speaker);
digitalWrite(LED,LOW);
}
Serial.print(baseValue);
Serial.print(",");
Serial.print(threshold);
Serial.print(", ");
Serial.println(analogRead(LDR));
delay(10);
}
No comments:
New comments are not allowed.