Help With First Project

I am learning Arduino IDE code for one of my semester final projects, and I am attempting to make a Simon Says game. I have run into the problem of the code entering "Limbo" after receiving an input, and just not running until a button is pressed again, where it then runs once only. What am I missing? Help is appreciated, and I need it kind of quickly, since its due thursday. Thanks!

Ignoring all previous updates below the code, I figured out the original problem. I forgot to put a line of code that de increments the array to the correct slot, instead of the one past it. Now I have a New problem where it simply never comes up as false, nor does it it wait for an input. Any input will cause it to run through the entire loop regardless of the while loop checking the value of "phase" not even a wrong input.
It is also not triggering the "startInput" variable to beome 0 after selecting the wrong variable, even though nothing would cause it to remain 1. What am I missing?

This now the most up to date code of the project, with the new problem at hand

//Constants setting pin Values
const byte buttonRedPin = 2;
const byte buttonGrnPin = 3;
const byte buttonYelPin = 4;
const byte buttonBluPin = 5;
const byte ledRedPin = 13;
const byte ledGrnPin = 12;
const byte ledYelPin = 11;
const byte ledBluPin = 10;
//Variables for Button states and string value
byte buttonStateRed = 1;
byte buttonStateGrn = 1;
byte buttonStateYel = 1;
byte buttonStateBlu = 1;
byte startInput = 0;
int arrayDigit = 0;
byte pressInput = 0;
byte phase = 0;
//Array Setup
int orderArray[99];
//Setup script, Define port functions of pins
void setup() {
  Serial.begin(9600);
  pinMode(ledRedPin, OUTPUT);
  pinMode(ledGrnPin, OUTPUT);
  pinMode(ledYelPin, OUTPUT);
  pinMode(ledBluPin, OUTPUT);
  pinMode(buttonRedPin, INPUT);
  pinMode(buttonGrnPin, INPUT);
  pinMode(buttonYelPin, INPUT);
  pinMode(buttonBluPin, INPUT);
  randomSeed(digitalRead(7));
}
//Loop script, god help us all
void loop() {
  //ButtonState Qualifiers
  buttonStateRed = digitalRead(buttonRedPin);
  buttonStateGrn = digitalRead(buttonGrnPin);
  buttonStateYel = digitalRead(buttonYelPin);
  buttonStateBlu = digitalRead(buttonBluPin);
  //Transient Mode Screen
  if (startInput == 0) {
    if (buttonStateRed == 0 || buttonStateGrn == 0 || buttonStateYel == 0 || buttonStateBlu == 0) {
      startInput = 1;
    } else {
      digitalWrite(ledRedPin, HIGH);
      delay(80);
      digitalWrite(ledGrnPin, HIGH);
      delay(80);
      digitalWrite(ledYelPin, HIGH);
      delay(80);
      digitalWrite(ledBluPin, HIGH);
      delay(80);
      digitalWrite(ledRedPin, LOW);
      delay(80);
      digitalWrite(ledGrnPin, LOW);
      delay(80);
      digitalWrite(ledYelPin, LOW);
      delay(80);
      digitalWrite(ledBluPin, LOW);
      delay(80);
    }
    //Start of Games Code
  } else {
    delay(200);
    while (orderArray[arrayDigit] > 0) {
      arrayDigit++;
    }
    orderArray[arrayDigit] = random(1, 5);
    while (arrayDigit > 0) {
      digitalWrite((orderArray[arrayDigit]) + 9, HIGH);
      delay(300);
      digitalWrite((orderArray[arrayDigit]) + 9, LOW);
      arrayDigit--;
    }
    digitalWrite((orderArray[arrayDigit]) + 9, HIGH);
    delay(300);
    digitalWrite((orderArray[arrayDigit]) + 9, LOW);
    while (orderArray[arrayDigit] > 0) {
      arrayDigit++;
    }
    arrayDigit--;
    phase = 1;
    while (phase == 1) {
      pressInput = 0;
      while (pressInput == 0) {
        buttonStateRed = digitalRead(buttonRedPin);
        buttonStateGrn = digitalRead(buttonGrnPin);
        buttonStateYel = digitalRead(buttonYelPin);
        buttonStateBlu = digitalRead(buttonBluPin);
        if (buttonStateRed == 0 || buttonStateGrn == 0 || buttonStateYel == 0 || buttonStateBlu == 0) {
          if (buttonStateRed == 0) {
            pressInput = 4;
          } else if (buttonStateGrn == 0) {
            pressInput = 3;
          } else if (buttonStateYel == 0) {
            pressInput = 2;
          } else if (buttonStateBlu == 0) {
            pressInput = 1;
          }
        }
      }
      Serial.print(pressInput);
      Serial.print(orderArray[arrayDigit]);
      if (orderArray[arrayDigit] == pressInput) {
        if (arrayDigit > 0) {
          arrayDigit--;
          Serial.print(8);
        } else {
          phase = 0;
          Serial.print(7);
        }
      }
      if (orderArray[arrayDigit] != pressInput) {
        //END SEQUENCE
        phase = 0;
        pressInput = 0;
        startInput = 0;
        //while(orderArray[arrayDigit] > 0){
        //arrayDigit++;
        //}
        //orderArray[arrayDigit] = 0;
        //while(arrayDigit > 0){
        //arrayDigit--;
        //orderArray[arrayDigit] = 0;
        //}
      }  //end sequence while
      pressInput = 0;
    }  //phase while
  }    //Else
  //Delay for code loop cause???
  delay(10);
}  //loop end

