/************************************************************************************************************/ /**********************************Sunrise Alarm with Custom Song Alarm**************************************/ /**************************************************by********************************************************/ /********************************************Dan Steigerwald*************************************************/ /************************************************************************************************************/ /************************************************************************************************************/ int Led1 = 10; // Pin for the LEDs int AlarmIn = 2; // Alarm Input int Alarm = 0; // Variable for reading the Alarm's status int Dim = 0; // Dimming variable (0-255) int Speaker = 9; // Piezo Speaker Pin /**********************************************SONG SETUP****************************************************/ /****************************************Hey Jude by The Beatles*********************************************/ /************************************************************************************************************/ int length = 41; // Number of notes //A space in the notes array is a rest. The corresponding 'beats' number is the duration of the rest// char notes[] = "ge egad defCCbgagfe gaaaCabCag cdeggevvc "; int beats[] = { 4, 8, 1, 2, 2, 3, 8, 1, 4, 2, 4, 6, 2, 2, 2, 2, 1, 1, 8, 1, 2, 2, 4, 4, 1, 2, 1, 1, 4, 4, 4, 2, 2, 2 , 6 , 2 , 4 , 2 , 6 , 8, 16 }; int tempo = 100; /*****************************************Note Playing Loops*************************************************/ void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(Speaker, HIGH); delayMicroseconds(tone); digitalWrite(Speaker, LOW); delayMicroseconds(tone); } } void playNote(char note, int duration) { char names[] = { 'v', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 2024, 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 9; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } /************************************************************************************************************/ /************************************************************************************************************/ /************************************************************************************************************/ void setup() { pinMode(Led1, OUTPUT); // Declare Led1 as an a output pinMode(AlarmIn, INPUT); // Declare AlarmIn as an input pinMode(Speaker, OUTPUT); // Declare Speaker as an output } void loop(){ Alarm = digitalRead(AlarmIn); // Read Alarm State Dim = 0; // Set Dim Variable to 0 do // When Alarm is on, start fade in and alarm process { if(Dim < 75) // If Dim is less then 75. This is done because the lower { // values of the Dim variable have more noticable steps analogWrite(Led1, Dim); // Write new Dim value to Leds delay(8000); // Delay 8 Secounds Dim+=1; // Add 1 to the brightness or Dim value Alarm = digitalRead(AlarmIn); // Check if Alarm state has changed } if(Dim >= 75 && Dim < 255) // If Dim is less then 255, but greater or equal to 75 { analogWrite(Led1, Dim); // Write new Dim value to Leds delay(1667); // Delay 1.667 Seconds Dim+=1; // Add 1 to the brightness or Dim value Alarm = digitalRead(AlarmIn); // Check if Alarm state has changed } if(Dim==255) // Once Dimming Process is complete, sound alarm { for (int i = 0; i < length; i++) // Inputs number of notes to sequence { if (notes[i] == ' ') // If note in sequence is a space, delay with no sound { delay(beats[i] * tempo); // calculates delay with no sound length from beats array } // and the tempo else { playNote(notes[i], beats[i] * tempo); // Play note in array } Alarm = digitalRead(AlarmIn); // Check if Alarm state has changed if(Alarm==LOW) // If Alarm state has changed to low, set song variable to { // 0 and break out of loop i=0; break; } delay(tempo / 2); // Delay in between notes, calculated from tempo inputed above } } }while(Alarm == HIGH); // Conditions of the 'Do While' loop digitalWrite(Led1, LOW); // turn LED OFF // Turn Leds off if Alarm is Low if (Alarm == LOW) // Sets Dim to 0 so when the alarm goes off again, it will start { // the fade in process at 0 Dim=0; } }