Switch cases not changing according to instruction

Hi! :slight_smile: I'm really new to Arduino, and I'm trying to make a little matching LEDs game, the main point is controlling one RGB LED brightness with 3 buttons(one for each rgb color), and trying to match the brightness of the second RGB LED.

I got the buttons working to change the brightness of the first LED from an array of 4 different values, so I created switch cases to set the values to match for the second LED(using the same array), and change to the next value once we get brightness matched.

The problem is that it gets stuck on the first case "correctAnswer1", and even when the other LED matched the values, it doesn't change to the next case "correctAnswer2". :frowning:

Hope I explained myself somehow, thank you!

#define rPin 11
#define gPin 10
#define bPin 9

#define rPin2 6
#define gPin2 5
#define bPin2 3

#define rBtn 8
#define gBtn 7
#define bBtn 4


#define SAMPLING 200 //200milliSecond per press, used for debouncing

//cases
const int prepareGame = 0;
const int beginGame = 1;
const int correctAnswer1 = 2;
const int matchedAnswer1 = 3;
const int correctAnswer2 = 4;
const int matchedAnswer2 = 5;
const int correctAnswer3 = 6;
const int matchedAnswer3 = 7;
const int correctAnswer4 = 8;
const int matchedAnswer4 = 9;
const int correctAnswer5 = 10;
const int matchedAnswer5 = 11;
const int allMatches = 12;


static unsigned long TIMER = 0; //record current millis()
byte brightState = 0; //val used to check current status of pushbutton
byte brightness[] = {0, 75, 150, 255};  //array for the pushbuttons


void setup()
{
  pinMode(rPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(bPin, OUTPUT);
  pinMode(rPin2, OUTPUT);
  pinMode(gPin2, OUTPUT);
  pinMode(bPin2, OUTPUT);
  pinMode(rBtn, INPUT);
  pinMode(gBtn, INPUT);
  pinMode(bBtn, INPUT);
}


void pushButtons()
{
  if(millis() - TIMER >= SAMPLING) //sampling ~= 250mS
  {  
    TIMER = millis();

   if(digitalRead(rBtn) == HIGH) //button pressed, pulled high
    {
      brightState++; //increment state
      if(brightState > 3) //want only 4 brightness options, 0, 1, 2, 3
        brightState = 0;
      analogWrite(rPin, brightness[brightState]); 
    }
    
    if(digitalRead(gBtn) == HIGH) //button pressed, pulled high
    {
      brightState++; //increment state
      if(brightState > 3) //want only 4 brightness options, 0, 1, 2, 3
        brightState = 0;
      analogWrite(gPin, brightness[brightState]); 
    }

    if(digitalRead(bBtn) == HIGH) //button pressed, pulled high
    {
      brightState++; //increment state
      if(brightState > 3) //want only 4 brightness options, 0, 1, 2, 3
        brightState = 0;
      analogWrite(bPin, brightness[brightState]); 
    }
  }
}

void matchingLed(){
  
  static int state = prepareGame; 
  
  switch (state){

    case prepareGame:
    analogWrite(rPin2, brightness[0]); //set the leds to 0
    analogWrite(gPin2, brightness[0]);
    analogWrite(bPin2, brightness[0]);

    state = beginGame;

    break;

    case beginGame: //set the second LED on the first brightness to matched
    if((digitalRead(rBtn) == HIGH) || (digitalRead(gBtn) == HIGH) || (digitalRead(bBtn) == HIGH)){
      state = correctAnswer1;
    }

    break;
    

   case correctAnswer1:
    analogWrite(rPin2, brightness[3]); //this is where it stays
    analogWrite(gPin2, brightness[0]); //even when rPin, gPin & bPin are on those values
    analogWrite(bPin2, brightness[0]);

    state = matchedAnswer1;

    break;

   case matchedAnswer1:
    if((rPin == brightness[3]) && (gPin == brightness[0]) && (bPin == brightness[0])){
      state = correctAnswer2;
    }

      break;

    case correctAnswer2:
    analogWrite(rPin2, brightness[1]);
    analogWrite(gPin2, brightness[2]);
    analogWrite(bPin2, brightness[3]);

    state = matchedAnswer2;

    break;

    case matchedAnswer2:
    if((rPin==brightness[1]) && (gPin==brightness[2]) && (bPin==brightness[3])){
      
      state = correctAnswer3;
    }

      break;

     case correctAnswer3:
    analogWrite(rPin2, brightness[3]);
    analogWrite(gPin2, brightness[1]);
    analogWrite(bPin2, brightness[0]);

    state = matchedAnswer3;

    break;

    case matchedAnswer3:
    if((rPin==brightness[3]) && (gPin==brightness[1]) && (bPin==brightness[0])){
      
      state = correctAnswer4;
    }

      break;

    case correctAnswer4:
    analogWrite(rPin2, brightness[2]);
    analogWrite(gPin2, brightness[2]);
    analogWrite(bPin2, brightness[2]);

    state = matchedAnswer4;

    break;

    case matchedAnswer4:
    if((rPin==brightness[2]) && (gPin==brightness[2]) && (bPin==brightness[2])){
      
      state = correctAnswer5;
    }

      break;

    case correctAnswer5:
    analogWrite(rPin2, brightness[0]);
    analogWrite(gPin2, brightness[3]);
    analogWrite(bPin2, brightness[0]);

    state = matchedAnswer5;

    break;

    case matchedAnswer5:
    if((rPin==brightness[0]) && (gPin==brightness[3]) && (bPin==brightness[0])){
      
      state = allMatches;
    }

      break;

    case allMatches:

      //here is going to be some kind of reward, still thinking

   state = prepareGame; //return to the first case to begin again

       break;
    
  }
}



void loop(){
  
  matchingLed();
  pushButtons();
}
  if ((rPin == brightness[3]) && (gPin == brightness[0]) && (bPin == brightness[0])) {

You are comparing constant pin numbers with values that you use for analogWrites. Is that what you intended?

For example, rPin will always be 11.