Piezo Element The Imperial March
Moderators: adafruit_support_bill, adafruit

Piezo Element The Imperial March

by rkill on Thu Nov 22, 2012 7:01 pm

Hello All,

I'm trying to get my Arduino to play The Imperial March on a piezo element. I must say that I'm a bit shocked that the code is not spread across every corner of the Internet. There's lots of videos and blog posts demonstrating The Imperial March on a piezo, but no code. One would think this should be the audible Hello World for any sound producing Arduino project.

I'm using the ARDX Piezo Elements turtoiral code @ http://ardx.org/CODE06

From what I can gather the keyboard notes are along the lines of a-a-a-f-c-a-f-c-a-e-e-e-g-e-c-f-c-a.

Here's the section of code I'm attempting to edit to make the magic happen.

Code: Select all | TOGGLE FULL SIZE
int length = 19; // the number of notes
char notes[] = "aaafcafcaeeegecfca "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int tempo = 300;


Not sure if the sharp notes will be possible using this code, but I'm not looking for perfection. Also it's easy to see, and hear, that my tempo and beats are all wrong.

I was able to find the following example which uses a TA MSP430 Launchpad. The tutorial references an Arduino project that was used to port code to the Launchpad. Unfortunately, the link to the Arduino code is broken.

http://processors.wiki.ti.com/index.php/Playing_The_Imperial_March

Any help would be much appreciated!
rkill
 
Posts: 2
Joined: Sun Sep 02, 2012 5:11 pm

Re: Piezo Element The Imperial March

by rkill on Sun Nov 25, 2012 5:01 pm


<edit op>TI MSP430 Launchpad</edit op>
<note>The code comment tone frequencies URL has been modified due to forum alert - "Your message contains banned spam words"</note>


It took some digging but the good ol' Internet pulled through for me :D

Enjoy!

Code: Select all | TOGGLE FULL SIZE
//code https://gist.github.com/1804108 20121125 @ 1339

//led for visualization (use 13 for built-in led)
int ledPin = 13;

//speaker connected to one of the pwm ports
int speakerPin = 9;

//tone frequencies http://home.(m-eye-t).bme.hu/~bako/tonecalc/tonecalc.htm
#define c 261
#define d 294
#define e 329
#define f 349
#define g 391
#define gS 415
#define a 440
#define aS 455
#define b 466
#define cH 523
#define cSH 554
#define dH 587
#define dSH 622
#define eH 659
#define fH 698
#define fSH 740
#define gH 784
#define gSH 830
#define aH 880

void setup()    
{    
  // sets the ledPin to be an output
  pinMode(ledPin, OUTPUT);
  //sets the speakerPin to be an output
  pinMode(speakerPin, OUTPUT);
}    
     
// run over and over again
void loop()
{
  march();
}    
     
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{
    //use led to visualize the notes being played
    digitalWrite(ledPin, HIGH);
   
    int x;    
    long delayAmount = (long)(1000000/frequencyInHertz);
    long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
    for (x=0;x<loopTime;x++)    
    {    
        digitalWrite(speakerPin,HIGH);
        delayMicroseconds(delayAmount);
        digitalWrite(speakerPin,LOW);
        delayMicroseconds(delayAmount);
    }    
   
    //set led back to low
    digitalWrite(ledPin, LOW);
       
    //delay to separate notes
    delay(20);
}    
     
void march()
{    
    //Sheet music http://www.musicnotes.com/sheetmusic/mtd.asp?ppn=MN0016254
    //time in ms
    //500 ms for a quart note
    beep(speakerPin, a, 500);
    beep(speakerPin, a, 500);     
    beep(speakerPin, a, 500);
    beep(speakerPin, f, 350);
    beep(speakerPin, cH, 150);
   
    beep(speakerPin, a, 500);
    beep(speakerPin, f, 350);
    beep(speakerPin, cH, 150);
    beep(speakerPin, a, 1000);
   
    beep(speakerPin, eH, 500);
    beep(speakerPin, eH, 500);
    beep(speakerPin, eH, 500);   
    beep(speakerPin, fH, 350);
    beep(speakerPin, cH, 150);
   
    beep(speakerPin, gS, 500);
    beep(speakerPin, f, 350);
    beep(speakerPin, cH, 150);
    beep(speakerPin, a, 1000);
   
    beep(speakerPin, aH, 500);
    beep(speakerPin, a, 350);
    beep(speakerPin, a, 150);
    beep(speakerPin, aH, 500);
    beep(speakerPin, gSH, 250);
    beep(speakerPin, gH, 250);
   
    beep(speakerPin, fSH, 125);
    beep(speakerPin, fH, 125);   
    beep(speakerPin, fSH, 250);
    delay(250);
    beep(speakerPin, aS, 250);   
    beep(speakerPin, dSH, 500); 
    beep(speakerPin, dH, 250); 
    beep(speakerPin, cSH, 250);
   
    beep(speakerPin, cH, 125); 
    beep(speakerPin, b, 125); 
    beep(speakerPin, cH, 250);     
    delay(250);
    beep(speakerPin, f, 125); 
    beep(speakerPin, gS, 500); 
    beep(speakerPin, f, 375); 
    beep(speakerPin, a, 125);
   
    beep(speakerPin, cH, 500);
    beep(speakerPin, a, 375); 
    beep(speakerPin, cH, 125);
    beep(speakerPin, eH, 1000);
   
    beep(speakerPin, aH, 500);
    beep(speakerPin, a, 350);
    beep(speakerPin, a, 150);
    beep(speakerPin, aH, 500);
    beep(speakerPin, gSH, 250);
    beep(speakerPin, gH, 250);
   
    beep(speakerPin, fSH, 125);
    beep(speakerPin, fH, 125);   
    beep(speakerPin, fSH, 250);
    delay(250);
    beep(speakerPin, aS, 250);   
    beep(speakerPin, dSH, 500); 
    beep(speakerPin, dH, 250); 
    beep(speakerPin, cSH, 250);
   
    beep(speakerPin, cH, 125); 
    beep(speakerPin, b, 125); 
    beep(speakerPin, cH, 250);     
    delay(250);
    beep(speakerPin, f, 250); 
    beep(speakerPin, gS, 500); 
    beep(speakerPin, f, 375); 
    beep(speakerPin, cH, 125);
           
    beep(speakerPin, a, 500);           
    beep(speakerPin, f, 375);           
    beep(speakerPin, c, 125);           
    beep(speakerPin, a, 1000);   
}
rkill
 
Posts: 2
Joined: Sun Sep 02, 2012 5:11 pm