Offline
Newbie
Karma: 0
Posts: 12
|
 |
« on: January 30, 2013, 01:09:14 am » |
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. can anyone tell me why? void setup(){ Serial.begin(9600); pinMode(13,OUTPUT); digitalWrite(13,LOW); }
int byteRead = 0;
void loop(){ if(Serial.available()){ byteRead = Serial.parseInt(); if(byteRead>255)digitalWrite(13,HIGH); else analogWrite(13,byteRead); delay(500); } }
|
|
|
|
« Last Edit: January 30, 2013, 01:53:54 am by ahwahha »
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 118
Posts: 10148
|
 |
« Reply #1 on: January 30, 2013, 02:09:47 am » |
Please don't cross-post.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 12
|
 |
« Reply #2 on: January 30, 2013, 02:46:17 am » |
because I don't know where it exactly belongs to ...
How about the question? is it something about Serial.available() that I don't fully understand?
my point is, if I need to keep sending one integer separately but rapidly, how can that unexpected delay be exist?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 9
Posts: 1001
|
 |
« Reply #3 on: January 30, 2013, 03:28:49 am » |
Which board are you using ? Read the reference of every function you use. If you read this : http://arduino.cc/en/Reference/analogWriteAnd take a good look at your board, you notice that PWM is not possible with pin 13 on a Arduino Uno.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 12
|
 |
« Reply #4 on: January 30, 2013, 03:42:23 am » |
Which board are you using ? Read the reference of every function you use. If you read this : http://arduino.cc/en/Reference/analogWriteAnd 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.
|
|
|
|
« Last Edit: January 30, 2013, 03:44:18 am by ahwahha »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: January 30, 2013, 04:12:53 am » |
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. How does "parseInt" know when it has reached the end of the data? #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait (From "Stream.cpp") the last one always have a additional delay of hundreds millis seconds. Ten hundred, to be precise. Send an extra comma, or a line feed, carriage return - anything but a decimal digit.
|
|
|
|
« Last Edit: January 30, 2013, 04:24:31 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 12
|
 |
« Reply #6 on: February 01, 2013, 04:06:55 am » |
a, or a line feed, carriage return - anything but a decimal digit.
I got it!! THANKS!!!!!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 836
|
 |
« Reply #7 on: February 01, 2013, 04:14:43 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 12
|
 |
« Reply #8 on: February 01, 2013, 04:24:03 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1406
May all of your blinks be without delay
|
 |
« Reply #9 on: February 01, 2013, 04:29:11 am » |
"Close, but no cigar" Try '\r' for carriage return or '\l' for linefeed.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: February 01, 2013, 04:34:16 am » |
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. But "parseInt" takes care of that by waiting for input, or timing-out after (by default) one second. ("ten hundred milliseconds"). If anything, the "available" may not be necessary, depending on how frequently updates are sent. '\l' for linefeed. Close, but no cigar 
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 12
|
 |
« Reply #11 on: February 01, 2013, 04:35:59 am » |
"Close, but no cigar" Try '\r' for carriage return or '\l' for linefeed.
When I input : 50 \r or 50 '\r' I still got the same effect as I input : 50 0 Still don't get it.......
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #12 on: February 01, 2013, 04:38:29 am » |
Still don't get it.. It doesn't matter what you send, so long as it isn't a decimal digit.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 12
|
 |
« Reply #13 on: February 01, 2013, 04:49:05 am » |
Still don't get it.. 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..............?
|
|
|
|
« Last Edit: February 01, 2013, 04:53:59 am by ahwahha »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #14 on: February 01, 2013, 04:57:42 am » |
last input delay ( what I call it ) " Call it a timeout, because that's what it is. I'm struggling to see what the problem is.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
|