You are on page 1of 8

/*

SparkFunInventor'sKit
Examplesketch11

BUZZER

Usethebuzzertoplayasong!

ThebuzzerinyourInventor'sKitisanelectromechanical
componentyoucanusetomakenoise.Insidethebuzzerisa
coilofwireandasmallmagnet.Whencurrentflowsthrough
thecoil,itbecomesmagnetizedandpullstowardsthemagnet,
creatingatiny"click".Whenyoudothisthousandsoftimes
persecond,youcreatetones.

TheArduinohasabuiltincommandcalledtone()whichclicks
thebuzzeratacertainfrequency.Thissketchknowsthe
frequenciesofthecommonnotes,allowingyoutocreatesongs.
We'renevergoingtoletyoudown!

Hardwareconnections:

Thebuzzerhastwopins.Oneispositiveandoneisnegative.
Thepostitivepinismarkedbya"+"symbolonboththetop
andbottomofthebuzzer.

ConnectthepositivepintoArduinodigitalpin9.
(NotethatthismustbeaPWMpin.)
ConnectthenegativepintoGND.

Tip:ifthebuzzerdoesn'tfitintothebreadboardeasily,
tryrotatingitslightlytofitintodiagonalholes.

ThissketchwaswrittenbySparkFunElectronics,
withlotsofhelpfromtheArduinocommunity.
(ThissketchwasoriginallydevelopedbyD.CuartiellesforK3)
Thiscodeiscompletelyfreeforanyuse.
Visithttp://learn.sparkfun.com/products/2forSIKinformation.
Visithttp://www.arduino.cctolearnabouttheArduino.

Version2.06/2012MDG
*/

/*
Thissketchusesthebuzzertoplaysongs.
TheArduino'stone()commandwillplaynotesofagivenfrequency.
We'llprovideafunctionthattakesinnotecharacters(ag),
andreturnsthecorrespondingfrequencyfromthistable:

note frequency
c262Hz
d294Hz
e330Hz
f349Hz
g392Hz
a440Hz
b494Hz
C523Hz

Formoreinformation,seehttp://arduino.cc/en/Tutorial/Tone
*/

constintbuzzerPin=9

//We'llsetupanarraywiththenoteswewanttoplay
//changethesevaluestomakedifferentsongs!

//Lengthmustequalthetotalnumberofnotesandspaces

constintsongLength=35

//Notesisanarrayoftextcharacterscorrespondingtothenotes
//inyoursong.Aspacerepresentsarest(notone)

charnotes[]="fedAgecaCDccGGAefggafbaafcecgga"//aspacerepresentsarest

//Beatsisanarrayofvaluesforeachnoteandrest.
//A"1"representsaquarternote,2ahalfnote,etc.
//Don'tforgetthattherests(spaces)needalengthaswell.

intbeats[]={1,1,1,3,2,1,1,2,1,1,7,5,1,1,1,5,2,2,5,1,2,2,3,3,1,1,1,2,1,1,3,1,1,2,100}

//Thetempoishowfasttoplaythesong.
//Tomakethesongplayfaster,decreasethisvalue.

inttempo=200

voidsetup()
{
pinMode(buzzerPin,OUTPUT)
}

voidloop()
{
inti,duration

for(i=0i<songLengthi++)//stepthroughthesongarrays
{
duration=beats[i]*tempo//lengthofnote/restinms

if(notes[i]=='')//isthisarest?
{
delay(duration)//thenpauseforamoment
}
else//otherwise,playthenote
{
tone(buzzerPin,frequency(notes[i]),duration)
delay(duration)//waitfortonetofinish
}
delay(tempo/10)//briefpausebetweennotes
}

//Weonlywanttoplaythesongonce,sowe'llpauseforever:
while(true){}
//Ifyou'dlikeyoursongtoplayoverandover,
//removetheabovestatement
}

intfrequency(charnote)
{
//Thisfunctiontakesanotecharacter(ag),andreturnsthe
//correspondingfrequencyinHzforthetone()function.

inti
constintnumNotes=13//numberofnoteswe'restoring

//Thefollowingarraysholdthenotecharactersandtheir
//correspondingfrequencies.Thelast"C"noteisuppercase
//toseparateitfromthefirstlowercase"c".Ifyouwantto
//addmorenotes,you'llneedtouseuniquecharacters.

//Forthe"char"(character)type,weputsinglecharacters
//insinglequotes.

charnames[]={'G','F','A','c','d','e','f','g','a','b','C','D','E'}
intfrequencies[]={196.00,174.61,220,246.94,294,330,349,392,440,494,523.25,587.33,
659.25}

//Nowwe'llsearchthroughthelettersinthearray,andif
//wefindit,we'llreturnthefrequencyforthatnote.

for(i=0i<numNotesi++)//Stepthroughthenotes
{
if(names[i]==note)//Isthistheone?
{
return(frequencies[i])//Yes!Returnthefrequency
}
}
return(0)//Welookedthrougheverythinganddidn'tfindit,
//butwestillneedtoreturnavalue,soreturn0.
}

