//within these brackets lie all the stuff needed for the sound app stuff. [ //analog works with core, gotta go digital with photon... // notes in the melody: int errorMelody[] = {}; //C4,G3,G3,A3,G3,0,B3,C4 int successMelody[] = {1568, 1480, 1174, 440, 392, 1319, 1661, 2093}; //to get your nerd thing working, you need this. //High G = 1568 //High F# = 1480 //High D# = 1174 //Middle A = 440 //Middle G# = 392 //High E = 1319 //High G# = 1661 //High C = 2093 // note durations: 4 = quarter note, 8 = eighth note, etc.: int errorNoteDur[] = {}; int successNoteDur[] = {10,10,10,8,8,10,8,6}; int alarmNoteDur[] = {}; int alarmMelody[] = {}; //everything above is for the music part of the app ] int speakerPin = D0; //REMEMBER TO CHANGE THE BUTTON TO WHAT IT IS AT HOME!!! int buttonPin = D4; int buttonState; int numRems; double currTime = 0.0; int currMin; double approxWakeUpTime = 0.0; double actualWakeUpTime = 0.0; int i = 0; String par = ""; boolean wakeReadyFlag = false; boolean awoken = false; //debugging variables double remCycleDur = .000000000005; void setup() { pinMode(buttonPin, INPUT_PULLUP); Particle.subscribe("PhotonWakeUp", checkCloudUpdates); Serial.begin(9600); Serial.println("Subscription locked and loaded"); } void loop() { //Serial.print("You're a god damn miracle worker, Alex."); //This chunk of code below keeps checking for le buttons buttonState = digitalRead(buttonPin); checkCurrTime(); delay(1000); if(buttonState == LOW && wakeReadyFlag == false) { //setActualTime Call here SleepStartClock(); successSound(); delay(2000); Serial.println("This means the alarm was set"); } else if(buttonState == LOW && wakeReadyFlag == false) { errorSound(); delay(2000); } if(currTime == actualWakeUpTime && wakeReadyFlag == true) { serial.println("Alarm should be playing..."); //PlayAlarm Call here while(!awoken) { alarmPlayingSound(); } awoken = false; successSound(); } } // void checkCloudUpdates(const char *event, const char *data) { //code that I took from the internet, should give me the ability to watch this //baby work with a serial monitor Serial.println(event); Serial.println(", data: "); if (data) Serial.println(data); else Serial.println("NULL"); //code that I added afterwards approxWakeUpTime = 0.0; //how I derive the hour par = String(data); par = par.substring((par.indexOf(":") - 2), par.indexOf(":") ); Serial.println(par); approxWakeUpTime = (double)par.toInt(); //how I derive the minutes par = String(data); par = par.substring((par.indexOf(":") + 1), (par.indexOf(":") +3) ); Serial.println(par); //how I combine them into one single all mighty entity approxWakeUpTime = approxWakeUpTime + ((double)par.toInt() / 60.0); Serial.println(approxWakeUpTime); Serial.println("ApproxWakeUpTime = " + (String)approxWakeUpTime); } void SleepStartClock() { //get the current time checkCurrTime(); Serial.println("the time was checked"); //get the time needed to wake up //code below calculates if the alarm can wake you up at a reasonable time. if( (approxWakeUpTime - currTime) >= .00001) { Serial.println("the loop has been travelled past"); numRems = (approxWakeUpTime - currTime) / remCycleDur; actualWakeUpTime = currTime + (remCycleDur * numRems); wakeReadyFlag = true; } //if it cannot, release the mean sounding sound else { errorSound(); } } void checkCurrTime() { //sets currentTime to the current time. currMin = Time.minute() * 100 / 60; //Serial.println( "Curr min ="+ (String)currMin); if (Time.isAM()) { currTime = Time.hour() - 6 + ((double)Time.minute() / 60.0); } else if (Time.hour() == 11) { currTime = ((double)Time.minute() / 60.0) - 12; } else { currTime = -1 * (6 - (Time.hourFormat12() + ((double)Time.minute() / 60.0) ) ); } //Serial.print("currTime = " + (String)currTime); } //Sound Codes Below!! void errorSound() { for (int thisNote = 7; thisNote > -1; thisNote--) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. //int noteDuration = 1000/noteDurations[thisNote]; //tone(speakerPin, melody[thisNote],noteDuration); //ALEX's VERSION OF NOTES int noteDuration = 1000/ successNoteDur[thisNote]; tone(speakerPin, successMelody[thisNote],noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(speakerPin); } } void alarmPlayingSound() { for (int thisNote = 0; thisNote < 46; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. //int noteDuration = 1000/noteDurations[thisNote]; //tone(speakerPin, melody[thisNote],noteDuration); //ALEX's VERSION OF NOTES int noteDuration = 1000/ 4; tone(speakerPin, alarmMelody[7], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(speakerPin); if (buttonState == LOW) { thisNote = 10000; awoken = true; } } } void successSound() { for (int thisNote = 0; thisNote < 8; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. //int noteDuration = 1000/noteDurations[thisNote]; //tone(speakerPin, melody[thisNote],noteDuration); //ALEX's VERSION OF NOTES int noteDuration = 1000/ successNoteDur[thisNote]; tone(speakerPin, successMelody[thisNote],noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(speakerPin); } }