Send command from Serial Monitor to Arduino without halting code

I need pointers please ..

I want to send a command from the Serial Monitor on the PC to the board ( USB connected ).

From what I understand with Serial.read(), it will sit and wait for input from the Serial Monitor and no other code in the loop is processed until it reads something.

Is there a way to get it to continue with the rest of a code sketch until it receives something from Serial Monitor ?

From what I understand with Serial.read(), it will sit and wait for input from the Serial Monitor

Then you don't understand. Have a look at the documentation over at the main site. http://arduino.cc/en/Serial/Read

Thanks again AWOL

Learning fast.

Got it to convert the serial bytes to a string, and found a way to clear the string using a selected character (~)

#include <LiquidCrystal.h>

//pin numbers for LCD display
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

int incomingByte = 0;	// for incoming serial data
String ReadString;
String LastString;

void setup() {

  Serial.begin(9600);
  lcd.begin(16, 4);

}

void LCDpos(int Lcol, int Lrow) {
  lcd.setCursor(Lcol, Lrow);
  if(Lrow > 1) lcd.setCursor(Lcol + 16, Lrow - 2);
}

void loop() {

  if (Serial.available() > 0) {

    char c = Serial.read();
    LastString = c;

    if(LastString == "~"){
      ReadString = "";
      Serial.println("Cleared String");
    }else{
      ReadString += c;
    }

    Serial.print("I received: ");
    Serial.println(ReadString);

    if(ReadString == "Dump Data"){
      LCDpos(0,3); lcd.print("Dumping Data");
    }
  }
}

Wouldn't

if(c == '~'){

be much simpler?

A perfect example of how all that experience makes it look so easy for you :slight_smile:

I was trying :

    if(c == "~"){

and couldn’t understand why it didn’t work.

Beg/Borrow/Buy a copy of "The C Programming Language" by Kernigan & Ritchie. K&R walks you through all this stuff: the difference between string and character literals, pointers versus data, and a bunch of other fun things. I found it quite useful due to my history as a Pascal/Prolog/Perl/Python/Ruby programmer.

I was trying :

Code:

if(c == "~"){

and couldn't understand why it didn't work.

That's because "c" is a string literal, but 'c' is a character literal.

getting fancy now.

Have upgraded so it clears the String whenever the ~ is found :

  if (Serial.available() > 0) {

      char c = Serial.read();
      ReadString += c;

      if(ReadString.indexOf('~') > 0){
        ReadString = "";
        Serial.println("Cleared String");
      }

    Serial.print("I received: ");
    Serial.println(ReadString);

    if(ReadString == "Dump Data"){
      LCDpos(0,3); lcd.print("Dumping Data");
    }
  }

Thanks also to ManicDee for the info

AWOL: That's because "c" is a string literal, but 'c' is a character literal.

Just goes to show how spoilt I was with years of VB5 coding.

A slightly easier (IMHO) read than Kernighan and Ritchie is Kelley and Pohl's "A Book on C"

Be aware that using the String type can cause you trouble later - it’s a great abstraction, especially when you’re starting out with the Arduino, but manipulating Strings involves dynamic memory allocation behind the scenes. As Arduino has little RAM, this can quickly lead to fragmentation and unexpected failures. Think about using char arrays instead for production code, even though it is more work, especially if it will run for a long time.