Skip to content

Instantly share code, notes, and snippets.

@tagliati
Created February 11, 2012 20:42
Show Gist options
  • Save tagliati/1804108 to your computer and use it in GitHub Desktop.
Save tagliati/1804108 to your computer and use it in GitHub Desktop.
Marcha imperial para arduino
int ledPin = 13;
//led for visualization (use 13 for built-in led)
int speakerPin = 11;
//speaker connected to one of the PWM ports
#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
//frequencies for the tones we're going to use
//used http://home.mit.bme.hu/~bako/tonecalc/tonecalc.htm to get these
void setup()
{
pinMode(ledPin, OUTPUT);
// sets the ledPin to be an output
pinMode(speakerPin, OUTPUT);
//sets the speakerPin to be an output
}
void loop() // run over and over again
{
march();
}
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{
digitalWrite(ledPin, HIGH);
//use led to visualize the notes being played
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);
}
digitalWrite(ledPin, LOW);
//set led back to low
delay(20);
//a little delay to make all notes sound separate
}
void march()
{
//for the sheet music see:
//http://www.musicnotes.com/sheetmusic/mtd.asp?ppn=MN0016254
//this is just a translation of said sheet music to frequencies / time in ms
//used 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);
//first bit
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);
//second bit...
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);
//start of the interesting bit
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);
//more interesting stuff (this doesn't quite get it right somehow)
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);
//repeat... repeat
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);
//and we're done \ó/
}
@pjgpetecodes
Copy link

Here's one that uses a couple of buttons (and leds) to choose between either the Imperial March or the Theme Tune...

int ledPin = 13;

int redLEDPin = 14;
int blueLEDPin = 12;

// led for visualization (use 13 for built-in led)

int speakerPin = 11;
// speaker connected to one of the PWM ports

int button1Pin = 0;
int button2Pin = 4;

bool waitingForButton1Release = false;
bool waitingForButton2Release = false;
  
#define c 261
#define d 294
#define e 329
#define f 349
#define fS 370
#define g 391
#define gS 415
#define a 440
#define aS 466
#define b 494
#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
// frequencies for the tones we're going to use
// used http://home.mit.bme.hu/~bako/tonecalc/tonecalc.htm to get these

void setup()
{
	pinMode(ledPin, OUTPUT);
	pinMode(redLEDPin, OUTPUT);
	pinMode(blueLEDPin, OUTPUT);
	
	// sets the ledPin to be an output

	pinMode(speakerPin, OUTPUT);
	// sets the speakerPin to be an output
  
  	pinMode(button1Pin, INPUT_PULLUP);  	
  	pinMode(button2Pin, INPUT_PULLUP);

	digitalWrite(redLEDPin, LOW);
	digitalWrite(blueLEDPin, LOW);
  	
}

void loop()
{  
  
    if (digitalRead(button1Pin) == LOW)
    {
      	waitingForButton1Release = true;
    }
  	else if (waitingForButton1Release == true)
    {
      	waitingForButton1Release = false;
      	startThemeTune();
    }  
  
    if (digitalRead(button2Pin) == LOW)
    {
      	waitingForButton2Release = true;
    }
  	else if (waitingForButton2Release == true)
    {
      	waitingForButton2Release = false;
      	startImperialMarch();
    }    
	
}

void beep(int speakerPin, int frequencyInHertz, long timeInMilliseconds)
{
	digitalWrite(ledPin, HIGH);
	// use led to visualize the notes being played

	long delayAmount = (long)(1000000 / frequencyInHertz);
	// the lesser delay the higher the note

	long timeInSeconds = timeInMilliseconds * 1000;

	long beepDuration = (long)(timeInSeconds / (delayAmount * 2));

	for (int i = 0; i < beepDuration; i++)
	{
		digitalWrite(speakerPin, HIGH);
		delayMicroseconds(delayAmount);
		digitalWrite(speakerPin, LOW);
		delayMicroseconds(delayAmount);
	}

	digitalWrite(ledPin, LOW);
	// set led back to low

	delay(20);
	// a little delay to make all notes sound separate
}

void startImperialMarch()
{
  
  	digitalWrite(redLEDPin, HIGH);
	
	// for the sheet music see:
	// http://www.musicnotes.com/sheetmusic/mtd.asp?ppn=MN0016254
	// this is just a translation of said sheet music to frequencies / time in ms
	// used 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);
	// first bit

	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);
	//second bit...

	beep(speakerPin, aH, 500);
	beep(speakerPin, a, 350);
	beep(speakerPin, a, 150);
	beep(speakerPin, aH, 500);
	beep(speakerPin, gSH, 350);
	beep(speakerPin, gH, 125);

	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, 350);
	beep(speakerPin, cSH, 125);
	// start of the interesting bit

	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, a, 125);

	beep(speakerPin, cH, 500);
	beep(speakerPin, a, 375);
	beep(speakerPin, cH, 125);
	beep(speakerPin, eH, 1000);
	// more interesting stuff

	beep(speakerPin, aH, 500);
	beep(speakerPin, a, 350);
	beep(speakerPin, a, 150);
	beep(speakerPin, aH, 500);
	beep(speakerPin, gSH, 350);
	beep(speakerPin, gH, 125);

	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, 350);
	beep(speakerPin, cSH, 125);
	// repeat... repeat

	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, cH, 125);
	beep(speakerPin, a, 1000);
	// and we're done \ó/
  
  	digitalWrite(redLEDPin, LOW);
	
}

