The time between data recieved

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

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

}

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.

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.

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.

unsigned long lastTime;

That should be put in the global scope.

To get the current time:

unsigned long currentTime = millis();

To get the difference:

unsigned long timeSince = currentTime - lastTime;

hmm... that doesn't seem right.

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

You need to update lastTime

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

Add lastTime = currentTime; after you computer timesince.

So this should work?

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

Ok, that's not right either?

    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.

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?

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

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.

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.

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.