Hi Guys,
I have a Problem. i'm trying to rebuild the game SIMON with Arduino (for those who don#
't know what game I mean, please lokk at the link: Simon (game) - Wikipedia)
Here the Code:
int switch1 = 11;
int switch2 = 10;
int switch3= 9;
int switch4 = 8;
int led1= 5;
int led2 = 4;
int led3= 3;
int led4= 2;
int turn = 0;
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(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(switch3, INPUT);
pinMode(switch4, INPUT);
randomSeed(analogRead(0)); //Added to generate "more randomness" with the randomArray for the output function
for (int y=0; y<=99; y++){ //For statement to loop through the output and input functions
output();
input();
}
}
void output() { //function for generating the array to be matched by the player
for (int y=turn; y <= turn; y++){ //Limited by the turn variable
Serial.println(""); //Some serial output to follow along
Serial.print("Turn: ");
Serial.print(y);
Serial.println("");
randomArray[y] = random(1, 5); //Assigning a random number (1-4) to the randomArray[y], y being the turn count
for (int x=0; x <= turn; x++){
Serial.print(randomArray[x]);
if (randomArray[x] == 1) { //if statements to display the stored values in the array
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() { //Function for allowing user input and checking input against the generated array
for (int x=0; x <= turn;){ //Statement controlled by turn count
input1 = digitalRead(switch1);
input2 = digitalRead(switch2);
input3 = digitalRead(switch3);
input4 = digitalRead(switch4);
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]) { //Checks value input by user and checks it against
fail(); //the value in the same spot on the generated array
} //The fail function is called if it does not match
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]) {
fail();
}
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]) {
fail();
}
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]) {
fail();
}
x++;
}
}
delay(500);
turn++; //Increments the turn count, also the last action before starting the output function over again
}
void fail() { //Function used if the player fails to match the sequence
for (int y=0; y<=5; y++){ //Flashes lights for failure
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);
}
delay(500);
turn = -1; //Resets turn value so the game starts over without need for a reset button
}
void loop() { //Unused void loop(), though for some reason it doesn't compile without this /shrug
}
The LEDs are connected with GND and the PINs which are mentiooned before in the code
The Buttons are also connected with GND and the PINS and 5 V
Now to my Problem: When I upload the code to the board. The Leds are going crazy. Firstly one or two shortly glow and then all 4 Ledds glow like there was an error.
Do anyone know whats going wrong? Would be very nice and great if you could help me.
PS: please excuse my bad english i'am no native speaker.
Greetz
