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

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 (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