Pages: [1]   Go Down
Author Topic: [SOLVED] String comparison after serial read  (Read 355 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to compare a string created from characters sent over serial to a word. If the word "no" matches, led 13 should turn on, and if the word "off" matches, led 13 should turn off.
All goess right untill the comparison is made, I checked by writing back results to serial.
I just can't get it to work? Any ideas?

Code:
String inData;
String test;
int testLed = 13; // LED on pin 13

void setup()
{
  Serial.begin(9600);
}


void loop(){
  inData="";
  if (Serial.available() > 0) {
    int h=Serial.available();    
   h--;

    for (int i=0;i<h;i++){
      inData += (char)Serial.read();
       }
   Serial.read();
  }
  //print it out
  if (inData == "") {
    //There is no spoon!
  }
  else
  {
    Serial.println(inData);
    processData();
  }
  delay(50); // Without delay, no data is shown in serial monitor.
}

void processData(){
      String t = inData;
      Serial.println("processing");
      if (t == "on") {
      Serial.println("light go ON");
      digitalWrite(testLed, HIGH);}
      if (t == "off"){
      Serial.println("light be gone");
      digitalWrite(testLed, LOW);}


        
}
« Last Edit: December 13, 2013, 10:13:39 am by dannyvdz » Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First all the stuff with "h" is not needed!.

Second you need to check that you have the whole line (in this case).

Third you are not being sent on/off you are also getting newline or <cr><lf> depending on the monitors settings.

Mark

Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8452
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (Serial.available() > 0) {
    int h=Serial.available();    // h probably == 1 at this point, unless the entire string was sent before you entered loop()
   h--;                // now it's 0, not that this variable is required

    for (int i=0;i<h;i++){   
      inData += (char)Serial.read();  // so this never gets executed
       }
   Serial.read();  // but even if it did why read again?
  }

This is bazaar, you're doing two serial.reads() based on a single available() and anyway all this will be complete long before the second char is received let alone any more of the string.

And how are you testing for the end of the transmission?

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm quite new to this so I followed some tutorials and changed/added some stuff here and there to get it working the way it is now. I don't know how else to do it.

I have tried without h--; but then it displays the word and skips to a new line on my serial terminal.

The data i'm getting seems to be allright, it's just not comparable..
Logged

ivrea (to)
Offline Offline
Faraday Member
**
Karma: 57
Posts: 3527
miaaao ^-^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here (first row):
 
Code:
Serial.println(inData);
  processData();
You see on serial monitor the right word ?
How you set the central combo in Serial monitor (no line ending) ?
Logged

my name is IGOR, not AIGOR

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see the exact same word I sent from my phone to the arduino in the serial terminal on my computer.
I can see that processData() is being called since my serial terminal also sends the word processing and that's all that happens...
Logged

ivrea (to)
Offline Offline
Faraday Member
**
Karma: 57
Posts: 3527
miaaao ^-^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try with  .StartsWith:
http://arduino.cc/en/Reference/StringStartsWith
Code:
if ( t.startsWith("off") )

Try also this to be sure that before and after the word you send no other chars:
Code:
 Serial.print("#"); Serial.print(inData); Serial.println("#");
  processData();
Logged

my name is IGOR, not AIGOR

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For reading from serial you should read this: http://hacking.majenko.co.uk/reading-serial-on-the-arduino
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for saving my day! The #-check showed my output was correct so it was the comparison which was the problem.

if ( t.startsWith("off") ) worked like it should, my serial output showed up only the led didn't work but that was another problem..

(I forgot -->  pinMode(testLed, OUTPUT); ...)
Seems to be working great now!

Logged

0
Offline Offline
Tesla Member
***
Karma: 116
Posts: 8938
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple code to look for "on" and "off" to operate the board LED.

Code:
\// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3); 
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thx for that one, I'll have a look at it later this week since works gonna take up most of my time the next few days.. smiley-sad
Logged

Pages: [1]   Go Up
Jump to: