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.

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. :)

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

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 :/ Im still workin at this, but if anyone sees my error please let me know!

You failed to actually "read the string." :P

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();
        
        if (character != INTERMINATOR) {
            readString.concat(character); // get it
        }
    }
    
    readString.concat("\0"); // null terminate the string
    
    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");
    }

    //clearing string for next read
    readString="";
}

All I can say is, it compiles. :)

I borrowed code from here and there around the web.

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.

 

 

@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

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

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.

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

  } 

}

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.