Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arduino Star Wars Song for Piezo
**/
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();
//Variant 1
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();
//Variant 2
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);
}
@MasterPreston

This comment has been minimized.

Copy link

commented Feb 11, 2014

This is sooooo great. #luvit

@napoleaninlondon

This comment has been minimized.

Copy link

commented Mar 4, 2014

Is the tone function defined in any library?

@mgmonteleone

This comment has been minimized.

Copy link

commented May 4, 2014

tone is a built in function ...http://arduino.cc/en/reference/tone

@VitorCoelho

This comment has been minimized.

Copy link

commented May 16, 2014

Amazing! Tested and worked correctly. I imagine how long it took you to do this hahahah

@rhettc

This comment has been minimized.

Copy link

commented Aug 23, 2014

Awesome!

@WinKILLER

This comment has been minimized.

Copy link

commented Oct 3, 2014

Great! Thanks!

@eddycek

This comment has been minimized.

Copy link

commented Jan 4, 2015

yes! :)

@Dayof

This comment has been minimized.

Copy link

commented Feb 12, 2015

Omg, awesome!!!

@derfaq

This comment has been minimized.

Copy link

commented Jun 6, 2017

great job!

@livingmentor

This comment has been minimized.

Copy link

commented Nov 27, 2017

Found some errors in the frequencies. Corrected, below:

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

@ydyd8yctdyc7d7yvy8vyur

This comment has been minimized.

Copy link

commented Dec 6, 2017

doesnt work it sucks and is horrid

@sumitrai23

This comment has been minimized.

Copy link

commented Dec 13, 2017

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

@sepentaj

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Feb 20, 2018

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

@jemoeder123456

This comment has been minimized.

Copy link

commented Feb 26, 2018

u stupid stupid cow u cowed my cow by cowing

@jemoeder123456

This comment has been minimized.

Copy link

commented Feb 26, 2018

kom dan 1 vs 1 mijnkreft

@piotrekkmt

This comment has been minimized.

Copy link

commented Mar 18, 2018

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?

@BradyAtkinson

This comment has been minimized.

Copy link

commented Apr 23, 2018

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

@BaeJuneHyuck

This comment has been minimized.

Copy link

commented Apr 24, 2018

Thank you!

@Meterle

This comment has been minimized.

Copy link

commented Jun 4, 2018

Thanks 4 the share

@typ138

This comment has been minimized.

Copy link

commented Jul 6, 2018

Thanks alot!

@DODO258

This comment has been minimized.

Copy link

commented Jul 27, 2018

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

Of course I linked your code

@bigseanmorris

This comment has been minimized.

Copy link

commented Sep 14, 2018

big sean approves

@cronos45

This comment has been minimized.

Copy link

commented Sep 21, 2018

I keep getting the error below, any help guys?

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

@donepel

This comment has been minimized.

Copy link

commented Sep 26, 2018

I needed try it!!! Thanks you

@mcs2k8

This comment has been minimized.

Copy link

commented Dec 19, 2018

Great job!!!!! Thumbs up!

@MrCalico

This comment has been minimized.

Copy link

commented Jan 6, 2019

Great Work NickSort. A Classic! Thanks.

@JackBauer76

This comment has been minimized.

Copy link

commented Feb 28, 2019

great, for my son was awesome, Thanks!

@Chamoniix

This comment has been minimized.

Copy link

commented Mar 9, 2019

damn cool thanks !

@veselin0

This comment has been minimized.

Copy link

commented Apr 24, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.