Vous êtes sur la page 1sur 14

//RealmOfTheMadCasey

#defineNOTE_B031
#defineNOTE_C133
#defineNOTE_CS135
#defineNOTE_D137
#defineNOTE_DS139
#defineNOTE_E141
#defineNOTE_F144
#defineNOTE_FS146
#defineNOTE_G149
#defineNOTE_GS152
#defineNOTE_A155
#defineNOTE_AS158
#defineNOTE_B162
#defineNOTE_C265
#defineNOTE_CS269
#defineNOTE_D273
#defineNOTE_DS278
#defineNOTE_E282
#defineNOTE_F287
#defineNOTE_FS293
#defineNOTE_G298
#defineNOTE_GS2104
#defineNOTE_A2110
#defineNOTE_AS2117
#defineNOTE_B2123
#defineNOTE_C3131
#defineNOTE_CS3139
#defineNOTE_D3147
#defineNOTE_DS3156
#defineNOTE_E3165
#defineNOTE_F3175
#defineNOTE_FS3185
#defineNOTE_G3196
#defineNOTE_GS3208
#defineNOTE_A3220
#defineNOTE_AS3233
#defineNOTE_B3247
#defineNOTE_C4262
#defineNOTE_CS4277
#defineNOTE_D4294
#defineNOTE_DS4311
#defineNOTE_E4330

#defineNOTE_F4349
#defineNOTE_FS4370
#defineNOTE_G4392
#defineNOTE_GS4415
#defineNOTE_A4440
#defineNOTE_AS4466
#defineNOTE_B4494
#defineNOTE_C5523
#defineNOTE_CS5554
#defineNOTE_D5587
#defineNOTE_DS5622
#defineNOTE_E5659
#defineNOTE_F5698
#defineNOTE_FS5740
#defineNOTE_G5784
#defineNOTE_GS5831
#defineNOTE_A5880
#defineNOTE_AS5932
#defineNOTE_B5988
#defineNOTE_C61047
#defineNOTE_CS61109
#defineNOTE_D61175
#defineNOTE_DS61245
#defineNOTE_E61319
#defineNOTE_F61397
#defineNOTE_FS61480
#defineNOTE_G61568
#defineNOTE_GS61661
#defineNOTE_A61760
#defineNOTE_AS61865
#defineNOTE_B61976
#defineNOTE_C72093
#defineNOTE_CS72217
#defineNOTE_D72349
#defineNOTE_DS72489
#defineNOTE_E72637
#defineNOTE_F72794
#defineNOTE_FS72960
#defineNOTE_G73136
#defineNOTE_GS73322
#defineNOTE_A73520
#defineNOTE_AS73729
#defineNOTE_B73951

#defineNOTE_C84186
#defineNOTE_CS84435
#defineNOTE_D84699
#defineNOTE_DS84978

#defineCHOICE_OFF
0//UsedtocontrolLEDs
#defineCHOICE_NONE
0//Usedtocheckbuttons
#defineCHOICE_RED(1<<0)
#defineCHOICE_GREEN
(1<<1)
#defineCHOICE_BLUE(1<<2)
#defineCHOICE_YELLOW(1<<3)

#defineLED_RED 10
#defineLED_GREEN3
#defineLED_BLUE 13
#defineLED_YELLOW5

//Buttonpindefinitions

//Buzzerpindefinitions
#defineBUZZER14
#defineBUZZER27

//Definegameparameters
#defineROUNDS_TO_WIN 15//Numberofroundstosuccesfullyrememberbeforeyouwin.
13isdoable.
#defineENTRY_TIME_LIMIT1800//Amountoftimetopressabuttonbeforegametimesout.
1800ms=1.8sec

#defineMODE_MEMORY0
#defineMODE_BATTLE1
#defineMODE_BEEGEES2
intBUTTON_RED
intBUTTON_GREEN
intBUTTON_BLUE
intBUTTON_YELLOW

