Mario Arduino from Eva Daly on Vimeo.
For this project I used super bright LEDs which would play in sync to the Mario theme which I downloaded from the Arduino website, the theme is being played through a piezo speaker. I assigned each LED 2 notes, so anytime the note in the melody played the LED would flash. I then used the potentiometer to control the tempo. Here is my code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This example uses a piezo speaker to play melodies. It sends | |
* a square wave of the appropriate frequency to the piezo, generating | |
* the corresponding tone. | |
* | |
* The calculation of the tones is made following the mathematical | |
* operation: | |
* | |
* timeHigh = period / 2 = 1 / (2 * toneFrequency) | |
* | |
* where the different tones are described as in the table: | |
* | |
* note frequency period timeHigh | |
* c 261 Hz 3830 1915 | |
* d 294 Hz 3400 1700 | |
* e 329 Hz 3038 1519 | |
* f 349 Hz 2864 1432 | |
* g 392 Hz 2550 1275 | |
* a 440 Hz 2272 1136 | |
* b 493 Hz 2028 1014 | |
* C 523 Hz 1912 956 | |
* | |
* http://www.arduino.cc/en/Tutorial/Melody | |
*/ | |
int speakerPin = 8; | |
int length = 295; // the number of notes | |
char notes[] = "EE E CE G g C g e a b ia gEGA FG E CDb C g e a b ia gEGA FG E CDb GNFR E uaC aCD GNFR E 1 11 GNFR E uaC aCD L D C CC C CD EC ag CC C CDE CC C CD EC ag EE E CE G g C g e a b ia gEGA FG E CDb C g e a b ia gEGA FG E CDb EC g u aF Fa bAAAGFEC ag EC g u aF Fa bF FFEDCe ec "; // a space represents a rest | |
float beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //Page 1 | |
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 4, //Page 2 | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, //Page4 | |
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //Page 5 | |
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1.3, 1.3, 1.3, 1.3, 1.3, 1.3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1.3, 1.3, 1.3, 1, 1, 1, 1, 1, 1, 2 }; //Page 6 | |
float tempo = 95; //this is my main tempo variable | |
int maxTempo = 240; //this is my max tempo | |
int myPot = 0; //this is the number of the analog in on the arduino that your pot goes into | |
void playTone(int ton1, int duration) { | |
for (long i = 0; i < duration * 1000L; i += ton1) { | |
tone(speakerPin, ton1); | |
//digitalWrite(led1, HIGH); | |
lightLED(ton1, duration); //This turns the led off for a given tone but it doesn;t turn it off again | |
delayMicroseconds(ton1); | |
} | |
noTone(speakerPin); | |
LEDsOff(); //here is where we turn of the led after the note has been played | |
//digitalWrite(led1, LOW); | |
} | |
void lightLED(int ton, int duration){ | |
int led1 = 1, led2 = 2, led3 = 3, led4 = 4, led5 = 5, led6 = 6; | |
pinMode(led1, OUTPUT); | |
pinMode(led2, OUTPUT); | |
pinMode(led3, OUTPUT); | |
pinMode(led4, OUTPUT); | |
pinMode(led5, OUTPUT); | |
pinMode(led6, OUTPUT); | |
//E | |
if(ton == 659){ | |
digitalWrite(led1, HIGH); | |
} | |
//e | |
if(ton == 329){ | |
digitalWrite(led1, HIGH); | |
} | |
//C | |
if(ton == 523){ | |
digitalWrite(led2, HIGH); | |
} | |
//c | |
if(ton == 261){ | |
digitalWrite(led2, HIGH); | |
} | |
//G | |
if(ton == 783){ | |
digitalWrite(led3, HIGH); | |
} | |
//g | |
if(ton == 391){ | |
digitalWrite(led3, HIGH); | |
} | |
//A | |
if(ton == 880){ | |
digitalWrite(led4, HIGH); | |
} | |
//a | |
if(ton == 440){ | |
digitalWrite(led4, HIGH); | |
} | |
//b | |
if(ton == 493){ | |
digitalWrite(led5, HIGH); | |
} | |
//i | |
if(ton == 466){ | |
digitalWrite(led6, HIGH); | |
} | |
//F | |
if(ton == 698){ | |
digitalWrite(led1, HIGH); | |
} | |
//D | |
if(ton == 587){ | |
digitalWrite(led2, HIGH); | |
} | |
//N | |
if(ton == 740){ | |
digitalWrite(led3, HIGH); | |
} | |
//R | |
if(ton == 622){ | |
digitalWrite(led4, HIGH); | |
} | |
//1 | |
if(ton == 1046){ | |
digitalWrite(led5, HIGH); | |
} | |
//L | |
if(ton == 622u){ | |
digitalWrite(led6, HIGH); | |
} | |
} | |
void LEDsOff(){ | |
int led1 = 1, led2 = 2, led3 = 3, led4 = 4, led5 = 5, led6 = 6; | |
pinMode(led1, OUTPUT); | |
pinMode(led2, OUTPUT); | |
pinMode(led3, OUTPUT); | |
pinMode(led4, OUTPUT); | |
pinMode(led5, OUTPUT); | |
pinMode(led6, OUTPUT); | |
digitalWrite(led1, LOW); | |
digitalWrite(led2, LOW); | |
digitalWrite(led3, LOW); | |
digitalWrite(led4, LOW); | |
digitalWrite(led5, LOW); | |
digitalWrite(led6, LOW); | |
} | |
void playNote(char note, int duration) { | |
// c c# d d# e f f# g g# a a# b | |
char names[] = { ' ', '!', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', 'i', 'N', 'R', 'u', '1', 'L', 'k'}; // [i = b flat] [N = G flat] [R = D#] [u = g#] [1 = C oct. 5] [L = E flat] | |
int tones[] = { 0, 1046, 138, 146, 155, 164, 174, 184, 195, 207, 220, 233, 246, 261, 293, 329, 349, 391, 440, 493, 523, 587, 659, 698, 783, 880, 987, 466, 740, 622, 415, 1046, 622u, 227}; | |
// play the tone corresponding to the note name | |
for (int i = 0; i < 34; i++) { | |
if (names[i] == note) { | |
playTone(tones[i], duration); | |
} | |
} | |
} | |
void setup() { | |
pinMode(speakerPin, OUTPUT); | |
} | |
void loop() { | |
for (int i = 0; i < length; i++) { | |
if (notes[i] == ' ') { | |
delay(beats[i] * tempo); // rest | |
} else { | |
playNote(notes[i], beats[i] * tempo); | |
} | |
tempo = analogRead(myPot); //analog read gives a value between 0 and 1023 | |
// pause between notes | |
tempo = tempo/1024; //this will limit the range between 0.0 and 1.0 | |
tempo = (tempo * maxTempo) + 40; //then we mutliply the maxTempo. I added 40 just so it doesn't get too slow | |
int tempoInt = (float) tempo; | |
delay(tempoInt / 2); | |
} | |
} |
No comments:
Post a Comment