Pages: [1]   Go Down
Author Topic: Serial comms without Delay()  (Read 833 times)
0 Members and 1 Guest are viewing this topic.
Katy, TX, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 36
[ch937]
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In my projects I always try to stay away from the Delay() command. I have adapted the code from the blinkwithoutdelay example to suit just about all my needs. Now I ordered a ft232rl breakout board and I will soon have my arduino conversing with my computer. I read the Making Things Talk book (a couple of parts I only skimmed through, maybe I missed it) and the examples provided, and I have yet to see an example where Delay() is avoided when communicating via serial. Is it possible? What would the code look like for that? This seems like it must be a common question but I didn't find it when I searched the forums. (sorry in advance if it's already been answered) Thanks!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 630
Posts: 49998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it possible?
Of course it is. I never use delay in any serial communications application. What is required, instead, is clearly defined start and end of packet markers.

Then, read the data that is available, and store data starting after the start of packet marker. Read all available data. No nothing with the data until the end of packet marker arrives. When it does, stop reading, and deal with the complete packet.

Search for "started && ended" for an example.

Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What I would do is to have the code as if the delay was in there, but then replace the delay with the micros() or millis() commands like in the BlinkWithoutDelay expample.

My code looks a little bit like this:

unsigned long previousTime;
unsigned long currentTime;

void setup() {
  Serial.begin(9600);
}

void loop() {
  currentTime = micros();
  if (currentTime - previousTime > 100000){
    Serial.println("It's Time");
    previousTime = currentTime;
  }
}

(I hand wrote it in this post, forgive any errors please)
Logged

Katy, TX, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 36
[ch937]
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


PaulS,
I did exactly that. I searched for your posts on started && ended and I think I have isolated what will work for me; thank you very much! In the process I also couldn't help but notice that far too often when you give somebody the answer that they are looking for, they don't even acknowledge it. They repeat their original question, and they continue rambling on about things that are already proven to not work, or about things that other people posted (just an observation). That must get very irritating. Hang in there and don't give up on people (obviously, you haven't yet). I think sometimes your responses are just over people's heads (or maybe I'm just projecting my own sentiments onto them) so they don't know how to reply. As soon as I get my usb>serial board I am going to meddle around with your code until I understand it. Thank you again for pointing me in the right direction.

WizenedEE,
That is exactly what I was looking for when I posted this, but in the process I was shown something which has the capacity for error checking. If you haven't already, I suggest you also read PaulS's posts. Thanks!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 630
Posts: 49998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@strantor
Thank you for the kind comments.
Logged

Pages: [1]   Go Up
Jump to: