Problem reading strings - Help needed


i trying write string perform task on arduino mega2560.
like turning on , off led 13.

my code works if serial monitor in no end line. in "nl & cr" not work.
what doing wrong??

code: [select]


string text;
boolean ledon = false;


void setup()
{
 
  serial.begin(9600);
  pinmode(13, output);
  digitalwrite(13, low);
 
}

void loop()
{

  while (serial.available()==0)
  {
   // waiting user input
  } 
  text = serial.readstring();
 
  if (text == "password ledon")
  {
    if (ledon == true)
    {
      serial.println(f("led on"));
      return;
    }
    else
    {
    digitalwrite(13, high);
    serial.println(f("led 13 on"));
    ledon = true;
    return;
    }
  }
  if (text == "password ledoff")
  {
    if (ledon == false)
    {
      serial.println(f("led off"));
      return;
    }
    else
    {
    digitalwrite(13, low);
    serial.println(f("led 13 off"));
    return;
    }
  } 
  else
  {
   serial.println(f("ohh snap, try again"));
  }
}

the "serial.readstring()" reads data until timeout (default 1 second).

so if type "hello\r", , compare in sketch if ( text == "hello" ) there no match, because '\r' part of string.

cr = '\r' = carriage return.
lr = '\n' = line feed


Arduino Forum > Using Arduino > Project Guidance > Problem reading strings - Help needed


arduino

Comments

Popular posts from this blog

arduino bug sur secteur, batterie, mais pas en usb?

Static ip - Raspberry Pi Forums

Printing to PDF adds printer margins