I want to know how to set multiple letter in switch case

Hi, I want to know how to set multiple letter in switch case because when I set my command key to a specific word then enter it in the serial monitor it doesn't work. can you help me guys Im just a beginner, here is my program.

int ledPin=9;
float potentiometermaxvalue = 5000;
float resistance;
float voltage;
float brightness;
float potentiometer;
float LEDvoltage;
float Pvoltage;
int serial;

void setup()
{
pinMode(9,OUTPUT);
Serial.begin(9600);
}

void loop()
{
if(Serial.available()>0)
{
serial= Serial.read();
}

switch(serial)
{
case'Vled':
potentiometer = analogRead(0);
resistance = 1000-((potentiometermaxvalue/1023)potentiometer);
brightness = potentiometer/4;
LEDvoltage=(5
brightness)/255;
Pvoltage = 5-LEDvoltage;
Serial.print("LEDvoltage=");
Serial.println(LEDvoltage);
analogWrite(9,brightness);
delay(1000);
break;

case 'Vpot':
potentiometer = analogRead(0);
resistance = 1000-((potentiometermaxvalue/1023)potentiometer);
brightness = potentiometer/4;
LEDvoltage=(5
brightness)/255;
Pvoltage = 5-LEDvoltage;
Serial.print("Pvoltage=");
Serial.println(Pvoltage);
analogWrite(9,brightness);
delay(1000);
break;

default:
Serial.println("Enter Vpot or V");
Serial.println("Invalid character");
delay(1000);
break;

}

}

switch() only works with an integer or an expression that resolves to an integer so multi character commands won't work. Use if/else instead and strcmp() to do the comparison as you can't compare multi character strings using ==

But first you need to get the input into a multi character string. Serial.read() reads only a single character

See Serial input basics - updated for some ideas on how to read a string in

A simpler way to do it is to use Serial.readString() and to use Strings (capital S) instead of c style strings (zero terminated arrays of chars) but Strings are not well regarded for use in small memory footprints

Welcome Please edit your post and add code tags. The simplest way is probably the below approach.

  1. In the IDE, use edit -> copy for forum.
  2. Edit your post and replace your current code with what you just copied.
  3. Save your post.

If you don't use code tags, your code in the post is corrupted (e.g. italic, partial code tags etc).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.