Guys, in short, I'm facing a question about the speed of serial communication,
I just start to try it and made the code at the bottom to test.
the action I want to test is
when I input " 50 0 170 0 255 0 " for example,
the led should " glow-> off -> glow brighter -> off -> glow full -> off "
the problem I am facing is that
the first 5 action go smooth with the delay I expected,
but the last action, the off here, no matter how many numbers I have input,
the last one always have a additional delay of hundreds millis seconds.
Read the reference of every function you use.
If you read this : analogWrite() - Arduino Reference
And take a good look at your board, you notice that PWM is not possible with pin 13 on a Arduino Uno.
Read the reference of every function you use.
If you read this : http://arduino.cc/en/Reference/analogWrite
And take a good look at your board, you notice that PWM is not possible with pin 13 on a Arduino Uno.
mega 2560
there is no problem of PWM
and if this is a PWM problem it shouldn't be a delay
my problem is the delay when there is just one integer sent into the board,
or when there is only one unread integer left in the buffer.
michinyon:
when there is a serial character available, you got no idea how many. You cannot necessarily
assume that the whole three character number is there.
I don't get it. Not understanding............
and now I know I need to tell arduino it's the end?
but how?
what is a carriage return?
I tried :
/r
'/r'
but still a delay there and at the end turn off the LED,
just like what I add is a digit zero at the end.
It doesn't matter what you send, so long as it isn't a decimal digit.
So now the case is that I need to send a decimal digit
then send a decimal digit again within 1000 ms
but the first "command" still do not have effect due to the " last input delay ( what I call it ) "
and the second "command" seems to "merge" to the first and result as some unwanted brightness.
for example if I type 2 then press enter, then type 55 then press enter,
I result as if I type 255 and press enter......
And i suppose if the first command response immediately,
the second one will also response immediately just after I press 'enter'
Now what i need to do to solve this is..............?
When I input :
50 \r
or
50 '\r'
I still got the same effect as I input :
50 0
Still don't get it.......
Input that where? If you are using the serial monitor to send the data, and you enter "50\r", the characters '5', '0', '', and 'r' are sent (along with maybe others). The characters '' and 'r' are not a single, special-meaning, character.
If you are using something other than the Serial Monitor application, we'd really need to see the code/know what the problem is.
Code that uses a comma as a delimiter between the desired data packets.
//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}
void loop() {
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
//if (readString.length() >0) {
Serial.println(readString); //prints string to serial port out
//do stuff with the captured readString
if(readString.indexOf("on") >=0)
{
digitalWrite(ledPin, HIGH);
Serial.println("LED ON");
}
if(readString.indexOf("off") >=0)
{
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
}
readString=""; //clears variable for new input
//}
}
else {
readString += c; //makes the string readString
}
}
}
but how come my code get a timeout?
when there is still one digit there but not none?
Because that's how the time out works - if there's one digit available, it has to wait to see if there's going to be another or a terminator , I.e. a non-digit.
If there's already a non-digit, then the parse routine on was the previous number is complete.