I am incredibly frustrated with my first Arduino code. I am trying to build a Simon game, hardware and software, with out looking at the work of others.
I came to the point in the code where I am starting to rip out the hair from my head. Practicly everything works perfect accept the restart mechanism. So what I have so far is the following. Arduino generates a pattern on 50 random numbers and stores it in an array (ledA_) that would light up corresponding LEDS. After it would play back the LEDs and wait for user input and store it in a different array (buttonA*) that would be used to compare user pattern to the generated pattern. I got it to check if the patterns are the same, but it fails in restarting the game. Meaning I want it to generate a new patter to be used to light up corresponding leds and build up them up in displaying and imputing 1 to 1,2 to 1,2,3 and so on._
Here is my code, I have only built in the check mechanism into val3 or button that is connected to pin 3. Someone of you might suggest a different way to write the code, that might be 100% true and better, but I simply want to CRACK THIS SOB and get it working.
The problem is that if I deliberately fail the pattern, it starts to light up random LEDs with out end.
I am begging for someone to help me, please.
Thank you
_> int val2,val3,val4,val5; //Button Debounce Step 1*_
> int zval2, zval3, zval4, zval5; //Button Debounce Step 2
> int state2,state3,state4,state5; //Button Debounce First state
> int ledA[50]; //LED array used to store rnd #(8,9,10,11) to light up leds in those pins
> int buttonA[50]; //Button array used store button presses that get translated into nubmers (8,9,10,11)
> int z=0; //Game Level
> int y=0; //Loop Level Display
> int t=0; //Loop Level Input
>
> void setup(){
> pinMode(2, INPUT); //.................button
> pinMode(3, INPUT); //.................button
> pinMode(4, INPUT); //.................button
> pinMode(5, INPUT); //.................button
> pinMode(8, OUTPUT); //Input/Output setup
> pinMode(9, OUTPUT); //.................
> pinMode(10, OUTPUT); //.................
> pinMode(11, OUTPUT); //.................
> pinMode(12, OUTPUT); //.................Piezo
> Serial.begin(9600); //Random seed
> randomSeed(analogRead(0)); //Random seed
> state2=digitalRead(2); //Button comparison
> state3=digitalRead(3); //Button comparison
> state4=digitalRead(4); //Button comparison
> state5=digitalRead(5); //Button comparison
> }
>
> void loop(){
>
> while(z<=49){
> ledA[z]=random(8,12); //Generates random numbers as the game goes on.
>
> //The following loop (y<=z)plays back generated pattern of the LEDs up to the current level
*> while(y<=z){ *
*> digitalWrite(ledA[y],HIGH); *
*> delay(500); *
*> digitalWrite(ledA[y],LOW); *
> delay (500);
> y++;
> }
> /*Loop (t<=z) Waits for an appropiet number of buttons to be pressed. Checks that corresponding buttons are pressed.Currently only butt
> on that is connected to pin 3(val3) has the check mechanism, others simply advance the counter with out seeing if corresponding buttons has been pressed*/
> while(t<=z){
> val2=digitalRead(2); //used in debouncing momentary switches
> val3=digitalRead(3);
> val4=digitalRead(4);
> val5=digitalRead(5);
> delay(10);
> zval2=digitalRead(2);
> zval3=digitalRead(3);
> zval4=digitalRead(4);
*> zval5=digitalRead(5); //end in debounce Comparison *
> if(val2==zval2){ //Here is the loop "input code for each button//
> if(val2!=state2){
> if(val2==LOW){ //detection when button is released
> buttonA[t]=8; //storage for a corresponding number into an array
> t++;
> }
> }
> state2=val2;
> } //input code for first button ends here
> if(val3==zval3){
> if(val3!=state3){
> if(val3==LOW){
> buttonA[t]=9;
> if(ledA[t]!=buttonA[t]){ //this is the button that has the check code built in. If it fails the game needs to be restarted
> z=-1; // Z is set to -1 because ones it exits (t<=z) loop the will be a delay(500) and z++ will make it into z=0
> t=0;
> y=0;
> break;
> }
> t++;
> }
> }
> state3=val3;
> }
> if(val4==zval4){
> if(val4!=state4){
> if(val4==LOW){
> buttonA[t]=10;
> t++;
> }
> }
> state4=val4;
> }
> if(val5==zval5){
> if(val5!=state5){
> if(val5==LOW){
> buttonA[t]=11;
> t++;
> }
> }
> state5=val5;
> }
>
> }
> delay(500);
> z++;
> y=0;
> t=0;
> }
>
> }
Filesonic