#include "pitches.h"
int blue_led = 2; //blue led connected to port 2
int yellow_led = 3; //yellow led is connected to port 3
int red_led = 4; //red led is connected to port 4
int buzzer_pin = 8; // piezo is connected to port 8
const int sensorPin = A0; //temperature sensor is connected to the A0 port
const float baselineTemp = 20.0; // the intial temperature needed
struct MusicStruct { //the notes needed for the harry potter song
int A = 550;
int As = 582;
int B = 617;
int C = 654;
int Cs = 693;
int D = 734;
int Ds = 777;
int E = 824;
int F = 873;
int Fs= 925;
int G = 980;
int Gs = 1003;
int A2 = 1100;
int A2s = 1165;
int B2 = 1234;
int C3 = 1308;
int C3s = 1385;
int D2 = 1555;
}Music;
struct LengthStruct {
float half = 0.5;
float one = 1.0;
float one_half = 1.5;
float two = 2.0;
float two_half = 2.5;
}Length;
int tempo = 400; // the pace for the harry potter song
int melody2[] = { //the melody for the mario kart song
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,
};
//Mario main them tempo
int tempo2[] = {
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
};
int melody3[] = { //Note of the pirates of the caribbean song, 0 is a rest/pulse
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
/* NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
*/
};
int tempo3[] = { //duration of each note (in ms) Quarter Note is set to 250 ms
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
// 125, 125, 250, 125, 125,
// 125, 125, 250, 125, 125,
// 125, 125, 250, 125, 125,
// 125, 125, 125, 250, 125,
};
void setup() {
Serial.begin (9600); // open a serial port
//setting up the leds and buzzer pin
pinMode (blue_led,OUTPUT);
digitalWrite(blue_led, LOW);
pinMode(yellow_led,OUTPUT);
digitalWrite(yellow_led,LOW);
pinMode(red_led,OUTPUT);
digitalWrite(red_led,LOW);
pinMode(buzzer_pin,OUTPUT);
};
void setTone(int pin, int note, int duration) { //setting up the tone
tone(pin, note, duration);
delay(duration);
noTone(pin);
}
int size2 = sizeof(melody2) / sizeof(int); // making sure size2 refers to everything in melody 2
int size3 = sizeof(melody3) / sizeof(int); // making sure size 3 refers to everything in melody 3
void loop() {
int sensorVal=analogRead(sensorPin);
//convert the ADC reading to voltage
float voltage = (sensorVal/1024.0) * 5.0;
//convert the voltage to temperature in degrees
float temperature = (voltage - .5) *100;
if(temperature >= baselineTemp+2 && //temperature needed to trigger the blue led
temperature < baselineTemp+4) {
digitalWrite (2, HIGH); // blue light on
digitalWrite (3,LOW); // yellow light off
digitalWrite (4, LOW); // red light off
//melody of harry potter
setTone(buzzer_pin, Music.B, tempo * Length.one);
setTone(buzzer_pin, Music.E, tempo * Length.one_half);
setTone(buzzer_pin, Music.G, tempo * Length.half);
setTone(buzzer_pin, Music.F, tempo * Length.one);
setTone(buzzer_pin, Music.E, tempo * Length.two);
setTone(buzzer_pin, Music.B2, tempo * Length.one);
setTone(buzzer_pin, Music.A2, tempo * Length.two_half);
setTone(buzzer_pin, Music.Fs, tempo * Length.two_half);
setTone(buzzer_pin, Music.E, tempo * Length.one_half);
setTone(buzzer_pin, Music.G, tempo * Length.half);
setTone(buzzer_pin, Music.F, tempo * Length.one);
setTone(buzzer_pin, Music.Ds, tempo * Length.two);
setTone(buzzer_pin, Music.F, tempo * Length.one);
setTone(buzzer_pin, Music.B, tempo * Length.two_half);
delay(500000);
noTone(8); //stop music
}
else if(temperature >= baselineTemp+4 && temperature < baselineTemp+10){ //requirements for yellow led
digitalWrite(2,LOW); // blue light is off
digitalWrite(3,HIGH); // yellow light turns on
digitalWrite(4,LOW); //red light is off
for (int x = 0; x < size2; x++) { //this refers to the loop & size2 refers to the melody
// 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 / tempo2[x];
tone(8, melody2[x], 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(8);
}
}
else if(temperature >= baselineTemp+10){ // temperature required for red led
digitalWrite (2,LOW); // blue light off
digitalWrite (3,LOW); // yellow light is off
digitalWrite (4,HIGH); // red light turns on
for (int i=0;i<size3;i++){ //size 3 is the total number of music notes in the song
int wait = tempo3[i] * 1.5;
tone(8,melody3[i],wait); //tone(pin,frequency,duration)
delay(wait); // to pause
noTone(8);} // stop music
}
}
Comments
Please log in or sign up to comment.