Sleep Promoter and Interrupter
Motivation:
I just came to the USA for 2 months. Everything is new. Sometimes I'm too excited to sleep. Sometimes the pressure from heavy workload forces me to stay up late. Consequently I only have limited time for sleep. I need a "Sleep Promoter and Interrupter" to help me sleep well and wake me up after a certain period of time.
Summary:
1. turn off the room light --> play lullaby 5 times, along with a night light on turn on the room light --> lullaby and night light off
2. input a number via keyboard --> play alarm after the number of hours
3. control the alarm --> turn the potentiometer will turn off the alarm or will adjust the frequency.
click to enlarge the sleep promoter and interrupter
Description:
The Sleep Promoter and Interrupter will play lullaby when the surounding gets dark (i.e., after sunset or when I turn off the room light. ) Along with the lullaby melody, the mild night light (LED) will be turned on. If I wokd up, turning on the room light, the lullaby will stop. If there is no light to interrupt the lullaby, it will be played 5 times and stoped. (5 times is enough for me to relax. Keep playing for more than 5 times will disturb me to fall asleep.)
Before going to bed, by simply entering an integer n (0<n<10) via keyboard, the first alarm melody will be played after n hours. (My_Note: Serial input need to be transformed into integer!!!I spent a lot of time to find this bug.)
I use a potentiometer to control the alarm. The alarm could be turned off if the pot value is 0. The potentiometer could also control the the sleep interrupter's frequency. For example, if the potentiometer value is 250, then the alarm melody is played every 5 minutes after the first one.
(I created 2 different pieces of melody, one for lullaby, the other for alarm; In the code I lowered the melody speed when playing the lullaby and increased it when playing the alarm music; )
Future work:
make more pieces of melody, add the second potentiometer to select desired music as alarm melody. (If the alarm melody is always the same, people will get used to it and become more and more difficult to be woke up.)
My Code:
/* Aithne Sheng-Ying Pao
Sleep Promoter and Interrupter
*/
int lightsensorPin = 0; // select the input pin for the light sensor
int potpin = 2; // select the input pin for potentiometer
int val = 0;
int ledPin = 13;
int speakerOut = 7;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; //count2
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956}; // count2
byte melody[] = "3e1p3e6g4e4e6g2p2e2g4C6b2a4a4g2d2e6f2d2d2e6f2p2d2f2b2a4g4b6C"; //count*2+1 indicate the note
byte alarm[] = "1c1e1f5g1c1e1f5g1c1e1f2g2e2c2e5d1e1e1d3c1c2e2g1g5f1e1f2g2e2c2d5c";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int i=0;
int potvalue=0;
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 32;
int MAX_COUNT_ALARM = 32;
int statePin = LOW;
int counttime = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
beginSerial(9600);
}
void loop() {
digitalWrite(speakerOut, LOW);
val = analogRead(lightsensorPin); // read value from the sensor
for (i=0; i<5; i++){
if (val <=100){
for (count = 0; count < MAX_COUNT_ALARM; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 50; count3++) { // count3 <= () *n n indecates the speed (change the length of each note)
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}// end of if
}//end of for i=0-2
counttime = Serial.read();
Serial.print(counttime);
digitalWrite(speakerOut, LOW);
if (counttime != -1){
counttime= (counttime-48)*1000*60*60;
delay(counttime);
Serial.print(counttime);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (alarm[count*2] - 48) * 30; count3++) { // count3 <= () *n n indecates the speed (change the length of each note) -48 is to transform char to number
for (count2=0;count2<8;count2++) {
if (names[count2] == alarm[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (alarm[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}
potvalue = analogRead(potpin);
while (potvalue !=0) {
delay(potvalue*2000);
Serial.print(counttime);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (alarm[count*2] - 48) * 30; count3++) { // count3 <= () *n n indecates the speed (change the length of each note) -48 is to transform char to number
for (count2=0;count2<8;count2++) {
if (names[count2] == alarm[count*2 + 1]) {
digitalWrite(speakerOut,HIGH*potvalue/20);
delayMicroseconds(tones[count2]*potvalue/20);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]*potvalue/20);
}
if (alarm[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}
}