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!

Just to add.

If you don’t know what an array is, it is a single variable name with multiple indexes. Think of a row of mailboxes for an apartment building. You have 2311 N Los Robles. There are 8 apartments in that building, so, you have 2311 apt 1, 2311 apt 2, etc. Until you reach 2311 apt 8. The boxes downstairs are all for 2311 N Los Robles, but, there are apartment numbers that differentiate the mailboxes.

With arrays you can store a single variable type in each “mailbox”. The variable type must be the same for all of the mailboxes at that address. So, you can have an array of numbers, or, an array of letters; this is a generalization. There are many bit lengths of numbers from 1 bit(boolean) to multiples of 8 bits(byte, int, float, double, long).

If we have a char array named word[5], and it is filled with the the batch of characters ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, you can access any letter in that array by calling out its index (aLetter = word[1] // aLetter would have the value ‘e’. Indexes start with 0 on arrays.).

I can keep going, if I need to. :slight_smile:

thanks guys!

@ max Im gonna cancel this account and start up a new one… none of the admins can figure out whats wrong with my account lol, so we can pm about meeting. I get my work schedule on mon. so i can tell you then!!!

The future of this project is to design a Siri like system (using written words instead of voice command) The idea came from two places, Siri itself and Igor the assistant, a project listed here on LMR. Essentially, I want this to be a fun little side prject with the possibility of making a low-grade assistant. 

@birdmun, I kind get your mailbox analogy, but Im having trouble visualizing it in code for arduino lol.  Like, I understand the logic of it, but dont understand what that would look like.

ok, heres an example…

I worked on this code last night but just cant seem to get it to work (but it DOES compile correctly, so no syntax errors)

 int ledpin = 13;

        String readString;

 

       void setup() {

 

         digitalWrite(ledpin,OUTPUT);

         digitalWrite(ledpin,LOW);

 

         Serial.begin(9600);

       }

         void loop()

         {

 

 

          if(readString.indexOf(“on”) >0)//checks for on

          {

 

            digitalWrite(ledpin, HIGH);    // set pin to high

            Serial.println(“Led On”);

          }

          if(readString.indexOf(“off”) >0)//checks for off

          {

 

            digitalWrite(ledpin, LOW);    // set pin to low

            Serial.println(“Led Off”);

          }

          //clearing string for next read

          readString="";

         }

 

 

What im trying to do is control the led on my arduino uno and have it write wether or not the led is in fact on. Instead the light is always on and it doe not respond to my serial input :confused: Im still workin at this, but if anyone sees my error please let me know!

More simple-er

I would start by sending one single byte as apposed to a string. Stings are funky in general and hard to work with --no reason to add this confusion. Also, there is typically a if(Serial.available()>0) in there as well. I would do this:

if (Serial.available()>0)

variable= read the serial
if (variable = whatever)
do something

Serial.flush()

 

Also, no one has mentioned Processing yet. This is something you should really look into. The code itself is almost identical to Arduino and is very easy to pick up. There are even serial examples in the Arduino IDE for arduino/processing talk.

 

 

Your biggest issue?

You failed to actually “read the string.” :stuck_out_tongue:

I was going to adjust your code when I ran across http://arduino.cc/forum/index.php?topic=53773.0

He is working toward a similar end.

I decided to attempt adjusting your code anyway.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int ledpin = 13;
String readString;
const int INTERMINATOR 13; // ASCII for carriage return

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();

    <span style="color: #008800; font-weight: bold;">if</span> (character != INTERMINATOR) {
        readString.concat(character); <span style="color: #888888;">// get it</span>
    }
}

readString.concat(<span style="color: #dd2200; background-color: #fff0f0;">"</span><span style="color: #0044dd; background-color: #fff0f0;">\0</span><span style="color: #dd2200; background-color: #fff0f0;">"</span>); <span style="color: #888888;">// null terminate the string</span>

<span style="color: #008800; font-weight: bold;">if</span> (readString == <span style="color: #dd2200; background-color: #fff0f0;">"on"</span>) {<span style="color: #888888;">// checks for on</span>
    digitalWrite(ledpin, HIGH);   <span style="color: #888888;">// set pin to high</span>
    Serial.println(<span style="color: #dd2200; background-color: #fff0f0;">"Led On"</span>);
}

<span style="color: #008800; font-weight: bold;">if</span> (readString == <span style="color: #dd2200; background-color: #fff0f0;">"off"</span>) {<span style="color: #888888;">// checks for off</span>
    digitalWrite(ledpin, LOW);     <span style="color: #888888;">// set pin to low</span>
    Serial.println(<span style="color: #dd2200; background-color: #fff0f0;">"Led Off"</span>);
}

<span style="color: #888888;">//clearing string for next read</span>
readString=<span style="color: #dd2200; background-color: #fff0f0;">""</span>;

}

All I can say is, it compiles. :slight_smile:

I borrowed code from here and there around the web.

thanks again

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

 

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

oops…

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

Try doing some debugging then.

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.

ok here we go!

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="";

  } 

}

If you haven’t had any luck yet, try this

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.

thanks!

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 :wink:

 

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?

I’m a bit puzzled why the

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.

 

Actually,

\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.

Yes sorry about that mistake

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.

Didn’t toggle my 2560’s pin 13 led

but changing to pin 23 works an external led just fine. Thanks, everybody!