Frère Jacques

 

For my stupid pet trick I decided to make an alarm. Lately I’ve been making an effort to wake up earlier every day so it’s easier to do on days I have to, circadian rhythm, all that jazz. Since spring finally seems to me coming (sort of) and the days are getting longer, this alarm will be triggered by the sunrise.

It’s a pretty basic setup: 2 LEDs, a piezo buzzer and a photo cell. When light hits the photocell (assuming daylight, though in this case I used the brightness on my computer), first a yellow LED is triggered, as a warning that it’s time to wake up. Once more light hits, the red LED is activated. When it is finally bright, and time to get up, the piezo plays the tune of Frère Jaques, a song about waking up, which also happens to feature just a few simple notes.

Here is the code, it’s a little long-winded because of the song, but easy to follow. In it, I use two ways of initializing the variable and a few if/else statements.

int lights;
int sound = 9;

void setup(){
  Serial.begin(9600);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(9,OUTPUT);
}

void loop(){
lights = analogRead(A5);
 Serial.println(lights);
 map(lights,490,890,0,255);
  if (lights>50) {digitalWrite(6,HIGH);
 }else{ digitalWrite(6,LOW);
 }
 if (lights>20) {digitalWrite(7,HIGH);  
 }else{ digitalWrite(7,LOW);
 }
 if (lights>250) {tone(sound,261,500);//c 
 delay(500);
 noTone(sound);
 tone(sound, 294,500);//d
 delay(500);
 noTone(sound);
 tone(sound,329,500);//e
 delay(500);
 noTone(sound);
 tone(sound,261,500);//c
 delay(500);
 tone(sound,261,500);//c 
 delay(500);
 noTone(sound);
 tone(sound, 294,500);//d
 delay(500);
 noTone(sound);
 tone(sound,329,500);//e
 delay(500);
 noTone(sound);
 tone(sound,261,500);//c
 delay(550);
 noTone(sound);
 tone(sound,329,500);//e
 delay(500);
 noTone(sound);
 tone(sound,349,500);//f
 delay(500);
 noTone(sound);
 tone(sound,392,500);//g
  delay(550);
 noTone(sound);
 tone(sound,329,500);//e
 delay(500);
 noTone(sound);
 tone(sound,349,500);//f
 delay(500);
 noTone(sound);
 tone(sound,392,500);//g
 delay(900);

 }else{ digitalWrite(7,LOW);
 }

}

http://youtu.be/UyBiVwfu2ZY

Leave a Reply