/* Nursery Rhyme Hat Liz Huessy March 2014 */ int ledPin = A3; // The white LED pin is connected to pin A3 int redPin = 10; // The red LED pin is connected to pin 10 int bluePin = 9; // The blue LED pin is connected to pin 9 int greenPin = 6;// The green LED pin is connected to pin 6 int ground = 11; // Pin 11 is the ground pin int speakerPin = A2; // The speaker is connected to digital pin A2 int sensorPin = A4; // The light sensor is connected to pin A4 int sensorValue; // Variable to store the value coming from the sensor void setup() { pinMode(speakerPin, OUTPUT); // Sets this pin to be an output pinMode(A3, OUTPUT); // Sets this pin to be an output pinMode(10, OUTPUT); // Sets this pin to be an output pinMode(9, OUTPUT); // Sets this pin to be an output pinMode(6, OUTPUT); // Sets this pin to be an output pinMode(sensorPin, INPUT); // pinMode(ground, OUTPUT); digitalWrite(ground, LOW); // Turns off power to pin 11 (ground) } void loop() // Runs on a loop { sensorValue = analogRead(sensorPin); // Read the value from the sensor delay(100); // Delay for 1/10 of a second if (sensorValue < 20)// If it is dark, turn all LEDs off { digitalWrite(6, LOW); delay(750); digitalWrite(9, LOW); delay(500); digitalWrite(10, LOW); delay(500); digitalWrite(A3, LOW); delay(500); } else { //If it is not dark, turn green and blue LEDs on and play the tune, then turn on red and white LEDs digitalWrite(6, HIGH); delay(500); digitalWrite(9, HIGH); delay(500); scale(); // Call the scale() function delay(1000); // Delay for 1 second digitalWrite(10, HIGH); delay(500); digitalWrite(A3, HIGH); delay(500); } } void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds) // the sound producing function { int x; long delayAmount = (long)(1000000/frequencyInHertz); long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2)); for (x=0;x