Back to Parent

const int SENSOR_PIN = 0;
/*
  Arduino Mario Bros Tunes
  With Piezo Buzzer and PWM
  by: Dipto Pratyaksa
  last updated: 31/3/13
*/
#include "pitches.h"


#define melodyPin 3
//Mario main theme melody
int melody[] = {
  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, 

  NOTE_G6, NOTE_E7, NOTE_G7, 
  NOTE_A7, 0, NOTE_F7, NOTE_G7, 
  0, NOTE_E7, 0,NOTE_C7, 
  NOTE_D7, NOTE_B6, 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, 

  NOTE_G6, NOTE_E7, NOTE_G7, 
  NOTE_A7, 0, NOTE_F7, NOTE_G7, 
  0, NOTE_E7, 0,NOTE_C7, 
  NOTE_D7, NOTE_B6, 0, 0
};
//Mario main them tempo
int tempo[] = {
  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, 

  9, 9, 9,
  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,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
};

//
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(3, OUTPUT);//speaker
  pinMode(13, OUTPUT); //LED when singing a note
  

}

void loop() {



  

  sing(1);
}
int song = 0;

void sing(int s){      
   // iterate over the notes of the melody:
   song = s;

     int size = sizeof(melody) / sizeof(int);
     for (int thisNote = 0; thisNote < size; thisNote++) {

       // 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/tempo[thisNote];

       tone(melodyPin, melody[thisNote],noteDuration);
         int speedOfSong;
           int sensorValue;
  sensorValue = analogRead(SENSOR_PIN);
  Serial.println(sensorValue);

  if (sensorValue > 10 and sensorValue < 200){
    speedOfSong = 1.3;
    Serial.println("stage 0");
  }
  if (sensorValue > 200 and sensorValue < 400) {
    speedOfSong = 1.6;
    Serial.println("stage 1");
  }
  if (sensorValue > 400 and sensorValue < 600){
    speedOfSong = 2;
    Serial.println("stage 2");
  }
  if (sensorValue > 600 and sensorValue < 800) {
    speedOfSong = 2.5;
    Serial.println("stage 3");
  }
  if (sensorValue > 800 and sensorValue < 1000) {
    speedOfSong = 3;
    Serial.println("stage 4");
  }


       // to distinguish the notes, set a minimum time between them.
       // the note's duration + 30% seems to work well:
       int pauseBetweenNotes = noteDuration * speedOfSong;
       delay(pauseBetweenNotes);

       // stop the tone playing:
       tone(melodyPin, 0, noteDuration);

    }
  }
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0