int LDR_330_Pin = 9; //define a pin for Photo resistor that detects if its daytime int LDR_Day_Pin = 10; //define a pin for Photo resistor that detects if its 3:30 int led_Pin = 6; //define a pin for LED int buzzerPin = 3; //define a pin for the buzzer int LDR_330_Pin_Value=0; int LDR_Day_Pin_Value=0; int threshold=280; const int songLength = 4; // Notes is an array of text characters corresponding to the notes // in your song. A space represents a rest (no tone) char notes[] = "f g "; // a space represents a rest // Beats is an array of values for each note and rest. // A "1" represents a quarter-note, 2 a half-note, etc. // Rests (spaces) need a length as well. int beats[] = {2,20,2,20}; // The tempo is how fast to play the song. // To make the song play faster, decrease this value. int tempo = 100; void setup() { Serial.begin(9600); //Begin serial communcation pinMode(led_Pin, OUTPUT ); pinMode(buzzerPin, OUTPUT); digitalWrite(led_Pin, LOW); } void loop() { int i, duration; LDR_330_Pin_Value=analogRead(LDR_330_Pin); LDR_Day_Pin_Value=analogRead(LDR_Day_Pin); Serial.print("LDR_330_Pin_Value="); Serial.print(LDR_330_Pin_Value); Serial.print(" LDR_Day_Pin_Value="); Serial.print(LDR_Day_Pin_Value); Serial.print(" tempo="); Serial.println(tempo); if (LDR_Day_Pin_Value>threshold) { //meaning its daytime if (LDR_Day_Pin_Value-LDR_330_Pin_Value>500) { //difference is higher than 500 digitalWrite(led_Pin, HIGH); for (i = 0; i < songLength; i++) // step through the song arrays { duration = beats[i] * tempo; // length of note/rest in ms if (notes[i] == ' ') // is this a rest? { delay(duration); // then pause for a moment } else // otherwise, play the note { tone(buzzerPin, frequency(notes[i]), duration); delay(duration); // wait for tone to finish } delay(tempo/10); // brief pause between notes } if (tempo > 30) tempo=tempo-1; } else { digitalWrite(led_Pin, LOW); tempo = 100; } } else { digitalWrite(led_Pin, LOW); tempo = 100; } delay(400); } int frequency(char note) { // This function takes a note character (a-g), and returns the // corresponding frequency in Hz for the tone() function. int i; const int numNotes = 8; // number of notes we're storing // The following arrays hold the note characters and their // corresponding frequencies. The last "C" note is uppercase // to separate it from the first lowercase "c". If you want to // add more notes, you'll need to use unique characters. // For the "char" (character) type, we put single characters // in single quotes. char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523}; // Now we'll search through the letters in the array, and if // we find it, we'll return the frequency for that note. for (i = 0; i < numNotes; i++) // Step through the notes { if (names[i] == note) // Is this the one? { return(frequencies[i]); // Yes! Return the frequency } } return(0); // We looked through everything and didn't find it, // but we still need to return a value, so return 0. }