Landing : Athabascau University
  • Blogs
  • Circuit 2C: Simon Says Game

Circuit 2C: Simon Says Game

I had no issues building the circuit. I modified the example code so that it flashes and buzzes morse code messages instead of playing tunes at the start and the end of the game. The morse code at the start of the game spells out the word "Start". The morse code at the end of the game spells out either "Win" or "Lose", depending on whether the player won or lost. The "win" message is buzzed at a higher tone and the "lose" message is buzzed at a lower tone. The "start" message is buzzed at a medium tone. I also changed the number of rounds needed to win to three instead of ten. Here is the code:

//set the pins where the buttons, LEDs and buzzer connect
int button[] = {2, 4, 6, 8}; //red is button[0], yellow is button[1], green is button[2], blue is button[3]
int led[] = {3, 5, 7, 9}; //red is led[0], yellow is led[1], green is led[2], blue is led[3]
int tones[] = {262, 330, 392, 494}; //tones to play with each button (c, e, g, b)
int roundsToWin = 3; //number of rounds the player has to play before they win the game (the array can only hold up to 16 rounds)
int buttonSequence[16]; //make an array of numbers that will be the sequence that the player needs to remember
int buzzerPin = 10; //pin that the buzzer is connected to
int pressedButton = 4; //a variable to remember which button is being pressed. 4 is the value if no button is being pressed.
int roundCounter = 1; //keeps track of what round the player is on