//Gamestatevariables
bytegameMode=MODE_MEMORY//Bydefault,let'splaythememorygame
bytegameBoard[32]//Containsthecombinationofbuttonsasweadvance
bytegameRound=0//Countsthenumberofsuccesfulroundstheplayerhasmadeitthrough
intvalue


voidsetup()
{
randomSeed(analogRead(1))//settingupawaytogettrulyrandomnumbers
value=random(4) //generatinganumberrandomlybetween0and6(RNJesus),andmaking
avariableequalit
if(value==0){
//ifthevariableis0,dothis
BUTTON_RED=6 //maketheinputassociatedwithbuttonred6
BUTTON_GREEN=12//maketheinputassociatedwithbuttongreen12
BUTTON_BLUE=2 //maketheinputassociatedwithbuttonblue2
BUTTON_YELLOW=9//maketheinputassociatedwithbuttonyellow9
}
if(value==1){
BUTTON_RED=12
BUTTON_GREEN=9
BUTTON_BLUE=6
BUTTON_YELLOW=2
}
if(value==2){
BUTTON_RED=12
BUTTON_GREEN=6
BUTTON_BLUE=9
BUTTON_YELLOW=2
}
if(value==3){
BUTTON_RED=2
BUTTON_GREEN=9
BUTTON_BLUE=6
BUTTON_YELLOW=12
}

//Setuphardwareinputs/outputs.Thesepinsaredefinedinthehardware_versionsheaderfile

//Enablepullupsoninputs
pinMode(BUTTON_RED,INPUT_PULLUP)
pinMode(BUTTON_GREEN,INPUT_PULLUP)
pinMode(BUTTON_BLUE,INPUT_PULLUP)
pinMode(BUTTON_YELLOW,INPUT_PULLUP)

//setsallthepinsasoutputs
pinMode(LED_RED,OUTPUT)

pinMode(LED_GREEN,OUTPUT)

pinMode(LED_BLUE,OUTPUT)
pinMode(LED_YELLOW,OUTPUT)

pinMode(BUZZER1,OUTPUT)
pinMode(BUZZER2,OUTPUT)

//Modechecking
gameMode=MODE_MEMORY//Bydefault,we'regoingtoplaythememorygame

//Checktoseeifthelowerrightbuttonispressed
if(checkButton()==CHOICE_YELLOW)play_beegees()

//Checktoseeifupperrightbuttonispressed
if(checkButton()==CHOICE_GREEN)
{
gameMode=MODE_BATTLE//Putgameintobattlemode

//Turnontheupperright(green)LED
setLEDs(CHOICE_GREEN)
toner(CHOICE_GREEN,150)

setLEDs(CHOICE_RED|CHOICE_BLUE|CHOICE_YELLOW)//Turnontheother
LEDsuntilyoureleasebutton

while(checkButton()!=CHOICE_NONE)//Waitforusertostoppressingbutton

//Nowdonothing.Battlemodewillbeservicedinthemainroutine
}

play_torture()//Aftersetupiscomplete,sayhellototheworld
}

voidloop()
{
attractMode()//Blinklightswhilewaitingforusertopressabutton

//Indicatethestartofgameplay
setLEDs(CHOICE_RED|CHOICE_GREEN|CHOICE_BLUE|CHOICE_YELLOW)//Turnall
LEDson
delay(1000)
setLEDs(CHOICE_OFF)//TurnoffLEDs
delay(250)

if(gameMode==MODE_MEMORY)
{
//Playmemorygameandhandleresult
if(play_memory()==true)

play_torture()//Playerwon,playwinnertones
else

play_loser()//Playerlost,playlosertones
}

if(gameMode==MODE_BATTLE)
{
play_battle()//Playgameuntilsomeoneloses

play_loser()//Playerlost,playlosertones
}
}

//==============================
//Thefollowingfunctionsarerelatedtogameplayonly

//Playtheregularmemorygame
//Returns0ifplayerloses,or1ifplayerwins
booleanplay_memory(void)
{
randomSeed(millis())//Seedtherandomgeneratorwithrandomamountofmillis()

gameRound=0//Resetthegametothebeginning

while(gameRound<ROUNDS_TO_WIN)
{
add_to_moves()//Addabuttontothecurrentmoves,thenplaythemback

playMoves()//Playbackthecurrentgameboard

//Thenrequiretheplayertorepeatthesequence.
for(bytecurrentMove=0currentMove<gameRoundcurrentMove++)
{

bytechoice=wait_for_button()//Seewhatbuttontheuserpresses

if(choice==0)returnfalse//Ifwaittimedout,playerloses

if(choice!=gameBoard[currentMove])returnfalse//Ifthechoiceisincorect,player
loses

delay(1000)//Playerwascorrect,delaybeforeplayingmoves
}

returntrue//Playermadeitthroughalltheroundstowin!
}

//Playthespecial2playerbattlemode
//Aplayerbeginsbypressingabuttonthenhandingittotheotherplayer
//Thatplayerrepeatsthebuttonandaddsone,thenpassesback.
//Thisfunctionreturnswhensomeoneloses
booleanplay_battle(void)
{
gameRound=0//Resetthegameframebacktooneframe

while(1)//Loopuntilsomeonefails
{
bytenewButton=wait_for_button()//Waitforusertoinputnextmove
gameBoard[gameRound++]=newButton//Addthisnewbuttontothegamearray

//Thenrequiretheplayertorepeatthesequence.
for(bytecurrentMove=0currentMove<gameRoundcurrentMove++)
{

bytechoice=wait_for_button()

if(choice==0)returnfalse//Ifwaittimedout,playerloses.

if(choice!=gameBoard[currentMove])returnfalse//Ifthechoiceisincorect,player
loses.
}

delay(100)//Givetheuseranextra100mstohandthegametotheotherplayer
}

returntrue//Weshouldnevergethere
}

//Playsthecurrentcontentsofthegamemoves
voidplayMoves(void)
{
for(bytecurrentMove=0currentMove<gameRoundcurrentMove++)
{

toner(gameBoard[currentMove],150)

//Waitsomeamountoftimebetweenbuttonplayback
//Shortenthistomakegameharder
delay(150)//150workswell.75getsfast.
}
}

//Addsanewrandombuttontothegamesequence,bysamplingthetimer
voidadd_to_moves(void)
{
bytenewButton=random(0,4)//min(included),max(exluded)

//Wehavetoconvertthisnumber,0to3,toCHOICEs
if(newButton==0)newButton=CHOICE_RED
elseif(newButton==1)newButton=CHOICE_GREEN
elseif(newButton==2)newButton=CHOICE_BLUE
elseif(newButton==3)newButton=CHOICE_YELLOW

gameBoard[gameRound++]=newButton//Addthisnewbuttontothegamearray
}

//==============================
//Thefollowingfunctionscontrolthehardware

//LightsagivenLEDs
//PassinabytethatismadeupfromCHOICE_RED,CHOICE_YELLOW,etc
voidsetLEDs(byteleds)
{
if((leds&CHOICE_RED)!=0)
digitalWrite(LED_RED,HIGH)
else
digitalWrite(LED_RED,LOW)

if((leds&CHOICE_GREEN)!=0)
digitalWrite(LED_GREEN,HIGH)
else
digitalWrite(LED_GREEN,LOW)

if((leds&CHOICE_BLUE)!=0)
digitalWrite(LED_BLUE,HIGH)
else
digitalWrite(LED_BLUE,LOW)


if((leds&CHOICE_YELLOW)!=0)
digitalWrite(LED_YELLOW,HIGH)
else
digitalWrite(LED_YELLOW,LOW)
}

//Waitforabuttontobepressed.
//ReturnsoneofLEDcolors(LED_RED,etc.)ifsuccessful,0iftimedout
bytewait_for_button(void)
{
longstartTime=millis()//Rememberthetimewestartedthethisloop

while((millis()startTime)<ENTRY_TIME_LIMIT)//Loopuntiltoomuchtimehaspassed
{
bytebutton=checkButton()

if(button!=CHOICE_NONE)
{

toner(button,150)//Playthebuttontheuserjustpressed

while(checkButton()!=CHOICE_NONE)//Nowlet'swaitforusertoreleasebutton

delay(10)//Thishelpswithdebouncingandaccidentaldoubletaps

returnbutton
}

returnCHOICE_NONE//Ifwegethere,we'vetimedout!
}

//Returnsa'1'bitinthepositioncorrespondingtoCHOICE_RED,CHOICE_GREEN,etc.
bytecheckButton(void)
{
if(digitalRead(BUTTON_RED)==0)return(CHOICE_RED)
elseif(digitalRead(BUTTON_GREEN)==0)return(CHOICE_GREEN)
elseif(digitalRead(BUTTON_BLUE)==0)return(CHOICE_BLUE)
elseif(digitalRead(BUTTON_YELLOW)==0)return(CHOICE_YELLOW)

return(CHOICE_NONE)//Ifnobuttonispressed,returnnone
}


//LightanLEDandplaytone
//Red,upperleft:
440Hz2.272ms1.136mspulse
//Green,upperright:880Hz1.136ms0.568mspulse
//Blue,lowerleft:
587.33Hz1.702ms0.851mspulse
//Yellow,lowerright:784Hz1.276ms0.638mspulse
voidtoner(bytewhich,intbuzz_length_ms)
{
setLEDs(which)//TurnonagivenLED

//PlaythesoundassociatedwiththegivenLED
switch(which)
{
caseCHOICE_RED:

buzz_sound(buzz_length_ms,1136)

break
caseCHOICE_GREEN:

buzz_sound(buzz_length_ms,568)

break
caseCHOICE_BLUE:

buzz_sound(buzz_length_ms,851)

break
caseCHOICE_YELLOW:

buzz_sound(buzz_length_ms,638)

break
}

setLEDs(CHOICE_OFF)//TurnoffallLEDs
}

//Togglebuzzereverybuzz_delay_us,foradurationofbuzz_length_ms.
voidbuzz_sound(intbuzz_length_ms,intbuzz_delay_us)
{
//Converttotalplaytimefrommillisecondstomicroseconds
longbuzz_length_us=buzz_length_ms*(long)1000

//Loopuntiltheremainingplaytimeislessthanasinglebuzz_delay_us
while(buzz_length_us>(buzz_delay_us*2))
{
buzz_length_us=buzz_delay_us*2//Decreasetheremainingplaytime

//Togglethebuzzeratvariousspeeds
digitalWrite(BUZZER1,LOW)

digitalWrite(BUZZER2,HIGH)
delayMicroseconds(buzz_delay_us)

digitalWrite(BUZZER1,HIGH)
digitalWrite(BUZZER2,LOW)
delayMicroseconds(buzz_delay_us)
}
}

//Playthewinnersoundandlights
voidplay_torture(void)
{
setLEDs(CHOICE_GREEN|CHOICE_BLUE)
winner_sound()
setLEDs(CHOICE_RED|CHOICE_YELLOW)
winner_sound()
setLEDs(CHOICE_GREEN|CHOICE_BLUE)
winner_sound()
setLEDs(CHOICE_RED|CHOICE_YELLOW)
winner_sound()
}

//Playthewinnersound
//Thisisjustaunique(annoying)soundwecameupwith,thereisnomagictoit
voidwinner_sound(void)//brokenresetbutton
{
buzz_sound(25,750)//playashort,lowerpitchednote
delay(100)//waitatenthofasecond
buzz_sound(50,650)//playaslightlylonger,higherpitchednote
delay(100)//waitatenthofasecond
buzz_sound(75,550)//playaslightlylonger,higherpitchednote
delay(100)//waitatenthofasecond
buzz_sound(100,400)//playaslightlylonger,higherpitchednote
delay(125)//waitaneigthofasecond
buzz_sound(125,350)//playaslightlylonger,higherpitchednote
return//gobacktothecodethatinitiatedthishorror
}

//Playthenormalsound/lights
voidplay_loser(void)
{

setLEDs(CHOICE_RED|CHOICE_GREEN)
buzz_sound(255,1570)

setLEDs(CHOICE_BLUE|CHOICE_YELLOW)
buzz_sound(1000,500)

setLEDs(CHOICE_RED|CHOICE_GREEN)
buzz_sound(25,2000)

setLEDs(CHOICE_BLUE|CHOICE_YELLOW)
buzz_sound(255,150)
delay(110)
buzz_sound(500,675)
delay(110)
buzz_sound(500,675)
delay(110)
buzz_sound(500,675)
}

//Showan"attractmode"displaywhilewaitingforusertopressbutton.
voidattractMode(void)
{
while(1)
{
setLEDs(CHOICE_RED)
delay(100)
if(checkButton()!=CHOICE_NONE)return

setLEDs(CHOICE_BLUE)
delay(100)
if(checkButton()!=CHOICE_NONE)return

setLEDs(CHOICE_GREEN)
delay(100)
if(checkButton()!=CHOICE_NONE)return

setLEDs(CHOICE_YELLOW)
delay(100)
if(checkButton()!=CHOICE_NONE)return
}
}

//==============================

//ThefollowingfunctionsarerelatedtoBeegeesEasterEggonly

//Notesinthemelody.Eachnoteisaboutan1/8thnote,"0"sarerests.
intmelody[]={
NOTE_G4,NOTE_A4,0,NOTE_C5,0,0,NOTE_G4,0,0,0,
NOTE_E4,0,NOTE_D4,NOTE_E4,NOTE_G4,0,
NOTE_D4,NOTE_E4,0,NOTE_G4,0,0,
NOTE_D4,0,NOTE_E4,0,NOTE_G4,0,NOTE_A4,0,NOTE_C5,0
}

intnoteDuration=115//Thisessentiallysetsthetempo,115isjustaboutrightforadisco
groove:)
intLEDnumber=0//KeepstrackofwhichLEDweareonduringthebeegeesloop

//Donothingbutplaybadbeegeesmusic
//Thisfunctionisactivatedwhenuserholdsbottomrightbuttonduringpowerup
voidplay_beegees()
{
//Turnonthebottomright(yellow)LED
setLEDs(CHOICE_YELLOW)
toner(CHOICE_YELLOW,150)

setLEDs(CHOICE_RED|CHOICE_GREEN|CHOICE_BLUE)//TurnontheotherLEDsuntil
youreleasebutton

while(checkButton()!=CHOICE_NONE)//Waitforusertostoppressingbutton

setLEDs(CHOICE_NONE)//TurnoffLEDs

delay(1000)//Waitasecondbeforeplayingsong

digitalWrite(BUZZER1,LOW)//setupthe"BUZZER1"sideofthebuzzertostaylow,whilewe
playthetoneontheotherpin.

while(checkButton()==CHOICE_NONE)//Playsonguntilyoupressabutton
{
//iterateoverthenotesofthemelody:
for(intthisNote=0thisNote<32thisNote++){

changeLED()

tone(BUZZER2,melody[thisNote],noteDuration)

//todistinguishthenotes,setaminimumtimebetweenthem.

//thenote'sduration+30%seemstoworkwell:

intpauseBetweenNotes=noteDuration*1.30

delay(pauseBetweenNotes)
//stopthetoneplaying:
noTone(BUZZER2)
}

}
}

//EachtimethisfunctioniscalledtheboardmovestothenextLED
voidchangeLED(void)
{
setLEDs(1<<LEDnumber)//ChangetheLED

LEDnumber++//GotothenextLED
if(LEDnumber>3)LEDnumber=0//Wrapthecounterifneeded
}

Vous aimerez peut-être aussi