Help me please. I want to send via serial for example "ra" string and then to receive an analog value. It works if I send "ra" but when I send anything else but not "ra" and I send back again "ra" it doesn't print the value on serial monitor. Then I want to send from Processing "ra" and see there the value (if somebody want please advice me about that).
Now you are clearing the input string if the first character received is not "ra" which it never will be. Read two characters when they are available before testing for "ra"
Now you are clearing the input string if the first character received is not "ra" which it never will be. Read two characters when they are available before testing for "ra"
I am a newbie, please be more specific. Sorry you deal with noobs but I want to learn.
Serial data arrives SLOWLY. Expecting to be able to read two characters is not logical.
You REALLY need to send data with some end-of-packet marker. Then, you read and store data until that end of packet marker arrives. Only then do you compare what you have stored to known values.
If the Serial Monitor is sending the data, it can append a carriage return, line feed, or both the the data you send. Either one makes a good end of packet marker.
Not sure just what you are trying to do, but below is some very basic serial control code.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if (readString == "on")
{
digitalWrite(ledPin, HIGH);
Serial.println("LED ON");
}
if (readString == "off")
{
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
}
readString="";
}
}