ok, I hadnt known I should do that, thanks!
Its been updated in the post

  • Always show us a schematic showing all components and connections.

The forum post just gave me the error "new users cannot upload attachements"
Trust me its just 4 lights and 4 buttons on seperate wires, its 100% not a problem with the wiring

That "running only once" could be a flag that was set, not being reset.

It looks like your code waits for the buttons become HIGH to change startInput. Do you have pull-down resistors on the buttons to keep them from floating (randomly becoming HIGH) when not pressed?

Im sorry could you explain what a pulldown resistor is? I have never heard of that before. But I havent dealt with the buttons randomly becoming high, atleast from what I have seen from serial.print

  • How are your switches/buttons wired ?

  • If this is an Arduino UNO, you should be using a floating analog pin like A0 instead of D7.

randomSeed(digitalRead(7));

BTW

  • For readability highly recommend you place { and } on lines by themselves.

This link has a good explanation: Arduino Pull-up Pull-down Resistor | Arduino FAQs

  • Login - Tinkercad This is a link to (a copy of) the project, so you can see the schematic

-I will change it to analog read next time im able!

It sort of matters that equates to over 24,000 possibilities. To be honest and a bit lazy I am not going to try to do that many iterations. Schematics is the language of electronics.

Login - Tinkercad

Heres the link to (a copy of) the project, if your able to access it to see the schematic, since I cant upload attachements cause I just made this forum account

  • Off to do grocery shopping :slight_smile:

  • Just some thoughts:

    • If I was doing this I would make a colour array.
      When the code selects the next colour it is stored in this array.
    • As the game advances the array fills up with the random colours.
    • As the user responds, compare their input it with the array colour for that array index . . .
  • Your Tinkercad link goes no where.

Useless to me as I do not have an account. As I understand it you have to show some activity on the forum first. This is what I found on line:
FASTEST WAY TO UNLOCK UPLOADS

  1. Log in
  2. Open multiple technical threads
  3. Scroll and actually read them
  4. Spend some real time on the forum
  5. Refresh — TL1 usually unlocks automatically

While looking at other posts you will learn many things about Arduino. Also you will find I have recommended the Arduino Cookbook many times. That schematic was asked by @LarryD in post 4.

The color array system you described... is exactly what I have implemented. I just use 1,2,3,4 instead of directly stating the colors

I dont really have time for that, Its due in 3 days.
Isnt it possible to simply go off the code itself? Theres 100% no fault with the wiring, since it works as expected in every other application of code

Not the right way to go.

"We" do not have a deadline.

No. You state that you do not know what a pull-up/pull-down resistor is. You have not learned the basics, but want to have intermediate-level code. I set-up your project. I ran your code. I asked you a question about pullup/down resistors , "I have never heard of that before" was your answer.

Really?

When/where does "100%" equal "runs once?"

In the world of life, you will hear: "No one has time to do it right the first time, but everyone has time to do it right the second time." So, what you need to do is make time to read all the helpful replies. Read about them, or ask questions until you understand them. You now have time to do it right.

You said you ran my code, when it got to the point where it flashed a color and the correct button was pushed, did it just stop? all lights go out? or perhaps yellow light flashed once then the red one stayed on? these are the only 2 results I am able to get. In both cases pushing any button runs the loop once then pauses the loop again. I am specifically looking for how I messed something up to cause the entire loop to repeatedly wait for a button input.