/* Arduino DueMilanove micro controller "sketch"/program * * YoungestSisterSketch1 * * LIGHT DETECTOR CONTROLLING THE TONE GENERATED BY A PIEZO ELECTRIC "BUZZER" * * As the light reaching the light detector INCREASES, * the tone generate by the "buzzer" goes DOWN * * As the light reaching the light detector DECREASES. * the tone generated by the "buzzer" goes UP * * Parts List: * * Arduino Duemilanove nicro controller (means 2009 in English) * USB cable to connect your computer to the Arduino's USB port * 10K ohm 1/4 watt Resistor * 0-20K ohm Light Detecting Resistor (LED) * Piezo electric "buzzer" 3-16V DC (Radio Shack Part No. 273-074) * 30 rows of pins Breadboard * jumper wires * * What Connects To What aka Schematic DIAGRAM * * GND<----------. * | * (R) 10K ohms * Analog | * )<-------------. [ VOLTAGE DIVIDER ] * Pin 1 | * (0-1023) | * (LDR) 0-20K ohms * | * | * +5V .--------->. * * GND<-------------. * | * :-----. * / \ * | Piezo | * | Buzzer | * \ / * +----. * PWM | * )---------------->. Positive leg of Piezo buzzer * Digital Pin 3 * (0-254) * * Digital Pin 2 * )---------------| 220 ohm|------. * .---+----. * | LED | * .--------. * | * GND-----------------------------. */ int LDR1Pin = 1; // identifies the variable "photosensorPin" as an INTeger variable // and sets its value to zero (0) int BWLED1Pin = 2; int SPKR1Pin = 3; // identifies the variable "piezPin" as an INTeger variable // and sets its value initially 13 // Digital Pin 3 on the Arduino is a Pulse Width Modulation (PWM) pin int LDR1val = 0; // identifies the variable "val" as an INTeger variable // it will be used to hold voltage readings coming from // the Voltage Divider and then divided by 2 to provide // an "in range" output to the piezo electric buzzer/speaker int LDR1sensorVal = 0; // identifies the variable "sensorVal" as an integer variable // It holds the raw Voltage Bridge output that is being // determined by the Light Detecting Resistor (LDR) which // has NO resistance under bright light and 20K ohms when no // light is falling on it // The following variables can be used to change // - the range of the tones generated by the speaker // - the interval between tones // - the frequency of the tones int LDR1sensorLim = 450; // limiter on how much of Voltage Divider's value goes to piezo/speaker // theoretical range is 0 - 1023 int LDR1SensorLimLow = 150; // limits the lowest voltage that can go to the piezo/speaker int LDR1cyclesAdj = 225; // adjust cycles /tone. Recomnd 250 int LDR1delayAdj = 10; // adjust delay between tones // Recommended value -10 int LDR1sensorDivisor = 2.5 ; // divisor when trimming sensor's raw output value down to buzzer range // Value must be in the 1.0 to 4.0 range, with 2.5 recommended // The smaller this number the LOWER the pitch and the LONGER // the pause between the speaker's output // BEGIN setting up initial conditions void setup() { pinMode(SPKR1Pin, OUTPUT); // sets Digital Pin 3, a PWM pin, to an OUTPUT pin // Pin 3's output will be the input to the piezo buzzer // range for PWM pins is 0-254 pinMode(LDR1Pin, INPUT); // sets Analog Pin 0 to an INTPUT pin // Pin 1 will input values FROM voltage divider // TO the Arduino pinMode(BWLED1Pin,OUTPUT); // Serial.begin(9600); // Set up serial communication between your computer // and the Arduino's USB port to 9600bps. // Used during testing. Can be deleted. } // Begin to have the LDR detect the amount of light falling on it // and send "tones" to the piezo buzzer that correspond to the // amount of light the LDR "sees" // void loop() { // ----------------------------->the top of the "outer loop" below digitalWrite(BWLED1Pin,HIGH); ?? turn LED ON delay(50); // keep it on for 50/1000 of a second digitalWrite(SPKR1Pin, LOW); // sets the piezPin OFF/LOW LDR1val = analogRead(LDR1Pin); // reads Pin 1's analog value from photosensor LDR1sensorVal = LDR1val; // Variable "val" is first used as the photosensor's output value // and then halved to get it in a range if(LDR1sensorVal>LDR1sensorLim) { digitalWrite(SPKR1Pin, LOW); delay(2500); digitalWrite(BWLED1Pin,LOW); } if(LDR1sensorVal