Reputation: 309
I'm using an Arduino Duemilanove to run some tests. I'm communicating with the arduino over a serial port. All i'm trying to do is read in a couple of ints, and i'm using the parseInt() function for its simplicity.
My question is, is there a way to program the ardunino to echo back each character as it is entered while still making use of the parseInt() function?
I know there are typically options to turn on local echo in a terminal clients, but i'd rather not rely on these.
Here is the code I'm using:
unsigned int timeTotal;
Serial.print("Enter Total Time of Period (ms): ");
while (!Serial.available()) { ; } //wait for input
while (Serial.available() > 0) {
//Serial.write(Serial.peek());
timeTotal = Serial.parseInt(); //read int
Serial.read(); //discard newline char at end of input
Serial.println();
Serial.print("Total Time: ");
Serial.println(timeTotal, DEC);
}
as you can see, i was trying to use a peek() but that only gets me one character....
Thanks in advance !!!
Upvotes: 3
Views: 1946
Reputation: 6187
You could read characters into a string (echoing them as you go) until your hit you delimiter. Then convert the string with atoi()
. Without looking at the source, I am sure this ia what parseInt does anyway.
Upvotes: 1