Go Down

Topic: String Compare (Read 311 times) previous topic - next topic

Klide

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

evanmars


Klide

Try 'z'


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

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

AWOL

#3
Jan 11, 2019, 08:53 pm Last Edit: Jan 11, 2019, 08:53 pm by AWOL
Quote
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

Klide

Sorry for the tags I am super new :smiley-sad: 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.....


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

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


AWOL

#5
Jan 11, 2019, 09:02 pm Last Edit: Jan 11, 2019, 09:04 pm by AWOL
Quote
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 '>'

KASSIMSAMJI

try

acti.indexOf("z") >= 0

or


acti.indexOf('z') >= 0
Expert To Be: Machine Learning And Computer/Machine Vision (ML/CV/MV)

Klide

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?

KASSIMSAMJI

#8
Jan 11, 2019, 09:17 pm Last Edit: Jan 11, 2019, 09:18 pm by KASSIMSAMJI
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

Code: [Select]


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

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

}

Expert To Be: Machine Learning And Computer/Machine Vision (ML/CV/MV)

Klide


Go Up