Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Alice Nguyen -19
Published

Activating Music Through Temperature

Play 3 songs using your body temperature. Each song is activated based on the temperature level.

BeginnerFull instructions provided1.5 hours787
Activating Music Through Temperature

Things used in this project

Hardware components

Temperature Sensor
Temperature Sensor
×1
LED, Blue
LED, Blue
×1
LED (generic)
LED (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×3
3 mm LED: Yellow
3 mm LED: Yellow
×1
Buzzer, Piezo
Buzzer, Piezo
×1
Jumper wires (generic)
Jumper wires (generic)
×10

Software apps and online services

Arduino IDE
Arduino IDE
Fritzing

Story

Read more

Schematics

Schematic / Circuit Design of Microcontroller

This is the schematic of our temperature activating microcontroller.

Code

TemperatureActivatingMusic.ino

Arduino
This code provides the information needed to connect the LED light, temperature sensor, and music together.
#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
  
  }
 
}
  

pitches.h

Arduino
This is the list of pitches needed for the Super Mario Theme Song. You would need to make a second tab in Arduino to add this code in.
#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_CA  550
#define NOTE_CS5 554
#define NOTE_CT  582
#define NOTE_D5  587
#define NOTE_DA  617
#define NOTE_DS5 622
#define NOTE_DT  654
#define NOTE_E5  659
#define NOTE_EA  693
#define NOTE_F5  698
#define NOTE_FA  734
#define NOTE_FS5 740
#define NOTE_FT  777
#define NOTE_G5  784
#define NOTE_GA  824
#define NOTE_GS5 831
#define NOTE_GT  873
#define NOTE_A5  880
#define NOTE_AA5 925
#define NOTE_AS5 932
#define NOTE_AAT 980
#define NOTE_B5  988
#define NOTE_BBA 1003
#define NOTE_C6  1047
#define NOTE_CCA 1100
#define NOTE_CS6 1109
#define NOTE_CCT 1165
#define NOTE_D6  1175
#define NOTE_DA  1234
#define NOTE_DS6 1245
#define NOTE_DDA 1308
#define NOTE_E6  1319
#define NOTE_EA  1385
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_FFA 1555
#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

Credits

Alice Nguyen -19
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.