Required component includes:
- An arduino board
- Piezo Sounder (or piezo disc)
- Light Dependent Resistor (LDR)
- 10k Ohms Resistor (accordingmay need different resister)
Figure from "Beginning Arduino" |
Code for this project:
// Light-detected Alarm int piezoPin = 8; // Piezo on Pin 8 int ldrPin = 0; // LDR on Analog Pin 0 int ldrValue = 0; // value read from the LDR void setup() { //nothing to do here } void loop() { ldrValue = analogRead(ldrPin); // read the value from the LDR tone(piezoPin, 1000); // play a 1kHz tone from the piezo delay(25); // wait a bit (still produce sound) noTone(piezoPin); // stop the tone delay(ldrValue); // wait the amount of millisecond in ldrValue }
In this code, the value of ldrValue depend on the voltage of Analog pin 0, which
is the same as the voltage of LDR (the darker the environment is, the higher the voltage of LDR). Therefore, the piezo sounder will sound quickly in light environ-ment.
No comments:
Post a Comment