0

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.

enter image description here

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.

enter image description here

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
    The serial plotter in the IDE is pretty basic. The x axis is just the number of y values, and 1 pixel in the x direction. You could try adding Serial.print(((millis()/10)%2)*50); to fake a 10 ms "grid". Serial.print(" "); – Gerben May 18 '18 at 16:34
  • If my current loop time is 40ms shouldn't I be dividing that by 40 to get a 10ms 'grid' ? – Rrz0 May 18 '18 at 17:18
  • Sorry, I kind of missed that part. I’d probably go with 1000ms. But note that a line could end up being at for example 2038ms instead of 2000ms due to the around 40ms it takes per loop. – Gerben May 18 '18 at 17:26
  • Alternative would be to also print the millis value an copy the data to excel. And use that to plot a graph. – Gerben May 18 '18 at 17:28
2

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 May 18 '18 at 18:54
  • 1
    Look at the BlinkWithoutDelay example of the Arduino IDE and replace the LED blink code with your measurement code. Then change the interval variable to the value you need. – chrisl May 18 '18 at 19:13
  • if time >= nextSampleTime should be replaced by if time - previousSampleTime >= MSEC_040. Otherwise the timing will fail when millis() rolls over. The “Blink without delay” tutorial does it the right way. – Edgar Bonet May 19 '18 at 15:54
  • Absolutely correct - fixed. Thanks, @EdgarBonet. [face-palm] – JRobert May 19 '18 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.