Mutiple if loop not working

Hi i am writing to the digital line on the mega.
and when i use a single character it works but when my characters get longer it no longer works
is issue in part becuase im not using the ascii table correctly?
example :
if(labview == '1')my led works

vs

if(labview == 'CH1_HIGH') my led will not work

im using the serial monitor to type in my input

i have leds connected to pins and grounds.

completed code:

int DIO_1 = 12;
int DIO_2 = 13;
char labview = 0;

void setup()
{
 Serial.begin(9600);
 pinMode(DIO_1,OUTPUT);
 pinMode(DIO_2,OUTPUT);
}
void loop()
{
 if (Serial.available()> 0)
  {
  labview = Serial.read();

//CH1
if(labview == 'CH1_HIGH')
  {
  digitalWrite(DIO_1,HIGH);
  }  
else if(labview == 'CH1_LOW')
  {
  digitalWrite(DIO_1,LOW);
  }
else if (labview == 'CH2_HIGH')
  {
  digitalWrite(DIO_2,HIGH);
  }
else if (labview == 'CH2_LOW')  
  {
    digitalWrite(DIO_2,LOW);
  }
}
} 
if(labview == 'CH1_HIGH') my led will not work

labview is declared as a single char and you read a single character into it. Can you see why there will never be a match with a multi byte expression ?

To add to the problem single quotes are used to denote that the value is a single char

See Serial input basics - updated for advice on receiving and parsing multi character strings

1 Like

That was not helpful.
It did not describe the double brackets and the example was nothing like what im trying to do.

From what your saying "labview is declared as a single char and you read a single character into it. --just how do i change that so i can read more than one characters.

i can see it does not work and you explained whats wrong but i cannot fix it if i dont have an example.

and
To add to the problem single quotes are used to denote that the value is a single char, i need an example. ive searched but found poor examles

Looks like you didn't really take the time to readk @UKHeliBob's response. Try again and read carefully:

1 Like

I'm a total amateur hobbyist here: what @UKHeliBob speaks is the truth: the referenced thread is legend imo, I live by the sword of @Robin2 's incredible and well explained examples. I was lost before I took the 45 minutes it takes to learn what's there. No joke.

i did see all that
its like 30 lines of code to read another character.

i though there would be a better declaration with some type of initialization.

Thank for the reference

Your wording is a bit odd but have a look at the functions in the Serial reference.

Note that Robin's tutorial provides a very good approach and is non-blocking code. Please don't complain if the functions in the reference cause your code to slow down (block) or, when using functions with String (capital S) objects, cause your code to crash. Note that the latter is not a bug in Serial implementation but caused by your incorrect use of String objects.

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