Help with Serial.Read() getting string.


int dword;
void setup(){

 serial.begin(9600);
 serial.write("power on");
}

void loop(){
dword=serial.read();

if(dword=='1'){
 serial.write("\nmotor 1 -> selected\n");
}
if(dword=='2'){
 serial.write("\nmotor 2 -> selected\n");
}

/*if*dword=="m1"){
//do.....
}*/
}


how can can type strings? instead of single chars,

there couple of ways go it.

you read byte (that's serial.read returns; not int), , make decision based on character, read again.

or, read available data, , store in array of characters.

code: [select]
char indata[20]; // allocate space string
char inchar; // store character read
byte index = 0; // index array; store character

void loop()
{
  while(serial.available() > 0) // don't read unless
                                                 // there know there data
  {
      if(index < 19) // 1 less size of array
      {
          inchar = serial.read(); // read character
          indata[index] = inchar; // store it
          index++; // increment write next
          indata[index] = '\0'; // null terminate string
      }
  }
  // string (but not using ==)
}


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Help with Serial.Read() getting string.


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums