Pages: [1]   Go Down
Author Topic: The time between data recieved  (Read 919 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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);
}

}
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
unsigned long lastTime;

That should be put in the global scope.

To get the current time:

Code:
unsigned long currentTime = millis();

To get the difference:

Code:
unsigned long timeSince = currentTime - lastTime;
« Last Edit: June 21, 2012, 08:42:08 pm by Arrch » Logged

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hmm... that doesn't seem right.

Code:
#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);
 }
}
Logged

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

You need to update lastTime
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Add
Code:
lastTime = currentTime;
after you computer timesince.
Logged


0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So this should work?

Code:
#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?
Logged

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, that's not right either?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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.
Logged

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Certainly you can say that. 4065 dogs - 4045 dogs = 20 dogs
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25852
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: