6. Code - BLiverio/ICEN353-Group-7 GitHub Wiki

//Used to control LEDs

#define choiceOff 0

#define choiceNone 0

#define choiceR (1 << 0)

#define choiceB (1 << 1)

#define choiceY (1 << 2)

//led pins

#define ledR 11

#define ledB 12

#define ledY 13

// Button pins

#define buttonR 2

#define buttonB 3

#define buttonY 4

// Game Rules

#define win 10 //Number of rounds to win

#define timeLimit 5000 //Game time out 5 seconds

// Buzzer pins

#define buzzer1 8

#define buzzer2 7

#define memory 0

// Game variables

byte gameMode = memory;

byte gameBoard[32]; //Combination of buttons

byte nextRound = 0; //Counter

void setup()

{

//Hardware I/O

pinMode(buttonR, INPUT_PULLUP);

pinMode(buttonB, INPUT_PULLUP);

pinMode(buttonY, INPUT_PULLUP);

pinMode(ledR, OUTPUT);

pinMode(ledB, OUTPUT);

pinMode(ledY, OUTPUT);

pinMode(buzzer1, OUTPUT);

pinMode(buzzer2, OUTPUT);

//Mode

gameMode = memory;

stall();

}

void stall(void) //waits for user to press a button to start the game

{

while(1){

setLEDs(choiceR);

delay(1000);

setLEDs(choiceB);

delay(1000);

setLEDs(choiceY);

delay(1000);

if (checkButton() != choiceNone) break;

}

}

void loop()

{

startMode();

// The lights will blink untill user pushes a button

setLEDs(choiceR | choiceB | choiceY );

delay(100);

setLEDs(choiceOff); // Turn off LEDs

delay(250);

if (gameMode == memory)

{

if (playSimon() == true)

playWinner(); // Player won

else

playLoser(); // Player lost

}

}

void startMode(void) //all lights go off and then game starts with the sounds

{

//Counter for lights to dance and check for button push

while(1){

setLEDs(choiceR);

delay(100);

if (checkButton() != choiceNone) break;

setLEDs(choiceB);

delay(100);

if (checkButton() != choiceNone) break;

setLEDs(choiceY);

delay(100);

if (checkButton() != choiceNone) break;

}

}

boolean playSimon(void)

{

randomSeed(millis()); // Seed the random generator with random amount of millis()

nextRound = 0; //Start the game

while (nextRound < win) //until it hits round 10, program keeps adding another random //led/choice

{ addMoves(); // Add a button to the current moves, then play them back

playMoves(); // Play back the current game board

//Player to repeat the sequence.

for (byte currentMove = 0 ; currentMove < nextRound ; currentMove++)

{ byte choice = wait_for_button(); // See what button the user presses

if (choice == 0) return false; // If wait timed out, player loses

if (choice != gameBoard[currentMove]) return false; // If the choice is incorect, player loses

}

delay(1000); // Player was correct, delay before playing moves

}

return true; // Player made it through all the rounds

}

// Plays the current contents of the game moves

void playMoves(void)

{

for (byte currentMove = 0 ; currentMove < nextRound ; currentMove++)

{ toner(gameBoard[currentMove], 150);

// Wait time between button pressed

delay(150);

}

}

// Adds a new random button

void addMoves(void)

{

byte newButton = random(0, 3); //min (included), max (excluded)

if(newButton == 0) newButton = choiceR;

else if(newButton == 1) newButton = choiceB;

else if(newButton == 2) newButton = choiceY;

gameBoard[nextRound++] = newButton; // Add this new button to the array to store for future rounds

}

//Hardware

void setLEDs(byte leds)

{ //red led

if ((leds & choiceR) != 0)

digitalWrite(ledR, HIGH);

else

digitalWrite(ledR, LOW);

//blue led

if ((leds & choiceB) != 0)

digitalWrite(ledB, HIGH);

else

digitalWrite(ledB, LOW);

//yellow led

if ((leds & choiceY) != 0)

digitalWrite(ledY, HIGH);

else

digitalWrite(ledY, LOW);

}

// Wait for a button to be pressed along

byte wait_for_button(void)

{

long startTime = millis();

while ( (millis() - startTime) < timeLimit) // Loop until too much time has passed

{

byte button = checkButton();

if (button != choiceNone)

{ toner(button, 150);

while(checkButton() != choiceNone) ; //User to release button

delay(10);

return button;

}

}

return choiceNone; //Player has timed out

}

// Checks for button pushing

byte checkButton(void)

{

if (digitalRead(buttonR) == 0) return(choiceR);

else if (digitalRead(buttonB) == 0) return(choiceB);

else if (digitalRead(buttonY) == 0) return(choiceY);

return(choiceNone); }

void toner(byte which, int buzz_length_ms)

{

setLEDs(which); //Turn on a given LED

switch(which)

{

case choiceR:

buzzSound(buzz_length_ms, 1234);

break;

case choiceB:

buzzSound(buzz_length_ms, 876);

break;

case choiceY:

buzzSound(buzz_length_ms, 654);

break;

}

setLEDs(choiceOff); // Turn off all LEDs

}

// Toggle buzzer every buzz_delay_us, for a duration of buzz_length_ms.

void buzzSound(int buzz_length_ms, int buzz_delay_us)

{ // Convert total play time from milliseconds to microseconds

long buzz_length_us = buzz_length_ms * (long)1000;

// Loop until the remaining play time is less than a single buzz_delay_us

while (buzz_length_us > (buzz_delay_us * 2)) { buzz_length_us -= buzz_delay_us * 2; //Decrease the remaining play time

// Set buzzer at various speeds

digitalWrite(buzzer1, LOW);

digitalWrite(buzzer2, HIGH);

delayMicroseconds(buzz_delay_us);

digitalWrite(buzzer1, HIGH);

digitalWrite(buzzer2, LOW);

delayMicroseconds(buzz_delay_us);

}

}

// If player wins

void playWinner(void)

{

setLEDs(choiceY| choiceB);

winnerSound();

setLEDs(choiceR | choiceY);

winnerSound();

setLEDs(choiceB | choiceY);

winnerSound();

setLEDs(choiceR | choiceY);

winnerSound();

}

// Sound for the winner

void winnerSound(void)

{ // Set the buzzer at various speeds (cycle)

for (byte i = 250 ; i > 69 ; i--)

{

for (byte j = 0 ; j < 3 ; j++)

{ digitalWrite(buzzer2, HIGH);

digitalWrite(buzzer1, LOW);

delayMicroseconds(i);

digitalWrite(buzzer2, LOW);

digitalWrite(buzzer1, HIGH);

delayMicroseconds(i);

}

}

}

//Loser sound and lights

void playLoser(void)

{

setLEDs(choiceB | choiceY);

buzzSound(750, 1000);

setLEDs(choiceR | choiceY);

buzzSound(750, 1000);

setLEDs(choiceB | choiceY);

buzzSound(750, 1000);

}