Why won't my switch case work?

I'm very new at arduino programming. I'm trying to make a project, in which there are 3 leds and one switch. When pressing the switch, a random led should turn on. I used a switch case for that...

int pin1 = 13;
int pin2 = 12;
int pin3 = 8;

int switchPin = 4;

void setup() {
  Serial.begin(9600);
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(switchPin, INPUT);

}

void loop() {
  int inByte = Serial.read();
  int buttonState = LOW;
  buttonState = digitalRead(switchPin);
  
  
  if(buttonState == HIGH) {
    switch(inByte) {
      case 1:
        digitalWrite(pin1, HIGH),
        digitalWrite(pin2, LOW),
        digitalWrite(pin3, LOW);
      case 2:
        digitalWrite(pin1, LOW),
        digitalWrite(pin2, HIGH),
        digitalWrite(pin3, LOW);
      case 3:
        digitalWrite(pin1, LOW),
        digitalWrite(pin2, LOW),
        digitalWrite(pin3, HIGH);
    }
  }
}

This wont work though. Any suggestions are very much appreciated!!

You are reading from the serial port before you know that there is anything available to be read. Look at the serial input basics tutuorial for information on reading serial data.

Data will be received as ASCII text. You might want to use the characters '1', '2', '3' in your case statements instead of numbers 1, 2, 3.

In addition to the serial.read mentioned above, Do you realize that inByte must be either 1 or 2 or 3 for the switch-case to select one of the cases? if inByte were 5 then the switch-case would basically be ignored.

You are also missing the "break" statements which allows only the selected case to be executed.

Switch case

Always put a "default:" in switch / case constructs even if all it does is 'break;'.

I’ve updated my code:

int pin1 = 13;
int pin2 = 12;
int pin3 = 11;

int switchPin = 4;

int buttonState = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("<Arduino is ready!>");
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(switchPin, INPUT);

}

void loop() {
  switchPressed();
}

void switchPressed() {
  buttonState = digitalRead(switchPin);
  
  if(buttonState == HIGH) {
    randomLED();
  } else {
    digitalWrite(pin1, LOW);
    digitalWrite(pin2, LOW);
    digitalWrite(pin3, LOW);
  }
}

void randomLED() {
  int randNumber;
  randNumber = random((1,3) + 1);
  buttonState = digitalRead(switchPin);
  switch(randNumber) {
    case 1:
      Serial.println("1");
      digitalWrite(pin1, HIGH),
      digitalWrite(pin2, LOW),
      digitalWrite(pin3, LOW);
      break;
    case 2:
      Serial.println("2");
      digitalWrite(pin1, LOW),
      digitalWrite(pin2, HIGH),
      digitalWrite(pin3, LOW);
      break;
    case 3:
      Serial.println("3");
      digitalWrite(pin1, LOW),
      digitalWrite(pin2, LOW),
      digitalWrite(pin3, HIGH);
      break;
  }
}

But it still doesn’t work… It ignores the switch when it’s pressed. (Honestly, everything just working not as intended…)

      digitalWrite(pin1, HIGH),
      digitalWrite(pin2, LOW),
      digitalWrite(pin3, LOW);

Whoops !
Did your semicolon key stop working ?

Another question
Have you got a resistor keeping pin 4 LOW when the button is not pressed ?

  randNumber = random((1, 3) + 1);

Check carefully what random(1, 3) returns
Try printing randNumber. Is its value what you think it should be ?
Try setting randNumber to a fixed number. Does the switch/case work then ?

Auto format (ctrl-t or Tools, Auto Format) would have shown the problem with the commas.
Code formatted with auto format with the commas:

switch (randNumber)
   {
      case 1:
         Serial.println("1");
         digitalWrite(pin1, HIGH),
                      digitalWrite(pin2, LOW),
                      digitalWrite(pin3, LOW);
         break;

After auto format with semicolons.

switch (randNumber)
   {
      case 1:
         Serial.println("1");
         digitalWrite(pin1, HIGH);
         digitalWrite(pin2, LOW);
         digitalWrite(pin3, LOW);
         break;

And if "compiler warnings" were turned on in preferences there would have been a warning for:

randNumber = random((1,3) + 1);
C:\Users\DaD\AppData\Local\Temp\arduino_modified_sketch_968006\sketch_dec21a.ino: In function 'void randomLED()':

C:\Users\DaD\AppData\Local\Temp\arduino_modified_sketch_968006\sketch_dec21a.ino:37:26: warning: left operand of comma operator has no effect [-Wunused-value]

   randNumber = random((1,3) + 1);

The random command should read

randNumber = random(1,4); // will return a random number between 1 and 3 inclusive. I think you had a typo in your code and it should read

randNumber = random(1,3)+1; but this would result in a value 2 or 3 as the random function returns either a 1 or 2 and you add a 1 to it.

Just an addition to the other comments :slight_smile: