I'm new in programming one arduino uno and i want to know whats wrong in the code below.
If i write the code only with Serial.read() the code works, and if i write the code only with Serial.parsInt() the code also works, but both together d'ont work.
If i write in the serial monitor something with only Serial.read in the code, he write the same thing in the lcd. If i write a ascii code in the serial monitor with only Serial.parseInt in the code, he write the rispective character in lcd. If i use the code above he d'ont write nothing than i understand.
I continue the discussion here because you posted on two forums...
Try to send "48A" from the serial monitor. Do you get "0" (the ASCII character #48) and then the "A" (or 65)?
If i send "48A" he write two charaters, the firts is undifined, the second is "A". If i send "48" he write two characters undefined and do the same with "65".
profpjmg:
If i send "48A" he write two charaters, the firts is undifined, the second is "A". If i send "48" he write two characters undefined and do the same with "65".
I copied your code and used the serial monitor as output instead of an LCD.
When I sent it "48A" (as one string, not each char separately!) I got "0" in one line and "A" on the next, as expected.
Both Serial.read() and Serial.parseInt() remove data from the input buffer. Whichever you call first gets the data so it will be gone when the second one tries.
It's a little more complicated because Serial.read() only takes one character whereas Serial.parseInt() will take several - until it has a valid number.
You can use the two of them in succession (in any order) as long as you have planned for what they will do.
Personally I prefer to read all the data first and then figure out what was received. This demo shows what I mean.
Let me be more specific. If I use in the code only Seria.read() he read and write in the lcd (or in the serial monitor if I use Serial.write()) everything I send from the serial monitor.
If I use in the code only Serial.parseInt() he read and write in the lcd (or in serial monitor) only the ASCII code, for example: if I send from the serial monitor "48" or "122" he write in lcd (or serial monitor) "0" and "z". Anything else like "xpto" he d'ont write nothing than I can understand. But, if I send "xpto122xpto" he write in the lcd (or serial monitor) "z" the character corresponding to the 122 ASCII code. It work with any other similar situations.
If I use in the code Serial.parsInt() and Serial.read(), at the same time, like in the code above, if the Serial.parseInt() read two or three numbers send from the serial monitor, Serial.write(valA) write in the lcd (or serial monitor) the character corresponding in ASCII code, but Serial.read() and lcd.write(valB) they read and write only one character
Example: if I send "122A" he write "zA"; if I send "xpto122A" he write "zA"; if I send "xpto122Axpto" he write "zA_" and "_" is space, and "" is a undefined character.
My ask is: the Serial.read() and lcd.write(valB) should not read and write everything is send from the serial monitor?
You will notice that the undefined character appears some time after the others. This means you reached the timeout of parseInt. In your example, when you send "xpto122Axpto":
The pasreInt reads until the end of an integer, i.e. until the "A". So you get 122 = "z".
Then the Serial.read reads the next character available, "A"
Now there's "xpto" left in the queue, but your program loop is back at parseInt. This function therefore waits and waits for an integer but all it sees is "xpto", so finally it gives up and returns an undefined value (probably -1) which displays as the odd character.
Still can't figure out just what the OP is needing, but below is some servo test code that receives commands sent from the serial monitor. The format of the commands has a numerical value first followed by a servo identifier, followed by the data delimiter. This data arrangement seems close to what the OP is describing.
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod; // create servo object to control a servo
void setup() {
Serial.begin(9600);
//myservoa.writeMicroseconds(1500); //set initial servo position if desired
myservoa.attach(6); //the pin for the servoa control
myservob.attach(7); //the pin for the servob control
myservoc.attach(8); //the pin for the servoc control
myservod.attach(9); //the pin for the servod control
Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or combined like 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}