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