Talking to my computer through Serial

Hey all I just got my first Arduino Uno and joined the forum!!!!

Anyway here is my project idea:

To talk through the arduino through pre-scripted text. So I can communicate through the serial monitor window :slight_smile:

So if i type in "Hello"
the Arduino responds "Hello, Doug"

Ive been working through the basics of the "hello world" sketch, I just feel like Im missing the part where the computer reads what I type into the serial monitor page.

Any ideas or pages I can be directed to?

You can read the characters in with Serial.read and store it in either a String or array[], and use either IF statements or case statements to trigger a response.

Now depending on the number of responses you want it to have, will determine how long your code will be.

You might also be able to use the index function, to search for prelisted words.

"You can read the characters in with Serial.read and store it in either a String or array[], and use either IF statements or case statements to trigger a response."

Thats the part Im confused about, can you tell me a bit more about the string or array?

I'd suggest storing the input in a char array, because the String class has some issues outstanding at the moment.

You would need to initialise the serial port in setup() as normal, and in loop() you would need a code fragment to read from the serial port and process the data read. For example (incomplete and untested):

const int MAXLEN = 32;
int length = 0;
char buffer[MAXLEN+1];

void loop()
{
    if(Serial.available())
    {
        char ch = Serial.read();
        if((ch == '\r') || (ch == '\n'))
        {
            if(length > 0)
            {
                processMessage(buffer);
            }
            length = 0;
        }
        else if(length < MAXLEN)
        {
            buffer[length++] = ch;
            buffer[length] = 0;
        }
        else
        {
            // buffer full - discard received character
        }
    }
}

void processMessage(char *msg)
{
    // your code here
}

hmn... Ill have to take some time to work through that one lol. Most of it seems like gibberish.

But thank you so much for the help! I am getting to know the basics of C++ so Ill learn on for sure!

const int MAXLEN = 32;
int length = 0;
char buffer[MAXLEN+1];

int? length could be a byte, and save half the memory. Got to be careful wasting memory.

Very simple serial string capture code.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.print(readString);
    Serial.println(", Doug");

    readString="";
  } 
}

Thanks zoomkat! Thats just about exactly what I was lookin for. Ive been playing around with the sketch to make different phrases and such.

So I see in this example that anything i type in will be repeated back at me with ", Doug" on it. Now that I understand what that looks like Ill bee looking in to how to make responses that are unique to what I type in.

For example if i type in "hello"
the computer responds "hello,Doug"

but...

If I type in "how are you?"
the computer responds "just fine thank you"

Im not asking you guys to do all the work for me, just maybe an example or two of how serial.read can be used to create individual responses

If I type in "how are you?"
the computer responds "just fine thank you"

The below page has the functions you can use to extract information from your captured string. You might use the (readString.indexOf("ow are you?") to see if the string contains "how are you?" similar to the bottom code.

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            myservo.write(40);
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

ok, well ill read through more of that and keep trying at it. My first few attempts have failed but Ill get it!!!! Thank you for all the help and the links!

zoomkat:

    delay(2);  //delay to allow byte to arrive in input buffer

Could you explain why you're doing this?

PeterH:

zoomkat:

    delay(2);  //delay to allow byte to arrive in input buffer

Could you explain why you're doing this?

Quite simple. The delay slows down the checking of the arduino serial input buffer such that the buffer isn't checked for content prior to another byte of the string sent being available.

ok so I have been trying to get this modified code to work with no success, lets see what the problem is:

String readString;

void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

while (Serial.available()) {
delay(2); //delay to allow byte to arrive in input buffer
char c = Serial.read();

}

if (readString.indexOf("hello") >0) {
Serial.println("welcome, Doug");

readString="";
}
}

When I say "hello" it should say "welcome, Doug"

You might try the below as the h in hello would probably be at position 0 instead of 1 in the string being evaluated.

if (readString.indexOf("hello") >=0) {

So I understand what your saying, about the arduino not recognizing the "h". but I cant see any solution on that page you linked.

but I cant see any solution on that page you linked.

That is your homework study page. :slight_smile:

:slight_smile: thanks for the help

I'll keep going over it until I figure it out or at least come up with more concrete questions lol

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
  }

Please describe exactly what you think this is accomplishing.

Reading the characters and then throwing them away is a rather useless feat.