void startThemeTune()
{
	//http://thirdstreetmusic.blogspot.com/2012/04/recorder-ensemble-star-wars-first.html

	digitalWrite(blueLEDPin, HIGH);
	
  	beep(speakerPin, d, 250);
	beep(speakerPin, d, 250);
	beep(speakerPin, d, 250);

	themeTunePart1();
  
  	delay(350);	

	beep(speakerPin, d, 125);
  
  	delay(25);
      
	beep(speakerPin, d, 250);

	delay(250);
	
	themeTunePart1();

	delay(250);
	
	themeTunePart2();

	delay(50);
  
	themeTunePart3();
  
  	digitalWrite(blueLEDPin, LOW);	

}

void themeTunePart1()
{
	beep(speakerPin, g, 1000);
	beep(speakerPin, dH, 1000);

	delay(250);

	beep(speakerPin, cH, 250);
	beep(speakerPin, b, 250);
	beep(speakerPin, a, 250);

	beep(speakerPin, gH, 1000);
	beep(speakerPin, dH, 1000);

	delay(125);

	beep(speakerPin, cH, 250);
	beep(speakerPin, b, 250);
	beep(speakerPin, a, 250);

	beep(speakerPin, gH, 1000);
	beep(speakerPin, dH, 1000);

	delay(125);

	beep(speakerPin, cH, 250);
	beep(speakerPin, b, 250);
	beep(speakerPin, cH, 250);

	beep(speakerPin, a, 1000);
}

void themeTunePart2() 
{
	beep(speakerPin, d, 500);

	delay(25);

	beep(speakerPin, d, 125);
	beep(speakerPin, e, 1000);

  	delay(25);

	beep(speakerPin, e, 125);
	
  	delay(25);

	beep(speakerPin, cH, 250);
	beep(speakerPin, b, 250);
	beep(speakerPin, a, 250);
	beep(speakerPin, g, 250);

	delay(25);

	beep(speakerPin, g, 175);
	beep(speakerPin, a, 175);
	beep(speakerPin, b, 175);
	beep(speakerPin, a, 250);

  	delay(25);
	
	beep(speakerPin, e, 250);
	beep(speakerPin, fS, 500);
  
  	delay(25);

  	beep(speakerPin, d, 500);

	delay(25);

	beep(speakerPin, d, 125);
	beep(speakerPin, e, 1000);

	delay(25);

	beep(speakerPin, e, 125);
  
	delay(75);

	beep(speakerPin, g, 250);
	beep(speakerPin, b, 250);
	beep(speakerPin, a, 250);
	beep(speakerPin, g, 250);
  
	delay(25);

  	beep(speakerPin, dH, 500);
	beep(speakerPin, a, 125);
	beep(speakerPin, a, 1000);	
	
	delay(350);
  	
  	beep(speakerPin, d, 500);

	delay(25);

	beep(speakerPin, d, 125);
	beep(speakerPin, e, 1000);

  	delay(25);

	beep(speakerPin, e, 125);
	
  	delay(25);

	beep(speakerPin, cH, 250);
	beep(speakerPin, b, 250);
	beep(speakerPin, a, 250);
	beep(speakerPin, g, 250);

	delay(75);
  
  	beep(speakerPin, g, 175);
	beep(speakerPin, a, 175);
	beep(speakerPin, b, 175);
	beep(speakerPin, a, 250);
  
	delay(50);
	
	beep(speakerPin, e, 250);
	beep(speakerPin, fS, 500);
}

void themeTunePart3() {
	
  	beep(speakerPin, dH, 125);

	delay(250);

	beep(speakerPin, dH, 125);  
	beep(speakerPin, gH, 250);
	beep(speakerPin, fH, 250);
	beep(speakerPin, dSH, 250);
	beep(speakerPin, dH, 250);
  	beep(speakerPin, cH, 250);
  	beep(speakerPin, aS, 250);
  	beep(speakerPin, a, 250);
  	beep(speakerPin, g, 250);
  	
	delay(25);
  
  	beep(speakerPin, dH, 500);
  
  	delay(125);
  
  	beep(speakerPin, d, 250);
  
  	delay(125);
 
  	beep(speakerPin, d, 125);
  	beep(speakerPin, d, 1000);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment