/*
/* Arduino DueMilanove micro controller "sketch"/program
*
* YoungestSisterSketch6
*
* 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 micro 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.
*
* GND. Positive leg of Piezo buzzer
* Digital Pin 7
* (0-254)
*
* Digital Pin 13
* )---------------| 220 ohm|-----(+)
* .---+----.
* | LED |
* .--------.
* |
* GND--------------------------(-)
Blink without Delay routine combined wit Youngest Sister LDR:Speaker Code
LED routine runs in background - without affecting timing of Tone Generation
LDR sends to speaker and speaker generates a tone specific to the LDR pins
output
Turns on and off a light emitting diode(LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.
The circuit:
* LED attached from pin 13 to ground.
* Note: on most Arduinos, there is already an LED on the board|
that's attached to pin 13, so no hardware is needed for this example.
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
//
// Youngest Sister Specific Stuff
//
int LDR1Pin = 1; // identifies the variable "photosensorPin" as an INTeger variable
// and sets its value to zero (0)
int SPKR1Pin = 7; // identifies the variable "piezPin" as an INTeger variable
// and sets its valu einitially to seveb (7)
// Digital Pin 7
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 LDR1sensorLimHIGH = 900; // limiter on how much of Voltage Divider's value goes to piezo/speaker
// theoretical range is 0 - 1023
int LDR1SensorLimLow = 60; // limits the lowest voltage that can go to the piezo/speaker 60 recomended
int LDR1cyclesAdj = 200; // adjust cycles /tone. Recomened 250
int LDR1delayAdj = +10; // adjust delay between tones
// Recommended value -10
int LDR1sensorDivisor = 4 ; // divisor when trimming sensor's raw output value down for buzzer range
// Value must be in the 1.0 to 4.0 range, with 2.8 recomended
// The smaller this number the LOWER the pitch and the LONGER
// the pause between the speaker's output
//
// ================== END Youngest Sister Specific Stuff ==============================
//
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 10000; // interval at which to blink (milliseconds)
void setup()
{
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
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
Serial.begin(9600); // Set up serial communication between your computer
// and the Arduino's USB port to 9600bps.
// Used during testing. Can be deleted.
}
void loop()
{
// ============== here is where you'd put code that needs to be running all the time========
// in this case it's keeping the 7 color LED running for 15 seconds ON, 15 seconds off
//
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
//
// Yougest Sister LDR monitoring and tone generated by speaker code
//
LDR1val = analogRead(LDR1Pin); // reads Pin 1's analog value from photosensor
// Serial.println(LDR1val);
LDR1sensorVal = LDR1val; // Variable "val" is first used as the photosensor's output value
// and then halved to get it in a range
if(LDR1sensorVal > LDR1sensorLimHIGH)
{ digitalWrite(SPKR1Pin, LOW); delay(1000); }
if(LDR1sensorVal < LDR1SensorLimLow)
{ digitalWrite(SPKR1Pin, LOW); delay(1000); }
LDR1val = LDR1val / LDR1sensorDivisor; // halves the photosensor's value Pin 0 assigned it
for( int LDR1i=0; LDR1i < LDR1cyclesAdj; LDR1i++)
{
// play it for varying cycles
digitalWrite(SPKR1Pin, HIGH);
delayMicroseconds(LDR1val-LDR1delayAdj);
digitalWrite(SPKR1Pin, LOW);
delayMicroseconds(LDR1val-LDR1delayAdj);
}
}