Hardware components | ||||||
![]() |
| × | 1 | |||
![]() |
| × | 6 | |||
![]() |
| × | 6 | |||
![]() |
| × | 1 | |||
![]() |
| × | 2 | |||
![]() |
| × | 2 | |||
![]() |
| × | 1 | |||
![]() |
| × | 2 | |||
Hand tools and fabrication machines | ||||||
![]() |
| |||||
![]() |
|
This project was originally intended as a way to demonstrate some of the basic functions of the Arduino Uno. It uses six pushbuttons and LEDs to re-create the classic 'Simon' game from the 1970s.
It was one of my first solo projects with the Arduino, and still one of my favorites. Originally, the code consumed almost thirty percent of available memory and the hardware used 12 data pins as well as power and ground.
I found the hardware in a drawer recently and decided to re-write the code in a more streamlined way. Simultaneously, I re-designed the hardware to use just 6 data pins. The new code uses just 11% of available memory.
At the surface, this is just a fun project with lights and sounds and interactivity. A deeper look shows uses of functions in code, using the same pins for input and output and use of the software reset function.
Basic Circuit

Simon Says
Arduino//This version of Simon Says Written by A. Wulf
//12/25/21
//************Global Variables*********************
int outVal[12];
int inVal = 0;
int toneNum;
int winLength;
int noteLength = 500;
int restLength = noteLength/5;
//*************************************************
void setup() {
winLength = 5; //initial length of challenge
toneNum = 0; //number of tones in current challenge
outTable(); //array to hold output values
intro(); //function to flash lights and play sounds at the start of the game
}
void (*resetFunc)(void) = 0; //software driven reset function
void loop() {
playChallenge(toneNum); //play the active challenge
compLoop(toneNum); //call the comparison loop
if (toneNum >= winLength){ //if the number of tones to play is equal to the max length you win!
youWin();
}
}
void compLoop(int numTone){
for(int k = 0; k <= numTone; k++){ //set comps to challenge length
getInput(); //wait for button value
if(inVal != outVal[k]){ //button doesn't match current outVal
youLose(); //lose Con
}
}
toneNum++; //increase challenge length
delay(200); //delay between good response and new challenge
}
void playChallenge(int toneNum){ //toneNum is 1 less than current number of tones in challenge
dataOut(); //set to output mode
for(int k = 0; k <= toneNum; k++){ //cycle through outVals up to number of tones in challenge
digitalWrite(outVal[k],HIGH); //light LED tied to pin called in outVal[k]
tone(A0,outVal[k]*60); //play proper tone
delay(noteLength); //hold 0.5 sec
digitalWrite(outVal[k],LOW);
noTone(A0); //LED out, no tone, hold 0.1 sec
delay(restLength);
} //next tone value
}
int getInput(){
dataIn(); //set to input mode
inVal = 0; //clear last input
int timeOut = 0; //set timeOut to 0;
while(inVal ==0){
timeOut++;
if (timeOut == 80){
youLose();
}
for (int k = 7; k <=12; k++){ //read all inputs
while (digitalRead(k) == HIGH){ //while button is pressed
tone(A0,k*60); //tone is played
if(digitalRead(k) == HIGH){ //change inVal to button pin on release
inVal = k;
}
}
delay(10);
noTone(A0); //no button, no tone
}
}
return inVal;
}
void intro(){
dataOut();
for(int k = 7; k <= 12; k++){
digitalWrite(k,HIGH);
tone(A0, k*60);
delay(50);
digitalWrite(k,LOW);
noTone(A0);
delay(50);
}
for(int k = 12; k >= 7; k--){
digitalWrite(k,HIGH);
tone(A0, k*60);
delay(50);
digitalWrite(k,LOW);
noTone(A0);
delay(50);
}
delay(1000);
}
void outTable(){
randomSeed(analogRead(A1)); //call seed
for (int k = 0; k < winLength; k++){ //loop to winLength
outVal[k] = random(7,13); //each value is 7-12 randomized [global]
}
}
void dataOut(){ //sets pins 7-12 to output mode
for (int k = 7; k<=12; k++){
pinMode(k, OUTPUT);
}
}
void dataIn(){ //sets pins 7-12 to Input mode
for (int k = 7; k<=12; k++){
pinMode(k, INPUT);
}
}
void youLose(){ //lose condition
for(int k = 0; k <= 2; k++){
tone (A0, 400);
delay(300);
noTone(A0);
delay(50); //plays loser song
}
tone(A0, 200);
delay(1000);
noTone(A0);
delay(500);
resetFunc(); //software reset
}
void youWin(){ //win condition
for (int i = 1; i <= 6; i++){
for (int k = 400; k <= 800; k = k + 50){
tone(A0, k); //play happy sounds
int Pin = random(7,13); //pick a random LED pin
digitalWrite(Pin,HIGH); //light random LED
delay(75);
digitalWrite(Pin,LOW); //ext same LED
}
}
noTone(A0);
delay(1500);
winLength = winLength + 1; //increase the max length of challenge by one
if (winLength > 7){
noteLength = 300;
}
if (winLength > 10){
noteLength = 100;
}
outTable(); //build a new challenge array
toneNum = 0; //set current challenge length to 1
}
Comments