#include<LiquidCrystal.h>//openinglcdlibrary
#defineBUTTON_PINA5//showsthebuttonpin
#defineMAX_ball10//showshowmanycatus'therecanbe

intbuttonState=0
//thelocationofthebuttonifnotpressed

intiball2[MAX_ball]={30,35,40,45,50,55,60,65,70,75}
//onearrayofspawnedballsandthenumbersrepresenttime

inticounter=0

intcharLocation=0
//wherethecharacterstarts

LiquidCrystallcd(12,11,5,4,3,2)//theconnectionsweusedonthebreadboard

byteplayer[8]={
B00000,
B00111,
B10111,
B01010,
B00111,
B00010,
B00010,
B00101
}

byteball[8]={
B00000,
B00000,
B00000,
B00000,
B00000,
B01110,
B01110,
B01110

bytehoop[8]={
B10000,
B01111,
B10110,
B10000,
B10000,

B10000,
B10000,
B10000
}

//thesecodesshowwherethelcdwilldisplaytheblocksoneachsquare(ball,hoop,
player)

voidsetup(){//thecodethatisusedonlyonce
lcd.begin(16,2)//showinghowbigthelcdis(16by2)
lcd.createChar(1,player)
lcd.createChar(1,hoop)
lcd.createChar(0,ball)

pinMode(BUTTON_PIN,INPUT)

voidloop(){//thecodethatcanbeusedmorethanonce
inti,duration

buttonState=digitalRead(BUTTON_PIN)
delay(200)
lcd.clear()
icounter++
if((icounter%2)==1){//thisisthecodeforthejumpingofthelizard.Itshowsthatwhenthe
buttonispresseditwillmoveuponeandtheremainderwhendividedbytwowilldotheother
command.
if(buttonState==LOW){
charLocation=0
lcd.setCursor(1,charLocation)
lcd.write(byte(1))
for(intk=1k<MAX_ballk++)
{

iball2[k]=iball2[k]1

if(iball2[k]<=15){
lcd.setCursor(iball2[k]+1,1)
lcd.write(byte(0))
if(iball2[k]<1){

iball2[k]=103
}
}

}
}
else{
charLocation=1
lcd.setCursor(1,charLocation)
lcd.write(byte(1))
for(intk=1k<MAX_ballk++)
{

iball2[k]=iball2[k]1
iball2[k]+iball2[k]1


if(iball2[k]<=15){
lcd.setCursor(iball2[k],0)
lcd.write(byte(0))
if(iball2[k]<1){
iball2[k]=103
}
}
if(iball2[k]==0&&charLocation==0){
//thiscodesaysthatifacactusandalizardareinthesamespaceitwillcancelout,therefore
"killing"thelizard
lcd.clear()
lcd.setCursor(0,1)
lcd.write(byte(2))
delay(25)
lcd.clear()
lcd.setCursor(3,0)
lcd.print("He'sonFire!")
delay(2000)
lcd.clear()
lcd.print("NoGood!")
lcd.setCursor(0,8)
lcd.print(millis()/1000)
delay(3000)
lcd.clear()
lcd.print("PressResetfor")

lcd.setCursor(3,1)
lcd.print("AnotherGame")
delay(100000)
lcd.clear()
}

if(iball2[k]==0&&charLocation==1){
//thiscodesaysthatifacactusandalizardareinthesamespaceitwillcancelout,therefore
"killing"thelizard
lcd.clear()
lcd.setCursor(0,1)
lcd.write(byte(2))
delay(25)
lcd.clear()
lcd.setCursor(3,0)
lcd.print("He'sonfire!")
delay(2000)
lcd.clear()
lcd.print("Nogood!")
lcd.setCursor(0,8)
lcd.print(millis()/1000)
delay(3000)
lcd.clear()
lcd.print("PressReset")
lcd.setCursor(3,1)
lcd.print("foranothergame")
delay(100000)
lcd.clear()
//thecodefortheendoncethelizarddies.thiscodeenablesthelcdtodisplaythewordswitha
slighdelaybetweeneachone.
iball2[k]=103

}
}
}
}}

You might also like