Using state machine for output and input

Hello,
I’ve been working on a project where the arduino RGB flashes a series of colors, and the user has to then recreate that series of colors by pressing the pushbuttons and pressure sensor. I’m trying to use a state machine, I’ve never used states before, so I’m having some trouble.

State 0, the waiting state, and state 1, the output series of colors state, work fine. In state 2, nothing is happening when I press the pushbuttons, so the whole for loop in that state doesn’t work.

If I comment out the whole for loop, and use any command like (if rightRead == LOW) turn the led red, and I try to push the button, nothing happens. But if I do it with the button unpressed, (if rightRead == HIGH) turn the led red, and I DON’T push the button, then it works. So, it works when the button is unpressed, but not with the button press. Does anyone know why? Or, is that not the problem here?

//==============SYSTEM STATES====
//INTIALIZE = 0
//PLAY = 1
//USERINPUT = 2

int state = 0;  //master state

//========INITIALIZE PINS==========
int ledDigitalOne[] = {6,10,11};
//6 = red, 10 = green, 11= blue
int buttonRight = 4;
int buttonLeft = 8;
int pressureSense = 2;
int piezo = 3;

//========DEFINE VARIABLES=========
const boolean ON=LOW;
const boolean OFF=HIGH;
int leftRead = HIGH;
int rightRead= HIGH;
int pressure = 0;



//========PREDEFINED COLORS=========
boolean GREEN[] = {OFF, ON, OFF};
boolean RED[] = {ON, OFF, OFF};
boolean BLUE[] = {OFF, OFF, ON};
boolean YELLOW[] = {ON,ON,OFF};
boolean CYAN[] = {OFF,ON,ON};
boolean MAGENTA[] = {ON,OFF,ON};

//======ARRAY TO STORE COLORS===========
const boolean* COLORS[] = {GREEN, RED, BLUE, YELLOW,CYAN,MAGENTA};

//INPUT AND OUTPUT ARRAYS
boolean* outputArray[6];
boolean* inputArray[6];



//==============SETUP===============
void setup()
{
   for(int i = 0;i<3; i++)
     pinMode(ledDigitalOne[i], OUTPUT); 
   pinMode(buttonRight, INPUT);
   pinMode(buttonLeft, INPUT);
   pinMode(piezo, OUTPUT);
     
}
  
//=====================LOOP==================  
void loop()
{

  
  switch(state)
  {
    case 0:  //INITIALIZE
      waitForPress();
      break;
    case 1:  //PLAY
      playOutputSequence();
      break;
    case 2:  //USERINPUT
      recordUserInput();
      break;
    
}
}

//=========================FUNCTIONS=================================
//sets led to color specified
void setColor(int* led, boolean* color)
{
  for(int i = 0; i<3;i++)
  {
    digitalWrite(led[i], color[i]);
  }
}
//==================================================
void waitForPress()
{
  leftRead = digitalRead(buttonLeft);
  if (leftRead ==LOW)
    state = 1;  //PLAY
}
//====================================================    
void playOutputSequence()
{
  
  
  //make OUTPUTARRAY random
  for(int i=0;i<6;i++)
    outputArray[i]=(boolean*) COLORS[random(6)];
  
  //SEND OUTPUT ARRAY TO SETCOLOR ONE AT A TIME
  for (int i=0;i<6;i++)
  {
    setColor(ledDigitalOne, outputArray[i]);
    delay(1000);
  }
  tone(3, 392,1000);
  state = 2;    //WAIT
}
//======================================================   

void recordUserInput()
{
pressure = analogRead(pressureSense);
leftRead = digitalRead(buttonLeft);
rightRead = digitalRead(buttonRight);

 //USER RESPONSE 
 for (int i= 0; i<6; i++)
{
  if (leftRead == LOW && rightRead ==LOW)
	inputArray[i] = GREEN;
  else if (rightRead ==LOW)
	inputArray[i] = RED;
  else if (leftRead ==LOW)
	inputArray[i] = BLUE;
  else if (rightRead == LOW && pressure >0)
	inputArray[i] = YELLOW;
  else if (leftRead == LOW && pressure>0)
	inputArray[i] = CYAN;
  else if (pressure>0 && leftRead == HIGH && rightRead == HIGH)
	inputArray[i] = MAGENTA;
  setColor(ledDigitalOne, inputArray[i]);
  delay(1000);
}

state = 0;   
}

I’ve been trying to figure this out for awhile, so I appreciate any help.

Some things to consider…

◉ Are you using external pullup resistors? Otherwise, should use INPUT_PULLUP for your pushbutton inputs.

◉ Does your pressure sensor send out a voltage or does it require a pullup resistor?

◉ One of the analog inputs for the pressure sensor (i.e. pin A2) might work much better than digital input pin 2.

◉ Missing curly braces around for loop in setup, need

 for (int i = 0; i < 3; i++) {
    pinMode(ledDigitalOne[i], OUTPUT);
  }

◉ Avoid using delays, check out the blink without delay example.

◉ I would change the logic so that something happens “when” a button is pressed, not “while” it is pressed. This would require knowing and using the button(s) previous state(s).