Skip to content

Instantly share code, notes, and snippets.

@eznj
Last active September 26, 2023 18:24
Show Gist options
  • Save eznj/4736535 to your computer and use it in GitHub Desktop.
Save eznj/4736535 to your computer and use it in GitHub Desktop.
Arduino Star Wars Song
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
const int buzzerPin = 8;
const int ledPin1 = 12;
const int ledPin2 = 13;
int counter = 0;
void setup()
{
//Setup pin modes
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop()
{
//Play first section
firstSection();
//Play second section
secondSection();
beep(f, 250);
beep(gS, 500);
beep(f, 350);
beep(a, 125);
beep(cH, 500);
beep(a, 375);
beep(cH, 125);
beep(eH, 650);
delay(500);
//Repeat second section
secondSection();
beep(f, 250);
beep(gS, 500);
beep(f, 375);
beep(cH, 125);
beep(a, 500);
beep(f, 375);
beep(cH, 125);
beep(a, 650);
delay(650);
}
void beep(int note, int duration)
{
//Play tone on buzzerPin
tone(buzzerPin, note, duration);
//Play different LED depending on value of 'counter'
if(counter % 2 == 0)
{
digitalWrite(ledPin1, HIGH);
delay(duration);
digitalWrite(ledPin1, LOW);
}else
{
digitalWrite(ledPin2, HIGH);
delay(duration);
digitalWrite(ledPin2, LOW);
}
//Stop tone on buzzerPin
noTone(buzzerPin);
delay(50);
//Increment counter
counter++;
}
void firstSection()
{
beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
beep(eH, 500);
beep(eH, 500);
beep(eH, 500);
beep(fH, 350);
beep(cH, 150);
beep(gS, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
}
void secondSection()
{
beep(aH, 500);
beep(a, 300);
beep(a, 150);
beep(aH, 500);
beep(gSH, 325);
beep(gH, 175);
beep(fSH, 125);
beep(fH, 125);
beep(fSH, 250);
delay(325);
beep(aS, 250);
beep(dSH, 500);
beep(dH, 325);
beep(cSH, 175);
beep(cH, 125);
beep(b, 125);
beep(cH, 250);
delay(350);
}
@Andrew-rw
Copy link

Great! Thanks!

@eddycek
Copy link

eddycek commented Jan 4, 2015

yes! :)

@Dayof
Copy link

Dayof commented Feb 12, 2015

Omg, awesome!!!

@derfaq
Copy link

derfaq commented Jun 6, 2017

great job!

@livingmentor
Copy link

Found some errors in the frequencies. Corrected, below:

const int aS = 466;
const int b = 494;

@sumitrai23
Copy link

Unbelievable... thank you, that's going in my latest project :-)

@sepentaj
Copy link

sepentaj commented Jan 26, 2018

so best and useful! ; I used it for making other musics, please add .ino to end of name and remove **/ code for use easier.
but how can u set speed?

@LRH18
Copy link

LRH18 commented Feb 20, 2018

magnificent !!
how long did it take for you to make something like this .

@piotrekkmt
Copy link

does any of you know if there's an online generator of these frequencies? Or would it need to be done by trial and error?

@ShinyCastform
Copy link

http://pages.mtu.edu/~suits/notefreqs.html This is a link for frequencies!

@BaeJuneHyuck
Copy link

Thank you!

@Meterle
Copy link

Meterle commented Jun 4, 2018

Thanks 4 the share

@typ138
Copy link

typ138 commented Jul 6, 2018

Thanks alot!

@gre-vel
Copy link

gre-vel commented Jul 27, 2018

I made a project that includes your code:
https://youtu.be/0yEIdtyNa0U

Of course I linked your code

@bigseanmorris
Copy link

big sean approves

@cronos45
Copy link

I keep getting the error below, any help guys?

exit status 1 'firstSection' was not declared in this scope

@donepel
Copy link

donepel commented Sep 26, 2018

I needed try it!!! Thanks you

@mcs2k8
Copy link

mcs2k8 commented Dec 19, 2018

Great job!!!!! Thumbs up!

@MrCalico
Copy link

MrCalico commented Jan 6, 2019

Great Work NickSort. A Classic! Thanks.

@JackBauer76
Copy link

great, for my son was awesome, Thanks!

@HugoGybels
Copy link

damn cool thanks !

@veselin0
Copy link

Thank you very much for your code. It's Great!

@x225am
Copy link

x225am commented Jun 12, 2019

Great!!!

@WeldProNDT
Copy link

Great job!

@daveyg1987
Copy link

Hi I'm rather new to this, how can I adapt this code to incorporate a push button, that will allow it to play the full sequence once per press of the button, then wait until the button is pressed again? I've tried cutting and pasting the push button code segments from the example in the ide software labeled 'Button' but it causes all sorts of errors starting with "'firstsection' was not declared in this scope" can someone give me a basic understanding of how I need to relocate the existing code segments to get them to play nicely? Thanks!

@dmitryUX
Copy link

If someone is getting errors when verifying this code, try to delete the very first line:
**/

Hope this helps.

@free-o
Copy link

free-o commented Nov 11, 2020

it's make me happy. Thank you very much my friend!!

@Bryniac
Copy link

Bryniac commented Jan 10, 2021

Thank you so much I have been learning Arduino for the past 4 days so I have been messing around only with LEDs. Now, with this I adapted it and made my Arduino version of Blue Bird. here is the gist for those interested:
https://gist.github.com/Bryniac/0c03831108e087f7aff9148b11be803a

@eznj
Copy link
Author

eznj commented May 6, 2022

Thank you so much I have been learning Arduino for the past 4 days so I have been messing around only with LEDs. Now, with this I adapted it and made my Arduino version of Blue Bird. here is the gist for those interested: https://gist.github.com/Bryniac/0c03831108e087f7aff9148b11be803a

Thanks for sharing @Bryniac

@oscardominguez1
Copy link

Thank you

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