Go Down

Topic: The time between data recieved (Read 1 time) previous topic - next topic

JRMN

How do I change the below code to tell me the amount of time in milliseconds between data received?

Code: [Select]

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); //RX,TX

int incomingByte = 0; // for incoming data

void setup() {
 Serial.begin(19200);
 mySerial.begin(19200);
}

void loop() {
 char option;
 
 // Output to serial monitor only when you receive data
 if (mySerial.available() > 0) {
   incomingByte = mySerial.read();      // read the incoming byte:
   Serial.print(incomingByte);
}

}

Arrch

Use millis() to get a timestamp. Save said timestamp and when the next byte is received, use it again and subtract to find the time inbetween.

JRMN

Thanks, I get that but for some reason I can't warp my head around it.  In order for it to work it has to all be done in the if statement.

Arrch

#3
Jun 22, 2012, 12:49 am Last Edit: Jun 22, 2012, 03:42 am by Arrch Reason: 1

Thanks, I get that but for some reason I can't warp my head around it.  In order for it to work it has to all be done in the if statement.


Code: [Select]
unsigned long lastTime;

That should be put in the global scope.

To get the current time:

Code: [Select]
unsigned long currentTime = millis();

To get the difference:

Code: [Select]
unsigned long timeSince = currentTime - lastTime;

JRMN

hmm... that doesn't seem right.

Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); //RX,TX

int incomingByte = 0; // for incoming data

unsigned long lastTime;

void setup() {
 Serial.begin(19200);
 mySerial.begin(19200);
}

void loop() {
  unsigned long currentTime = millis();
 
 // Output to serial monitor only when you receive data
 if (mySerial.available() > 0) {
   unsigned long timeSince = lastTime - currentTime;  
   incomingByte = mySerial.read();      // read the incoming byte:
   Serial.print(incomingByte);
   Serial.print("\t");
   Serial.println(timeSince);
}
}

WizenedEE


jraskell

That's because you aren't setting your lasttime variable.

Add
Code: [Select]
lastTime = currentTime; after you computer timesince.

JRMN

So this should work?

Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); //RX,TX

int incomingByte = 0; // for incoming data

unsigned long lastTime;

void setup() {
  Serial.begin(19200);
  mySerial.begin(19200);
}

void loop() {
   unsigned long currentTime = millis();
 
  // Output to serial monitor only when you receive data
  if (mySerial.available() > 0) {
    unsigned long timeSince = lastTime - currentTime; 
    incomingByte = mySerial.read();      // read the incoming byte:
    Serial.print(incomingByte);
    Serial.print("\t");
    Serial.println(timeSince);
    lastTime = currentTime;
}
}


I know there are 1000 milliseconds in a second.  Is it safe to say that 4065 - 4045 = 20 milliseconds?

JRMN


Nick Gammon

Code: [Select]
    unsigned long timeSince = lastTime - currentTime; 


currentTime will be higher won't it? It is more recent. So you need the subtraction around the other way.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

JRMN

See, it's not just me.  I know there are 1000 milliseconds in a second.  Is it safe to say that 4065 - 4045 = 20 milliseconds?

Nick Gammon

Certainly you can say that. 4065 dogs - 4045 dogs = 20 dogs
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

AWOL

Quote
Is it safe to say that 4065 - 4045 = 20 milliseconds?

Most of the time, yes, it is.
Of course, your timer could have wrapped, but that only after about seven weeks.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

JRMN

Ok thanks everyone.  I still haven't had a chance to test it out, but worst case, I can't just use Serial.print(millis()); and just manually subtract them.

Nick Gammon

Why buy a dog and bark yourself? The processor knows how to do subtractions. Make sure you subtract the right way around as I mentioned above.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up