int LightSensor = A3;
int leds = D5;
int StartButton = A0;
int emergencyOff = A1;
const int speakerPin = D2;
int Alarming = 0;
int lightThresh = 150;
int timeElapsed = 0;
int lightReading = 0;
//nunber of notes and rests
const int songLength = 18;
//list of tones and rests
char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
//duration of the notes
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
//tempo of the song
int tempo = 150;
void setup()
{
pinMode(speakerPin, OUTPUT);
pinMode(LightSensor,INPUT);
pinMode(leds,OUTPUT);
pinMode(StartButton,INPUT);
pinMode(emergencyOff,INPUT);
digitalWrite(leds,LOW);
// We only want to play the song once, so we'll put it in the setup loop
//If you want your song to loop forever, place that code in the loop() below.
}
void loop()
{
if (analogRead(StartButton) >= 3000){
Alarming = 1;
//can be set to any amount of time to accommodate a sleep goal
delay(8000);
}
if (analogRead(emergencyOff) >= 3000){
Alarming = 0;
digitalWrite(leds, LOW);
}
//Turning the alarm off with the light sensor
lightReading = analogRead(LightSensor);
if (lightReading < lightThresh){
timeElapsed = 0;
}else{
if (timeElapsed == 0){
timeElapsed = millis();
}
if (timeElapsed + 2000 < millis() ){
Alarming = 0;
}
}
Particle.variable("light", &lightReading, INT);
//The alarm sequence. Lights flash and the audio alarm goes off
if (Alarming){
int i, duration;
delay(500);
for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats[i] * tempo; // length of note/rest in ms
if (notes[i] == ' ') // is this a rest?
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
digitalWrite(leds, HIGH);
tone(speakerPin, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
digitalWrite(leds, LOW);
}
delay(tempo/10); // brief pause between notes
}
}
}
int frequency(char note)
{
// This function takes a note character (a-g), and returns the
// corresponding frequency in Hz for the tone() function.
int i;
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters
// in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes
{
if (names[i] == note) // Is this the one?
{
return(frequencies[i]); // Yes! Return the frequency
}
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .