String Compare

Hi,
I am super new to Arduino. Board is awesome! Please see my question below.

Arduino 2560

Goal: Send COMMANDS from Win APP to Arduino Board. Board activate Voltage Pins Based on the Commands.

Issue: For some reason "If" statement is NOT working.

I can talk to the board thru my serial communication I send letter "A" over serial. And get Response letter "A" back.
Now when I put "If" statement. If serial reads "A" to do something its doesn't work.
I think statement that I use is NOT correct for this board or I am missing something very simple.
Typically in C if( s == t ){} to compare

Code below:

String acti;

void setup() {
// initialize serial:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() { }

void serialEvent() {
acti= Serial.readString();
if ((acti)=="z"){
Serial.print(acti);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
//Serial.print(acti); If this is active boards does receive the command
}

If you remove "If" statement everything works.
Please advise if more information is needed.
Thanks

Try 'z'

evanmars:
Try 'z'

I got an error
Warning: invalid conversion from 'char' to 'const char*' [-fpermissive]

if ((acti)=='z'){

For some reason "If" statement is NOT working.

Hold the front page.

Maybe what you're comparing isn't correct.

Just my tuppence-worth.

Please remember to use code tags when posting code

Sorry for the tags I am super new :slightly_frowning_face: here

I have a defined value in code and send value over the serial port. I see board is reading the value correct if I remove "If"

Case statements seems to work.
From previous error it seems like it is trying to set value instead of comparing it.....

Klide:
I got an error
Warning: invalid conversion from 'char' to 'const char*' [-fpermissive]

if ((acti)=='z'){

I see board is reading the value correct if I remove "If"

Maybe what you think you're reading, and what you're actually reading are different.

My money is on that, rather than your assertion that a simple 'if' isn't working.

What I usually do in cases like this is to bracket the printing "acti" with '<' and '>'

try

acti.indexOf("z") >= 0

or

acti.indexOf('z') >= 0

KASSIMSAMJI:
try

acti.indexOf("z") >= 0

or

acti.indexOf('z') >= 0

Thank you!" acti.indexOf("z") >= 0" - this worked!
Personal curiosity why it be index instead of compare?

Klide:
Thank you!" acti.indexOf("z") >= 0" - this worked!
Personal curiosity why it be index instead of compare?

white space (not seen) char in acti

you can check by

for ( byte x = 0 ; x < acti.length(); x++ ) {

Serial.println(acti.charAt(x));   // this returns ascii of all chars in acti

}

Oh I see! Thanks :slight_smile: