How do I find out the elapsed time from the serial Plotter graph? I am plotting sensor values from the Arduino Nano and would like to know the time scale on the x-axis in order to calculate settling time of oscillations. I have tried the suggestions here but they do not seem to work in my case.
For example, in the graph below I would like to find out the time it took for the sensors to record their first movement at around point 190.
I am doing the following to print two values:
//printData1();
Serial.print(roll_err);
Serial.print(" ");
Serial.println(pitch_err);
Serial.print(" ");
Baud Rate: 9600 Loop Time: 40ms
If my loop time is 40ms then I was thinking that the total time would be 180*40ms. Is this reasoning correct?
Is there a better way to read the time elapsed in the serial plotter?
EDIT: Trying to get a 10ms 'grid'
As per the suggestion in the answer below I have tried running something similar to the following but the loop does not run at a constant time.
I tried to add Serial.print(((millis()/10)%2)*50);
but as can be seen, the loop time is not constant.
enter image description here
1 Answer 1
It depends on how precise you want your time-base to be. The plotter's x-axis is in sample-count, not time, so you have to emit samples at a fixed rate for it to have time-significance.
Your
loop(){
yadda();
zadda();
}
may or may not run at a constant rate, depending on what takes place in each iteration of the loop, as well as on your definition of "constant" (I don't believe I just said that...)
Even if none of your code is conditional, binary to ASCII conversion, clock and Serial interrupts, and other low-level code we don't see, may run slightly differently from iteration to iteration; e.g. the number of interrupts during any given iteration may well vary.
If you need to hold a tighter timebase, stalling at the bottom of the loop until millis() reaches (the previous millis() + an interval constant) will at least get you to within a millisecond of a fixed loop-rate. You could, of course, do the same thing with micros(), as long as you know for a fact that the loop can never take longer to execute than 1/frequency you need it to run at, and even then, it should take a fair slop-factor (that's a technical term! :) less than that, "just in case".
Update:
Though I wrote "stalling" at the end of the loop, a better technique is to call every job (in your case, 1 of them - for now ? ) as frequently as possible and let each job decide whether it needs to execute. So in your case:
loop {
maybeSampleAndPlot();
// maybeDoSomeOtherThing(); // not implemented yet...
}
maybeSampleAndPlot(){
time = millis();
if time - previousSampleTime >= MSEC_040
save (time + MSEC_040) as nextSampleTime;
sample the data;
plot the data;
endif
}
-
Thanks for your answer. I will try to implement this. Is the interval constant used to allow for additions in computational time within the main loop? Updated my question. Would you be kind enough to provide some short pseudo-code on how I should go about implementing the
millis()
computation? Much appreciated.rrz0– rrz02018年05月18日 18:54:32 +00:00Commented May 18, 2018 at 18:54 -
1Look at the
BlinkWithoutDelay
example of the Arduino IDE and replace the LED blink code with your measurement code. Then change theinterval
variable to the value you need.chrisl– chrisl2018年05月18日 19:13:52 +00:00Commented May 18, 2018 at 19:13 -
if time >= nextSampleTime
should be replaced byif time - previousSampleTime >= MSEC_040
. Otherwise the timing will fail whenmillis()
rolls over. The "Blink without delay" tutorial does it the right way.Edgar Bonet– Edgar Bonet2018年05月19日 15:54:15 +00:00Commented May 19, 2018 at 15:54 -
Absolutely correct - fixed. Thanks, @EdgarBonet. [face-palm]JRobert– JRobert2018年05月19日 18:42:05 +00:00Commented May 19, 2018 at 18:42
Serial.print(((millis()/10)%2)*50);
to fake a 10 ms "grid". Serial.print(" ");