Let's Make Robots!

Talk to my computer through Serial?

Long time no see LMR!!!!

This semester was my most brutal yet, but the rewards have been priceless!

 

Anyways, back to the robots,

So my current project is to create a virtual indentity that I can have a VERY simple conversation with. His name is Sergei, cause everyone needs an awesome Russian buddy. But I find myself being a bit stuck on one thing: Serial Communications

 

What Im really trying to better understand is how to make the Arduino respond to my serial input. 

Here is another link detailing it:  

I know all the information is there but I just keep missing something..... I cant seem to get any of it to work. 

 

For a test, I want to say "Hola" in the serial input and the computer says "welcome, Doug" in response. I cant imagine its a very complicated code but if anyone has seen any examples, please share them!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
merser's picture
int ledpin = 13;
String readString;


void setup() {
    digitalWrite(ledpin,OUTPUT);
    digitalWrite(ledpin,LOW);
    Serial.begin(9600);
}

void loop() {
    char character; // may need to be of type String instead
    while(Serial.available()) { // wait for input
      character = Serial.read();
        
      if (character != '\n') {
        readString.concat(character); // get it
      }
        
    }
    
    delay(100);
    
    if (readString == "on") {// checks for on
        digitalWrite(ledpin, HIGH);   // set pin to high
        Serial.println("Led On");
    }

    if (readString == "off") {// checks for off
        digitalWrite(ledpin, LOW);     // set pin to low
        Serial.println("Led Off");
    }

    readString = "";
}

The thing with serial on the arduino is the instructions don't follow the normal order you'd expect.
Hence the delay I added to allow time for all the characters to be read from buffer. Otherwise the
string "readString" is cleared before all the characters are fetched and compared.

The code compiles and uploads well, it doesnt seem to control the LED on the arduino board (havent hooked it up to an external LED) but the serial responses are perfect! Now that I have a working code I need to learn what makes it work ;)

 

Ive read through it and really only one part confuses me:

 if (character != '\n') {

 readString.concat(character); // get it

 

Can you explain this portion to me?

merser's picture

I'm a bit puzzled why the led doesn't respond for you, as I tested the code on my arduino duemilanove 100 percent.
 Is your led on pin 13?

The section of code tests if the next character is a carriage return, if not then add the character to readString.
A carriage return is when you hit enter and is usually how one determines the end of a communication in serial.

 

birdmun's picture

\n is the special character for line feed
\r is the carriage return character

@DougG3: If you look for escape characters, you will find a list of 10 or 15 that include \t (tab), \\ (\), \' ('), among others.

@merser: it would seem the only thing my code was missing is a delay.

merser's picture

Yes \n for new line, doh! regardless it's the terminator used for serial.

Other than the delay,  you missed an equals sign for your INTERMINATOR assignment otherwise yeah yours should work.
I thought you had a good start on the problem, I just picked it up and finished it, plus had the advantage of having an arduino to test it on.

I have this code that takes whatever i type into the serial monitor and repeates it....

here is the next step:

creating an "if" statement thatacts upon what I type in...here is my best effort, and my epic fail lol:

 

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.println(readString);

 

 

    readString="";

  } 

}

@Birdmun, your code sample complies but same as before, there is no response from the computer in the serial monitor window :/

 

@ Chris, Im all about simplicity! Ive just never seen a code like that, or at least one that uses words instead of numbers lol

birdmun's picture

Have the arduino print what is in the variable readString. After that have it print the ASCII characters that are part of the readString variable. You may find that there is an extra character somewhere that you need to take into account. You can usually add special characters to the comparison string by way of \0 for null \n for line feed \r for carriage return and so on.

almost forgot, I made a new account to get rid of that "spam" thing that none us uf could figure out :/

Maxhirez's picture

I still can't send you a private message for some reason!