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.
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!
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.).
@ 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!
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.
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.
voidloop() { 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:
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.
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.