Simón (Arduino)

Simon is an electronic game of memory skill invented by Ralph H. Baer and Howard J. Morrison,[1] with software programming by Lenny Cope, and manufactured and distributed by Milton Bradley. Simon was launched in 1978 at Studio 54 in New York City and was an immediate success, becoming a pop culture symbol of the 1980s. This is my version of Simon Says using Arduino. I connected 4 buttons to the 8, 9, 10, and 11 pins; the LEDs are connected to the 2, 3, 4, and 5 pins.

Simons says (Arduino) from Ricardo Munoz on Vimeo.

Buttons assembled

‘Simon says’ completed

Simon v.2 Code:

//corrections based on Simon Says game by Robert Spann (http://arduino.cc/forum/index.php/topic,8458.0.html)

int led1 = 5; //LED
int led2 = 4;
int led3 = 3;
int led4 = 2;

int turno = 0;

int btn1 = 11; //buttons
int btn2 = 10;
int btn3 = 9;
int btn4 = 8;

int input1 = LOW;
int input2 = LOW;
int input3 = LOW;
int input4 = LOW;

int randomArray[100]; //Intentionally long to store up to 100 inputs (doubtful anyone will get this far)
int inputArray[100];
void setup() {

Serial.begin(9600);

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);

pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(btn3, INPUT);
pinMode(btn4, INPUT);

randomSeed(analogRead(0)); //add randomness

for (int y=0; y<=99; y++){ //For statement to loop through the output and input functions
output();
input();
}

}

void output() { //original (computer’s) array

for (int y=turno; y <= turno; y++){ //Limited by the turn variable
Serial.println(“”); //Some serial output to follow along
Serial.print(“Turno: “);
Serial.print(y);
Serial.println(“”);

randomArray[y] = random(1, 5); //first computer’s move and storing each move, limit is number of effective moves
for (int x=0; x <= turno; x++){

Serial.print(randomArray[x]);

if (randomArray[x] == 1) { //turn on lights accordingly
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(100);
}

if (randomArray[x] == 2) {
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(100);
}

if (randomArray[x] == 3) {
digitalWrite(led3, HIGH);
delay(500);
digitalWrite(led3, LOW);
delay(100);
}

if (randomArray[x] == 4) {
digitalWrite(led4, HIGH);
delay(500);
digitalWrite(led4, LOW);
delay(100);
}
}
}
}

void input() { //button pressing and comparison against computer’s moves

for (int x=0; x <= turno;){ //Statement controlled by turn count
input1 = digitalRead(btn1);
input2 = digitalRead(btn2);
input3 = digitalRead(btn3);
input4 = digitalRead(btn4);

if (input1 == HIGH){ //Checking for button push
digitalWrite(led1, HIGH);
delay(200);
digitalWrite(led1, LOW);
inputArray[x] = 1;
delay(50);
Serial.print(” “);
Serial.print(1);
if (inputArray[x] != randomArray[x]) { //Compare input vs computer’s array; if different, ‘fail’ function is called; if correct, games continues
falla();
}
x++;
}

if (input2 == HIGH){
digitalWrite(led2, HIGH);
delay(200);
digitalWrite(led2, LOW);
inputArray[x] = 2;
delay(50);
Serial.print(” “);
Serial.print(2);
if (inputArray[x] != randomArray[x]) {
falla();
}
x++;

}

if (input3 == HIGH){
digitalWrite(led3, HIGH);
delay(200);
digitalWrite(led3, LOW);
inputArray[x] = 3;
delay(50);
Serial.print(” “);
Serial.print(3);
if (inputArray[x] != randomArray[x]) {
falla();
}
x++;

}

if (input4 == HIGH){

digitalWrite(led4, HIGH);
delay(200);
digitalWrite(led4, LOW);
inputArray[x] = 4;
delay(50);
Serial.print(” “);
Serial.print(4);
if (inputArray[x] != randomArray[x]) {
falla();
}
x++;

}
}
delay(500);
turno++; //Increments the turn count, also the last action before starting the output function over again
}

void falla() { //Function used if the player fails to match the sequence

delay(200);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
delay(200);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
delay(200);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
delay(200);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
delay(500);

turno = -1; //game reset
}

void loop() { //Unused void loop(), though for some reason it doesn’t compile without this /shrug
}

Advertisements

Your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s