how to get exact timestamp of a sensor reading and compare it with other sensor values in arduino?
Ex: Get the time of low reading of LDR and compare it
1 Answer 1
As Majenko said you can use the millis() keyword.
Unless millis is not accurate enough. In that case you can use micros(), but this overflows every 70s if I'm correct, but you can offset it against millis().
If you need to compare two values, what you can do is:
- get millis()
- get micros()
- read sensor data (optimize if needed)
- get micros()
Average the difference of the micros, and add it to millis, and you have the average time for the sensor data readings.
Of course if it doesn't matter that all sensor data is read at approximately the same time, you can use millis() for each sensor data reading and work with those values.
-
Why should you take the aveage timestamp of the measure and not the beginning or the end? I think that the beginning is more consistent and more appropriate (usually you acquire and then elaborate, so the actual measurement is closer to the beginning rather than the end)frarugi87– frarugi872018年03月16日 11:23:10 +00:00Commented Mar 16, 2018 at 11:23
-
@frarugi87 I also would use the start, but if you need the one time stamp for multiple measurements, the 'average' might be more applicable.Michel Keijzers– Michel Keijzers2018年03月16日 11:30:30 +00:00Commented Mar 16, 2018 at 11:30
-
since arduino code is in a loop how can i exactly know the timenuwan karunarathna– nuwan karunarathna2018年03月16日 11:47:40 +00:00Commented Mar 16, 2018 at 11:47
-
You first get the time with millis(). Than immediately afterwards you do the measurement. Assumign the measurement takes very little time, you can use the millis() value as 'timestamp'.Michel Keijzers– Michel Keijzers2018年03月16日 11:49:38 +00:00Commented Mar 16, 2018 at 11:49
millis()
as others have suggested.