help with switch case

Hello, Brains-Trust, and thank you for taking the time to have a quick look over this.

I’ve hit a bit of a wall on the switch use, I’ve pulled everything back to try and troubleshoot this, and nothing is working for me and I’m now stumped. I am just not seeming to get anything to pass the first case,

program is receiving simple 3 character statements, in this case, it’s looking for an =a1 or =j1 or the other is =a0 or =j0 to then turn an LED on or off on their respective pins,

Where have I strayed from the path? thank you so much again for your time, and help!

int CodeIn;// used on all serial reads
int Koutpin;

void setup() 
{                                                  

  for (int KoutPin = 3; KoutPin < 24; KoutPin++)   // Get these pins 3 TO 23 ready as outputs FOR LEDs
  {
    pinMode(KoutPin, OUTPUT);
   digitalWrite(KoutPin, LOW); 
   }

 Serial.begin(115200);   
 
}

void loop(){
  if (Serial.available()) {
    CodeIn = getChar();
    if (CodeIn == '=') {EQUALS();} // The first identifier is "="
  }
}

char getChar()// Get a character from the serial buffer
{
  while(Serial.available() == 0);// wait for data
  return((char)Serial.read());
}
  
void EQUALS(){      // The first identifier was "="
CodeIn = getChar(); // Get the second identifier
  switch(CodeIn){ // Now lets find what to do with it
    case 'a':   // we got an =a,
   int APState = getChar();   //     1 = 0n, 0 = off
      if (APState =='1'){digitalWrite(22, HIGH);} else {digitalWrite(22, LOW);} 
         break;
         
    case 'j':  // we got an =a,
   int HHState = getChar(); // 1 = 0n, 0 = off
      if (HHState =='1'){digitalWrite(23, HIGH);} else {digitalWrite(23, LOW);} 
     
     break;
     } 
}

not sure what your error was. I was getting “warning: jump to case label [-fpermissive]” which was corrected by defining APState and HHState outside of the switch statement (i needed different LED pins, and reformatted your code)

#define LED_A   10
#define LED_B   11

int CodeIn;
// used on all serial reads
int Koutpin;

// -----------------------------------------------------------------------------
void setup ()
{
#if 0
    for  (int KoutPin = 3; KoutPin < 24; KoutPin++)   // Get these pins 3 TO 23 ready as outputs FOR LEDs
    {
        pinMode (KoutPin, OUTPUT);
        digitalWrite (KoutPin, LOW);
    }

#else
    pinMode (LED_A, OUTPUT);
    pinMode (LED_B, OUTPUT);
#endif

    Serial.begin (115200);
}

// -----------------------------------------------------------------------------
void loop (){
    if  (Serial.available ()) {
        CodeIn = getChar ();
        if  (CodeIn == '=') {
            EQUALS (); // The first identifier is "="
        }
    }
}

// -----------------------------------------------------------------------------
char getChar ()// Get a character from the serial buffer
{
    while (Serial.available () == 0)
        ;
    // wait for data
    return ( (char)Serial.read ());
}

void EQUALS ()      // The first identifier was "="
{
    int APState;
    int HHState;

    CodeIn = getChar ();
    // Get the second identifier
    switch (CodeIn) { // Now lets find what to do with it
    case 'a':   // we got an =a,
        APState = getChar ();
        //     1 = 0n, 0 = off
        if  (APState =='1'){
            digitalWrite (LED_A, HIGH);
        }
        else {
            digitalWrite (LED_A, LOW);
        }
        break;

    case 'j':  // we got an =a,
        HHState = getChar ();
        // 1 = 0n, 0 = off
        if  (HHState =='1'){
            digitalWrite (LED_B, HIGH);
        }
        else {
            digitalWrite (LED_B, LOW);
        }
        break;
    }

}
void EQUALS()
{      // The first identifier was "="
  CodeIn = getChar(); // Get the second identifier
  switch(CodeIn){ // Now lets find what to do with it

    case 'a':    // we got an =a,
    {
      int APState = getChar();   //     1 = 0n, 0 = off
      digitalWrite(22, APState == '1' : HIGH : LOW);
      break;
    }    

    case 'j':  // we got a =j,
    {
      int HHState = getChar(); // 1 = 0n, 0 = off
      digitalWrite(23, HHState =='1' ? HIGH : LOW);
      break;
    }
  }
}

(uncompiled, untested) should also fix it, by forcing the scope of the variables to be local.

Or avoid the problem completely

void EQUALS()
{      // The first identifier was "="
  CodeIn = getChar(); // Get the second identifier
  switch(CodeIn){ // Now lets find what to do with it

    case 'a':    // we got an =a,
      digitalWrite(22, getChar() == '1' : HIGH : LOW);
      break;

    case 'j':  // we got a =j,
      digitalWrite(23, getChar() == '1' ? HIGH : LOW);
      break;
  }
}

Thank you so much for your reply, FromalyKnownAs, and GCJR ! So so grateful, I hope to have some time tonight to have a crack on these options! thanks so much again!

Nice little code there to! didn't know the ? : thing; that's "going straight to the pool room" as we say here is Australia ;o)

Update// They both worked, thank you!! I will run with the simple one-line code,

digitalWrite(23, getChar() == '1' ? HIGH : LOW);

This one seemed to be less picky, nice and direct!