Measuring time

Hi,

Trying to measure time between value changes. I already have measurement data coming and most of the time it is steady having only minor noise, now and then value jumps either up or down and then continues steady on that level till next jump. I need to find out time of how long it took from one steady level to another.

Pseudo code looks like:

while(true)
 has measurement changed enough from earlier measurement (-+10%)
  if yes
    record_start_time();
    while(!steady_again()) do_nothing();
    record_stop_time();
 else
    do_nothing();
done;

What would be best and most efficient way to do it in 328 chip? I could do it by comparing 1-2 earlier values with IF's but i doubt that it's efficient enough and could affect on value measurements.

--jp

I set up an array of unsigned long and put assign the millis() value to an element when an 'event' occurs. Its a simple matter to subtract one from the next to find out the interval.

Trying to measure time between value changes.

Pluggy’s reply answers that question well enough.

I could do it by comparing 1-2 earlier values with IF’s but i doubt that it’s efficient enough and could affect on value measurements.

If you are more concerned about how to determine the changes than about calculating the time difference, here is a proposal for that aspect of it.

The following pseudo-code mixes a float with ints and contains various other uglies, but you should be able to sort it out. The key is in the assignment of variation and its use in the comparison of measurement with earlier_measurement.

float variation = 0.0;

variation = earlier_measurement * 0.1;

// has measurement changed enough from earlier measurement (-+10%)
if (measurement > earlier_measurement + variation ||
    measurement < earlier_measurement - variation) {
    // yes, measurement is outside minor noise levels here

    record_start_time();

    // while(!steady_again())
    while (measurement > earlier_measurement + variation ||
           measurement < earlier_measurement - variation) {
           // measurement continues outside minor noise levels here
            
           earlier_measurement = measurement;
           variation = earlier_measurement * 0.1;
           // don't forget a time delay to let the analog pin settle
           measurement = analogRead(whatever_pin);

   }
   // measurement is back within minor noise levels here

   record_stop_time();

}

You give no real definition for “steady again”, but it probably means you want to extend the code inside the while loop to wait until either ‘n’ samples in a row are within limits, or a certain amount of time has passed with measurements within limits.