long startTime = 0; //timer variable for time limit on button press
long timeLimit = 2000; //time limit to hit a button
int ditDuration = 100; //duration of a dit for morse code messages
int loseFreq = 92; //Tone frequency for the lose message
int winFreq = 2183; //Tone frequency for the win message
int startFreq = 370; //Tone frequency for the start message
boolean gameStarted = false; //variable to tell the game whether or not to play the start sequence
void setup() {
//set all of the button pins to input_pullup (use the built-in pull-up resistors)
pinMode(button[0], INPUT_PULLUP);
pinMode(button[1], INPUT_PULLUP);
pinMode(button[2], INPUT_PULLUP);
pinMode(button[3], INPUT_PULLUP);
//set all of the LED pins to output
pinMode(led[0], OUTPUT);
pinMode(led[1], OUTPUT);
pinMode(led[2], OUTPUT);
pinMode(led[3], OUTPUT);
pinMode(buzzerPin, OUTPUT); //set the buzzer pin to output
}
void loop() {
if (gameStarted == false) { //if the game hasn't started yet
startSequence(); //flash the start sequence
roundCounter = 0; //reset the round counter
delay(1500); //wait a second and a half
gameStarted = true; //set gameStarted to true so that this sequence doesn't start again
}
//each round, start by flashing out the sequence to be repeated
for (int i = 0; i <= roundCounter; i++) { //go through the array up to the current round number
flashLED(buttonSequence[i]); //turn on the LED for that array position and play the sound
delay(200); //wait
allLEDoff(); //turn all of the LEDs off
delay(200);
}
//then start going through the sequence one at a time and see if the user presses the correct button
for (int i = 0; i <= roundCounter; i++) { //for each button to be pressed in the sequence
startTime = millis(); //record the start time
while (gameStarted == true) { //loop until the player presses a button or the time limit is up (the time limit check is in an if statement)
pressedButton = buttonCheck(); //every loop check to see which button is pressed
if (pressedButton < 4) { //if a button is pressed... (4 means that no button is pressed)
flashLED(pressedButton); //flash the LED for the button that was pressed
if (pressedButton == buttonSequence[i]) { //if the button matches the button in the sequence
delay(250); //leave the LED light on for a moment
allLEDoff(); //then turn off all of the lights and
break; //end the while loop (this will go to the next number in the for loop)
} else { //if the button doesn't match the button in the sequence
loseSequence(); //play the lose sequence (the loose sequence stops the program)
break; //when the program gets back from the lose sequence, break the while loop so that the game can start over
}
} else { //if no button is pressed
allLEDoff(); //turn all the LEDs off
}
//check to see if the time limit is up
if (millis() - startTime > timeLimit) { //if the time limit is up
loseSequence(); //play the lose sequence
break; //when the program gets back from the lose sequence, break the while loop so that the game can start over
}
}
}
if (gameStarted == true) {
roundCounter = roundCounter + 1; //increase the round number by 1
if (roundCounter >= roundsToWin) { //if the player has gotten to the 16th round
winSequence(); //play the winning song
}
delay(500); //wait for half a second between rounds
}
}
//----------FUNCTIONS------------
//FLASH LED
void flashLED (int ledNumber) {
digitalWrite(led[ledNumber], HIGH);
tone(buzzerPin, tones[ledNumber]);
}
//TURN ALL LEDS OFF
void allLEDoff () {
//turn all the LEDs off
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], LOW);
digitalWrite(led[3], LOW);
//turn the buzzer off
noTone(buzzerPin);
}
//CHECK WHICH BUTTON IS PRESSED
int buttonCheck() {
//check if any buttons are being pressed
if (digitalRead(button[0]) == LOW) {
return 0;
} else if (digitalRead(button[1]) == LOW) {
return 1;
} else if (digitalRead(button[2]) == LOW) {
return 2;
} else if (digitalRead(button[3]) == LOW) {
return 3;
} else {
return 4; //this will be the value for no button being pressed
}
}
//START SEQUENCE
void startSequence() {
randomSeed(analogRead(A0)); //make sure the random numbers are really random
//populate the buttonSequence array with random numbers from 0 to 3
for (int i = 0; i <= roundsToWin; i++) {
buttonSequence[i] = round(random(0, 4));
}
startMessage();
}
//WIN SEQUENCE
void winSequence() {
winMessage();
ledsOn();

//wait until a button is pressed
do {
pressedButton = buttonCheck();
} while (pressedButton > 3);
delay(100);
gameStarted = false; //reset the game so that the start sequence will play again.
}
//LOSE SEQUENCE
void loseSequence() {
loseMessage();
ledsOn();

//wait until a button is pressed
do {
pressedButton = buttonCheck();
} while (pressedButton > 3);
delay(200);
gameStarted = false; //reset the game so that the start sequence will play again.
}
void startMessage(){
letterS(startFreq);
letterT(startFreq);
letterA(startFreq);
letterR(startFreq);
letterT(startFreq);
noTone(buzzerPin);
}
void winMessage(){
letterW(winFreq);
letterI(winFreq);
letterN(winFreq);
noTone(buzzerPin);
}
void loseMessage(){
letterL(loseFreq);
letterO(loseFreq);
letterS(loseFreq);
letterE(loseFreq);
noTone(buzzerPin);
}
void letterS(int toneFreq){
dit(toneFreq);
space();
dit(toneFreq);
space();
dit(toneFreq);
longSpace();
}
void letterT(int toneFreq){
dah(toneFreq);
longSpace();
}
void letterA(int toneFreq){
dit(toneFreq);
space();
dah(toneFreq);
longSpace();
}
void letterR(int toneFreq){
dit(toneFreq);
space();
dah(toneFreq);
space();
dit(toneFreq);
longSpace();
}
void letterW(int toneFreq){
dit(toneFreq);
space();
dah(toneFreq);
space();
dah(toneFreq);
longSpace();
}
void letterI(int toneFreq){
dit(toneFreq);
space();
dit(toneFreq);
longSpace();
}
void letterN(int toneFreq){
dah(toneFreq);
space();
dit(toneFreq);
longSpace();
}
void letterL(int toneFreq){
dit(toneFreq);
space();
dah(toneFreq);
space();
dit(toneFreq);
space();
dit(toneFreq);
longSpace();
}
void letterO(int toneFreq){
dah(toneFreq);
space();
dah(toneFreq);
space();
dah(toneFreq);
longSpace();
}
void letterE(int toneFreq){
dit(toneFreq);
longSpace();
}
void dit(int toneFreq){
tone(buzzerPin, toneFreq);
ledsOn();
delay(ditDuration);
}
void dah(int toneFreq){
tone(buzzerPin, toneFreq);
ledsOn();
delay(3*ditDuration);
}
void space(){
allLEDoff();
delay(ditDuration);
}
void longSpace(){
allLEDoff();
delay(3*ditDuration);
}
void ledsOn(){
for (int j = 0; j <= 3; j++) {
digitalWrite(led[j], HIGH);
}
}

This code is mostly similar to the example code. The functions I added include the startMessage(), winMessage(), and loseMessage() functions, as well as all the functions for morse code letters, dits, dahs, and spaces. I altered the winSequence(), loseSequence, and startSequence() functions to call my message functions instead of playing the tunes that they originally did. 

Here is a video of the program in action:  MorseCodeSimonSays

I did not have any issues while writing this program.