# help with code for simon says like game

Hello there!
Well, me and two of my friends are building a simon says like game in our science class. It works like this:
a random number from 13 to 10 (the output pins on our arduino). Then it selects the LED that is connectet to the output.The Led is flighting up for a short time. Then you need to press the corresponding button. Then the next one is chosen, but you also need to press the one before that. So for example the first color of led is yellow: you press the yellow button. If ne next Color is red, the yellow led lights up and then the red one. then you need to press yellow and red and so on. if you press the wrong button, it starts again.

since we are new to this, and our teacher couldnt find anything wrong with our code (she recently learned arduino and that stuff herself, i used to do a little bit with raspberry pis, but not on a level like this), we wanted to show you the code, so you could find any errors or any simplifications.

heres the code (we're using an arduino mega 2560):

``````int buttonState = 0;
int SimonClear1 = 0;
int SimonClear21 = 0;
int SimonClear2 = 0;
int SimonClear3 = 0;
int SimonClear31 = 0;
int SimonClear32 = 0;
int SimonClear4 = 0;
int SimonClear41 = 0;
int SimonClear42 = 0;
int SimonClear43 = 0;
int Simon1 = 0;
int Simon2 = 0;
int Simon3 = 0;
int Simon4 = 0;
void setup() {
pinMode(13, OUTPUT);
pinMode(22, INPUT);
pinMode(12, OUTPUT);
pinMode(23, INPUT);
pinMode(11,OUTPUT);
pinMode(24, INPUT);
pinMode(10,OUTPUT);
pinMode(25, INPUT);
}
void loop() {
if (buttonState == HIGH) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
if (buttonState == HIGH) {
digitalWrite(12, HIGH);
} else {
digitalWrite(12, LOW);
}
if (buttonState == HIGH) {
digitalWrite(11, HIGH);
} else {
digitalWrite(11, LOW);
}
if (buttonState == HIGH) {
digitalWrite(10, HIGH);
} else {
digitalWrite(10, LOW);
}
bitSet(SimonClear1, 0);
bitSet(SimonClear21, 0);
bitSet(SimonClear2, 0);
bitSet(SimonClear31, 0);
bitSet(SimonClear32, 0);
bitSet(SimonClear3, 0);
bitSet(SimonClear41, 0);
bitSet(SimonClear42, 0);
bitSet(SimonClear43, 0);
bitSet(SimonClear4, 0);
bitSet(Simon1, random(10,13));
digitalWrite((Simon1), HIGH);
while((SimonClear1) == 0){
if(pinstate(Simon1)) == HIGH) {
bitSet(SimonClear1, 1);
}}
bitSet(Simon2, random(10,13));
delay(1000);
digitalWrite((Simon1), HIGH);
delay(1000);
digitalWrite((Simon2), HIGH);
while((SimonClear21) == 0){
if(pinstate(Simon1)) == HIGH){
bitSet(SimonClear21, 1);
}}
while(SimonClear2 == 0){
if(pinstate(Simon2)) == HIGH){
bitSet(SimonClear2, 1);
}}
bitSet(Simon3, random(10,13));
delay(1000);
digitalWrite((Simon1), HIGH);
delay(1000);
digitalWrite((Simon2), HIGH);
delay(1000);
digitalWrite((Simon3), HIGH);
while(SimonClear21 == 0){
if(pinstate(Simon1)) == HIGH){
bitSet(SimonClear31, 1);
}}
while(SimonClear21 == 0){
if(pinstate(Simon2)) == HIGH){
bitSet(SimonClear32, 1);
}}
while(SimonClear21 == 0){
if(pinstate(Simon3)) == HIGH){
bitSet(SimonClear3, 1);
}}
bitSet(Simon4, random(10,13));
delay(1000);
digitalWrite((Simon1), HIGH);
delay(1000);
digitalWrite((Simon2), HIGH);
delay(1000);
digitalWrite((Simon3), HIGH);
delay(1000);
digitalWrite((Simon4), HIGH);
while(SimonClear41 == 0){
if(pinstate(Simon1)) == HIGH){
bitSet(SimonClear41, 1);
}}
while(SimonClear42 == 0){
if(pinstate(Simon2)) == HIGH){
bitSet(SimonClear42, 1);
}}
while(SimonClear43 == 0){
if(pinstate(Simon3)) == HIGH){
bitSet(SimonClear43, 1);
}}
while(SimonClear4 == 0){
if(pinstate(Simon4)) == HIGH){
bitSet(SimonClear4, 1);
}}
digitalWrite(10, HIGH)
digitalWrite(11, HIGH)
digitalWrite(12, HIGH)
digitalWrite(13, HIGH)
}
``````

a random number from 13 to 10 (the output pins on our arduino).

What is that sentence fragment supposed to mean?

Then it selects the LED that is connectet to the output.

What does the selecting? Based on what?

The Led is flighting up for a short time.

The LED is doing what?

So for example the first color of led is yellow: you press the yellow button.

The state of a pin is what is important, not the color of the switch connected to the pin.

Why do you give the pins meaningless names, but then use the numbers in the rest of the code?

You WILL be using arrays before you are done. You might as well learn about them now, and start over.

How ARE the switches wired?

I agree with PaulS about the array use, here is an example for the beginning of your code:

``````#define numberOfButtons 4
byte buttonPins[numberOfButtons] = {22, 23, 24, 25};
byte ledPins[numberOfButtons] = {13, 12, 11, 10};

void setup() {
Serial.begin (115200);
for (byte i = 0; i < numberOfButtons; i++) {
pinMode (buttonPins[i], INPUT_PULLUP);
pinMode (ledPins[i], OUTPUT);
}
}

void loop() {
for (byte i = 0; i < numberOfButtons; i++)

}
``````

Buttons are declared as INPUT_PULLUP here, meaning:

``````GND -- push button -- pin
``````

But I really don't understand what you want to do with the bitset part. biset sets (writes a 1 to) a bit of a numeric variable.

``````bitSet(SimonClear1, 0);
``````

sets to 1 the bit 0 of the variable `SimonClear1`. Then

``````bitSet(Simon1, random(10,13));
``````

sets to 1 a random bit of the same variable, the bit's place is randomly chosen between 10 and 13. Let's say it's bit 11: SimonClear1 is now equal to b0000010000000001 which mean 0x41 in hex and 65 in decimal.
After that, you

``````digitalWrite((Simon1), HIGH);
``````

set to HIGH the pin 65 ? ? ?

Is that really what you want to do ?