Interactive Alarm Clock Code
Dependencies: 4DGL-uLCD-SE mbed
Revision 0:68a3851de2ee, committed 2015-12-10
- Comitter:
- tshin7
- Date:
- Thu Dec 10 08:59:55 2015 +0000
- Commit message:
- Alarm Clock
Changed in this revision
diff -r 000000000000 -r 68a3851de2ee 4DGL-uLCD-SE.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
diff -r 000000000000 -r 68a3851de2ee SongPlayer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SongPlayer.h Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,41 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +class SongPlayer +{ +public: + SongPlayer(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the constructor + } +// class method to play a note based on PwmOut class + void PlaySong(float frequency[], float duration[], float volume=1.0) { + vol = volume; + notecount = 0; + _pin.period(1.0/frequency[notecount]); + _pin = volume/2.0; + noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); + // setup timer to interrupt for next note to play + frequencyptr = frequency; + durationptr = duration; + //returns after first note starts to play + } + void nextnote(); +private: + Timeout noteduration; + PwmOut _pin; + int notecount; + float vol; + float * frequencyptr; + float * durationptr; +}; +//Interrupt Routine to play next note +void SongPlayer::nextnote() +{ + _pin = 0.0; + notecount++; //setup next note in song + if (durationptr[notecount]!=0.0) { + _pin.period(1.0/frequencyptr[notecount]); + noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); + _pin = vol/2.0; + } else + _pin = 0.0; //turn off on last note +} \ No newline at end of file
diff -r 000000000000 -r 68a3851de2ee alarmChoice.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/alarmChoice.cpp Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,44 @@ +#include "mbed.h" +#include "uLCD_4DGL.h" +#include "alarmChoice.h" +#include <string> + +uLCD_4DGL game(p9,p10,p11); // serial tx, serial rx, reset pin; +int alarmGame; +string gameName = "LED Color Sequence"; + +void alarmChoice::displayChoice() +{ + game.locate(0,10); + game.printf("Alarm game:"); + game.locate(0,11); + game.printf("%s",gameName); +} + +string alarmChoice::setGameName() +{ + if (gameName == "Random "){ + gameName = "LED Color Sequence"; + } else if (gameName == "LED Color Sequence"){ + gameName = "Math Problem "; + } else if (gameName == "Math Problem "){ + gameName = "Mission Control "; + } else if (gameName == "Mission Control "){ + gameName = "Random "; + } + return gameName; +} + +int alarmChoice::setGame() +{ + if (gameName == "Random "){ + alarmGame = rand()%3; + } else if (gameName == "LED Color Sequence"){ + alarmGame = 0; + } else if (gameName == "Math Problem "){ + alarmGame = 1; + } else if (gameName == "Mission Control "){ + alarmGame = 2; + } + return alarmGame; +}
diff -r 000000000000 -r 68a3851de2ee alarmChoice.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/alarmChoice.h Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,9 @@ +#include "mbed.h" +#include <string> +class alarmChoice +{ + public: + void displayChoice(); + string setGameName(); + int setGame(); +}; \ No newline at end of file
diff -r 000000000000 -r 68a3851de2ee ledColorSequence.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ledColorSequence.cpp Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,71 @@ +#include "mbed.h" +#include "timeDisplay.h" +#include "setAlarm.h" +#include "ledColorSequence.h" +#include <string> + +Serial pc(USBTX,USBRX); +DigitalOut red(p22); +DigitalOut green(p23); +DigitalOut blue(p24); +int color; +int colorCount=0; +char colorSequence[5]; + +string ledColorSequence::chooseColor() +{ + while(colorCount<4){ + color = rand()%3; + if (color==0){ + red=1; + colorSequence[colorCount] = 'r'; + wait(0.4); + red=0; + }else if (color==1){ + green=1; + colorSequence[colorCount] = 'g'; + wait(0.4); + green=0; + }else if (color==2) { + blue=1; + colorSequence[colorCount] = 'b'; + wait(0.4); + blue=0; + } + colorCount++; + } + colorSequence[4]='\0'; + colorCount=0; + return colorSequence; +} + +void ledColorSequence::repeatColorSequence(string colorSequence) +{ + while(colorCount<4){ + if (colorSequence[colorCount] == 'r'){ + red=1; + wait(0.4); + red=0; + }else if (colorSequence[colorCount] == 'g'){ + green=1; + wait(0.4); + green=0; + }else if (colorSequence[colorCount] == 'b') { + blue=1; + wait(0.4); + blue=0; + } + colorCount++; + } + colorCount=0; +} + +void ledColorSequence::turnOffColor() +{ + if (blue==1||green==1||red==1){ + blue=0; + green=0; + red=0; + } +} +
diff -r 000000000000 -r 68a3851de2ee ledColorSequence.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ledColorSequence.h Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,9 @@ +#include "mbed.h" +#include <string> +class ledColorSequence +{ + public: + string chooseColor(); + void repeatColorSequence(string); + void turnOffColor(); +}; \ No newline at end of file
diff -r 000000000000 -r 68a3851de2ee main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,147 @@ +#include "mbed.h" +#include "timeDisplay.h" +#include "uLCD_4DGL.h" +#include "setAlarm.h" +#include "ledColorSequence.h" +#include "mathProblem.h" +#include "speaker.h" +#include "alarmChoice.h" +#include <string> + +DigitalIn hourSet(p5); +DigitalIn minSet(p6); +DigitalIn gameChoice(p7); +DigitalIn missionControlWin(p28); +DigitalOut missionControlReset(p29); +DigitalOut missionControlStart(p30); +Serial device(USBTX,USBRX); +Serial bluetooth(p13,p14); +timeDisplay timeLCD; +setAlarm alarmSet; +ledColorSequence LedGame; +mathProblem mathProblemGame; +alarmChoice alarmGameChoice; +speaker speakerPlay; +Timer tSpeaker; +Timer tLED; + +string currentTime; +string currentAlarmTime; +string chosenGameName = "LED Color Sequence"; +char mathProblemAnswerStr [5]; +string ledColorSeq = " "; +char input; +int chosenGame = 0; +int mathProblemAnswer=0; +int mathProblemAnswerLength; +int charCount = 0; +int matched = 0; +int score; + +int main() +{ + hourSet.mode(PullDown); + minSet.mode(PullDown); + gameChoice.mode(PullDown); + timeLCD.setTime(); + bluetooth.baud(115200); + tSpeaker.start(); + tLED.start(); + while(1) { + printf("%s", missionControlWin ? "true" : "false"); + currentTime = timeLCD.displayTime(); + currentAlarmTime = alarmSet.displayAlarmTime(); + alarmGameChoice.displayChoice(); + if(hourSet==1) { + alarmSet.setHour(); + } else if (minSet==1) { + alarmSet.setMinute(); + } + if (gameChoice == 1) { + chosenGameName = alarmGameChoice.setGameName(); + chosenGame = alarmGameChoice.setGame(); + } + if (currentAlarmTime.compare(currentTime)==0) { + speakerPlay.playSpeaker(); + if (chosenGame == 0) { + ledColorSeq=LedGame.chooseColor(); + tLED.stop(); + tLED.reset(); + tLED.start(); + wait(.9); + } else if (chosenGame == 1) { + mathProblemAnswer = mathProblemGame.displayMathProblem(); + mathProblemAnswerLength = sprintf(mathProblemAnswerStr,"%d",mathProblemAnswer); + wait(.9); + } else if (chosenGame ==2) { + missionControlReset=1; + missionControlStart=1; + wait(.9); + } + } + if (ledColorSeq!=" ") { + if(tSpeaker.read()>=7.65){ + tSpeaker.stop(); + tSpeaker.reset(); + tSpeaker.start(); + speakerPlay.playSpeaker(); + } + if(tLED.read()>=5){ + tLED.stop(); + tLED.reset(); + tLED.start(); + LedGame.repeatColorSequence(ledColorSeq); + } + while(bluetooth.readable()) { + while(charCount<ledColorSeq.length()) { + if (bluetooth.getc()==ledColorSeq[charCount]) { + matched++; + charCount++; + } + } + if (matched==ledColorSeq.length()) { + speakerPlay.turnOffSpeaker(); + LedGame.turnOffColor(); + ledColorSeq=" "; + } + charCount=0; + matched=0; + } + } else if (mathProblemAnswer != 0) { + if(tSpeaker.read()>=7.65){ + tSpeaker.stop(); + tSpeaker.reset(); + tSpeaker.start(); + speakerPlay.playSpeaker(); + } + while(bluetooth.readable()) { + while(charCount<mathProblemAnswerLength) { + if (bluetooth.getc()==mathProblemAnswerStr[charCount]) { + matched++; + charCount++; + } + } + if (matched==mathProblemAnswerLength) { + speakerPlay.turnOffSpeaker(); + mathProblemGame.eraseMathProblem(); + mathProblemAnswer=0; + } + charCount=0; + matched=0; + } + } else if (missionControlStart == 1) { + if(tSpeaker.read()>=7.65){ + tSpeaker.stop(); + tSpeaker.reset(); + tSpeaker.start(); + speakerPlay.playSpeaker(); + } + if (missionControlWin == 1){ + missionControlReset=0; + missionControlReset=1; + missionControlStart=0; + speakerPlay.turnOffSpeaker(); + } + } + } +}
diff -r 000000000000 -r 68a3851de2ee mathProblem.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mathProblem.cpp Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,58 @@ +#include "mbed.h" +#include "uLCD_4DGL.h" +#include "mathProblem.h" +#include <string> + +uLCD_4DGL math(p9,p10,p11); // serial tx, serial rx, reset pin; +int ranNum1; +int ranNum2; +int ranNum3; +int answer; +int operationChoice; + +int mathProblem::displayMathProblem() +{ + math.locate(0,14); + math.printf("Math Problem:"); + + operationChoice = rand()%3; + + if (operationChoice == 0) { + ranNum1=rand()%9+1; + ranNum2=rand()%9+1; + ranNum3=rand()%9+1; + + answer = ranNum1 + ranNum2 * ranNum3; + + math.locate(0,15); + math.printf("%d + %d * %d = ???",ranNum1, ranNum2, ranNum3); + } else if (operationChoice == 1) { + ranNum1=rand()%9+1; + ranNum2=rand()%9+1; + ranNum3=rand()%9+1; + + answer = ranNum1 - ranNum2 * ranNum3; + + math.locate(0,15); + math.printf("%d - %d * %d = ???",ranNum1, ranNum2, ranNum3); + } else if (operationChoice == 2) { + ranNum1=rand()%9+1; + ranNum2=rand()%9+1; + ranNum3=rand()%9+1; + + answer = ranNum1 * ranNum2 * ranNum3; + + math.locate(0,15); + math.printf("%d * %d * %d = ???",ranNum1, ranNum2, ranNum3); + } + return answer; +} + +void mathProblem::eraseMathProblem() +{ + + math.locate(0,14); + math.printf(" "); + math.locate(0,15); + math.printf(" "); +} \ No newline at end of file
diff -r 000000000000 -r 68a3851de2ee mathProblem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mathProblem.h Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,8 @@ +#include "mbed.h" +#include <string> +class mathProblem +{ + public: + int displayMathProblem(); + void eraseMathProblem(); +}; \ No newline at end of file
diff -r 000000000000 -r 68a3851de2ee mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/165afa46840b \ No newline at end of file
diff -r 000000000000 -r 68a3851de2ee setAlarm.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setAlarm.cpp Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,61 @@ +#include "mbed.h" +#include "uLCD_4DGL.h" +#include "setAlarm.h" +#include <string> + +uLCD_4DGL alarm(p9,p10,p11); // serial tx, serial rx, reset pin; +string alarmTime = "12:00:00 AM"; + +string setAlarm::displayAlarmTime() +{ + alarm.locate(0,5); + alarm.printf("Alarm time:"); + alarm.locate(0,6); + alarm.printf("%s",alarmTime); + return alarmTime; +} +void setAlarm::setHour() +{ + if ((alarmTime[0]=='0'&&alarmTime[1]<'9')||(alarmTime[0]=='1'&&alarmTime[1]<'1')) { + alarmTime[1]=alarmTime[1]+1; + } else if (alarmTime[0]=='0'&&alarmTime[1]=='9') { + alarmTime[0]='1'; + alarmTime[1]='0'; + } else if (alarmTime[0] =='1'&&alarmTime[1]=='1') { + alarmTime[1]=alarmTime[1]+1; + if (alarmTime[9]=='A') { + alarmTime[9]='P'; + } else if (alarmTime[9]=='P') { + alarmTime[9]='A'; + } + } else if (alarmTime[0]=='1'&&alarmTime[1]=='2') { + alarmTime[0]='0'; + alarmTime[1]='1'; + } +} + +void setAlarm::setMinute() +{ + if ((alarmTime[3]<'6'&&alarmTime[4]<'9')) { + alarmTime[4]=alarmTime[4]+1; + } else if (alarmTime[3]<'5'&&alarmTime[4]=='9') { + alarmTime[3]=alarmTime[3]+1; + alarmTime[4]='0'; + } else if (alarmTime[3]=='5'&&alarmTime[4]=='9') { + alarmTime[3]='0'; + alarmTime[4]='0'; + if ((alarmTime[0]<'1'&&alarmTime[1]<'9')||(alarmTime[0]=='1'&&alarmTime[1]<'1')){ + alarmTime[1]=alarmTime[1]+1; + } else if (alarmTime[0] =='1'&&alarmTime[1]=='1') { + alarmTime[1]=alarmTime[1]+1; + if (alarmTime[9]=='A') { + alarmTime[9]='P'; + } else if (alarmTime[9]=='P') { + alarmTime[9]='A'; + } + } else if (alarmTime[0]=='1'&&alarmTime[1]=='2') { + alarmTime[0]='0'; + alarmTime[1]='1'; + } + } +} \ No newline at end of file
diff -r 000000000000 -r 68a3851de2ee setAlarm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setAlarm.h Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,9 @@ +#include "mbed.h" +#include <string> +class setAlarm +{ + public: + string displayAlarmTime(); + void setHour(); + void setMinute(); +}; \ No newline at end of file
diff -r 000000000000 -r 68a3851de2ee speaker.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/speaker.cpp Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,160 @@ +#include "mbed.h" +#include "speaker.h" +#include "SongPlayer.h" + +#define NOTE_B0 31 +#define NOTE_C1 33 +#define NOTE_CS1 35 +#define NOTE_D1 37 +#define NOTE_DS1 39 +#define NOTE_E1 41 +#define NOTE_F1 44 +#define NOTE_FS1 46 +#define NOTE_G1 49 +#define NOTE_GS1 52 +#define NOTE_A1 55 +#define NOTE_AS1 58 +#define NOTE_B1 62 +#define NOTE_C2 65 +#define NOTE_CS2 69 +#define NOTE_D2 73 +#define NOTE_DS2 78 +#define NOTE_E2 82 +#define NOTE_F2 87 +#define NOTE_FS2 93 +#define NOTE_G2 98 +#define NOTE_GS2 104 +#define NOTE_A2 110 +#define NOTE_AS2 117 +#define NOTE_B2 123 +#define NOTE_C3 131 +#define NOTE_CS3 139 +#define NOTE_D3 147 +#define NOTE_DS3 156 +#define NOTE_E3 165 +#define NOTE_F3 175 +#define NOTE_FS3 185 +#define NOTE_G3 196 +#define NOTE_GS3 208 +#define NOTE_A3 220 +#define NOTE_AS3 233 +#define NOTE_B3 247 +#define NOTE_C4 262 +#define NOTE_CS4 277 +#define NOTE_D4 294 +#define NOTE_DS4 311 +#define NOTE_E4 330 +#define NOTE_F4 349 +#define NOTE_FS4 370 +#define NOTE_G4 392 +#define NOTE_GS4 415 +#define NOTE_A4 440 +#define NOTE_AS4 466 +#define NOTE_B4 494 +#define NOTE_C5 523 +#define NOTE_CS5 554 +#define NOTE_D5 587 +#define NOTE_DS5 622 +#define NOTE_E5 659 +#define NOTE_F5 698 +#define NOTE_FS5 740 +#define NOTE_G5 784 +#define NOTE_GS5 831 +#define NOTE_A5 880 +#define NOTE_AS5 932 +#define NOTE_B5 988 +#define NOTE_C6 1047 +#define NOTE_CS6 1109 +#define NOTE_D6 1175 +#define NOTE_DS6 1245 +#define NOTE_E6 1319 +#define NOTE_F6 1397 +#define NOTE_FS6 1480 +#define NOTE_G6 1568 +#define NOTE_GS6 1661 +#define NOTE_A6 1760 +#define NOTE_AS6 1865 +#define NOTE_B6 1976 +#define NOTE_C7 2093 +#define NOTE_CS7 2217 +#define NOTE_D7 2349 +#define NOTE_DS7 2489 +#define NOTE_E7 2637 +#define NOTE_F7 2794 +#define NOTE_FS7 2960 +#define NOTE_G7 3136 +#define NOTE_GS7 3322 +#define NOTE_A7 3520 +#define NOTE_AS7 3729 +#define NOTE_B7 3951 +#define NOTE_C8 4186 +#define NOTE_CS8 4435 +#define NOTE_D8 4699 +#define NOTE_DS8 4978 + +float note[]= { + NOTE_E7, NOTE_E7, 0, NOTE_E7, + 0, NOTE_C7, NOTE_E7, 0, + NOTE_G7, 0, 0, 0, + NOTE_G6, 0, 0, 0, + + NOTE_C7, 0, 0, NOTE_G6, + 0, 0, NOTE_E6, 0, + 0, NOTE_A6, 0, NOTE_B6, + 0, NOTE_AS6, NOTE_A6, 0, + + NOTE_G6, NOTE_E7, NOTE_G7, + NOTE_A7, 0, NOTE_F7, NOTE_G7, + 0, NOTE_E7, 0, NOTE_C7, + NOTE_D7, NOTE_B6, 0, 0, + + NOTE_C7, 0, 0, NOTE_G6, + 0, 0, NOTE_E6, 0, + 0, NOTE_A6, 0, NOTE_B6, + 0, NOTE_AS6, NOTE_A6, 0, + + NOTE_G6, NOTE_E7, NOTE_G7, + NOTE_A7, 0, NOTE_F7, NOTE_G7, + 0, NOTE_E7, 0, NOTE_C7, + NOTE_D7, NOTE_B6, 0, 0 +}; +float duration[]= { + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + + 0.075,0.075,0.075, + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + + 0.075,0.075,0.075, + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, + 0.1, 0.1, 0.1, 0.1, +}; + +float off[1] = {0.0}; + +SongPlayer mySpeaker(p25); + +void speaker::playSpeaker() +{ + mySpeaker.PlaySong(note,duration); +} + +void speaker::turnOffSpeaker() +{ + mySpeaker.PlaySong(off,off); +} \ No newline at end of file
diff -r 000000000000 -r 68a3851de2ee speaker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/speaker.h Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,7 @@ +#include "mbed.h" +class speaker +{ + public: + void playSpeaker(); + void turnOffSpeaker(); +}; \ No newline at end of file
diff -r 000000000000 -r 68a3851de2ee timeDisplay.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/timeDisplay.cpp Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,39 @@ +#include "mbed.h" +#include "uLCD_4DGL.h" +#include "timeDisplay.h" +#include <string> + +DigitalIn hour(p5); +DigitalIn minute(p6); +DigitalIn set(p7); + +uLCD_4DGL timeScreen(p9,p10,p11); // serial tx, serial rx, reset pin; + +void timeDisplay::setTime() { + hour.mode(PullDown); + minute.mode(PullDown); + set.mode(PullDown); + timeScreen.locate(0,0); + int seconds = 0; + while (set==0){ + if(hour==1) { + seconds = seconds + 3600; + set_time(seconds); + } else if (minute==1) { + seconds = seconds + 60; + set_time(seconds); + } + time_t timeSec = time(NULL); + char buffer[32]; + strftime(buffer, 32, "%I:%M:%S %p\r", localtime(&timeSec)); + timeScreen.printf("%s", buffer); + } +} +string timeDisplay::displayTime() { + timeScreen.locate(0,0); + time_t seconds = time(NULL); + char buffer[32]; + strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds)); + timeScreen.printf("%s", buffer); + return buffer; +}
diff -r 000000000000 -r 68a3851de2ee timeDisplay.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/timeDisplay.h Thu Dec 10 08:59:55 2015 +0000 @@ -0,0 +1,8 @@ +#include "mbed.h" +#include <string> +class timeDisplay +{ + public: + void setTime(); + string displayTime(); +}; \ No newline at end of file