Super Mario Speed Tuner

Submitted by christineschantz on Tue, 03/05/2013 - 22:32

Description

In this assignment I created a ’tempo-tuner’, which speeds up the tempo of the original Super Mario Theme Song. Here I use the potentiometer to speed up the tempo of the song. The song is is adjusted from slow to fast tempo, and the values in between.

I use some of the code from http://www.phys-x.org/rbots/index.php?option=com_content&view=article&id=66:lesson-5-play-melody-with-piezo&catid=41:kits&Itemid=70 to play the song, and then edited the void loop, so it uses the value from the potentiometer.

Challenges

As the tempo of the song depends on many variables (as tempo, duration, beats) in this code, it was hard to figure out which one of these parameters that should be adjusted by the potentiometer. I got help from Laura to figure this out.

Components

·       Arduino Uno

·       Potentiometer

·       Piezo buzzer

·       Resistor (10 ohm)

·       Paper / Carton

·       Tape

Code

// TONES  ==========================================

// Start by defining the relationship between

//       note, period, &  frequency.

// period is in microsecond so P = 1/f * (1E6)

#define  c3    7634

#define  d3    6803

#define  e3    6061

#define  f3    5714

#define  g3    5102

#define  a3    4545

#define  b3    4049

#define  c4    3816    // 261 Hz

#define  d4    3401    // 294 Hz

#define  e4    3030    // 329 Hz

#define  f4    2865    // 349 Hz

#define  g4    2551    // 392 Hz

#define  a4    2272    // 440 Hz

#define  a4s   2146

#define  b4    2028    // 493 Hz

#define  c5    1912    // 523 Hz

#define  d5    1706

#define  d5s   1608

#define  e5    1517

#define  f5    1433

#define  g5    1276

#define  a5    1136

#define  a5s   1073

#define  b5    1012

#define  c6    955

// Define a special note, 'R', to represent a rest

#define  R     0

// SETUP ============================================

// Set up speaker on a PWM pin (digital 9, 10 or 11)

int speakerOut = 9;

int potIn = A0;

// Do we want debugging on serial out? 1 for yes, 0 for no

int DEBUG = 1;

void setup() {

pinMode(speakerOut, OUTPUT);

if (DEBUG) {

Serial.begin(9600); // Set serial out if we want debugging

}

}

//super mario theme

//the tones

int melody[] = {e5, e5, R, e5, R, c5, e5, R, g5, R, R, R, g4, R, R, R, c5, R, R, g4, R, R, e4, e4, e4};

//the beats

int beats[] = {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 , 16, 16, 16, 16, 16, 16, 8, 16, 8, 16, 16, 16, 16};

int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo

long tempo = 10000;

// Set length of pause between notes

int pause = 1000;

// Loop variable to increase Rest length

//int rest_count = 50; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables

int toneM = 0;

int beat = 0;

long duration  = 0;

// PLAY TONE  ==============================================

// Pulse the speaker to play a tone for a particular duration

void playTone() {

long elapsed_time = 0;

if (toneM > 0) { // if this isn't a Rest beat, while the tone has

//  played less long than 'duration', pulse speaker HIGH and LOW

while (elapsed_time < duration) {

digitalWrite(speakerOut,HIGH);

delayMicroseconds(toneM / 2);

// DOWN

digitalWrite(speakerOut, LOW);

delayMicroseconds(toneM / 2);

// Keep track of how long we pulsed

elapsed_time += (toneM);

}

}

}

// LET THE WILD RUMPUS BEGIN =============================

void loop() {

// Set up a counter to pull from melody[] and beats[]

for (int i=0; i<MAX_COUNT; i++) {

toneM = melody[i];

beat = beats[i];

// get potentiometer input

int tempo = map(analogRead(potIn), 0, 1023, 10000, 10500);

int delayValue = map(analogRead(potIn), 0, 1023, 0, 200);

duration = beat * tempo;

Serial.println(duration);

delay(delayValue);

playTone();

}

}

Drupal theme by Kiwi Themes.