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