Calculation time for pulsed measurement

Hi,

I am developing an experiment where a photodiode measures the intensity from a LED. Now, since ‘real’ instruments use a pulsed source I want to try to build it that way as well.
Sadly enough I don’t have my papers with me right now and I don’t remember the ideal frequency, but it shouldn’t be too high (between 5 - 10 Hz, I’ll check it tomorrow, but thought it might be nice to ask the question already)

So, in it’s most basic form, all I want the Arduino to do is to put the LED on and off at 5 to 10 Hz, and measure a voltage on the photodiode everytime the LED is on. My question now is, can I just construct a loop with a delay of 100ms? (see - massively simplified - code) or do I have to take into account the time it takes to read and store (write) a value? I can imagine the time needed for that is negligible compared to the low frequency…

loop{
  light ON
  measure
  debug.print(measurement1)
  delay(100)
  light OFF
  measure
  debug.print(measurement2)
  delay(100)
}

And what if, in every 100ms (5Hz) or 50ms (10Hz) period, I measure the average of 10, or 20? (or put differently: how many measurements could I take, average out and print in e.g. a 50ms interval?)

My code now measures the average and standard deviation like this:

				// Here we compute the mean.
				mean_v=0;
                                mean_vref=0;
                for(i_math=0;i_math<49;i_math++)
				{
					mean_v=mean_v+V[i_math]; // V[50] is an array containing 50 measurments of the Voltage, Vref[50] is the same but for the reference voltage
                                        mean_vref=mean_vref+Vref[i_math];
				}
				mean_v=mean_v/counter;
                                mean_vref=mean_vref/counter;

				// Here we compute the std. 
				std_v=0;
                                std_vref=0;
                for(i_math=0;i_math<49;i_math++)
				{
					std_v=std_v+ pow(((double)V[i_math]-mean_v),2); // voltage value is so small that 50 measurement will not overflow a float.
                                        std_vref=std_vref+ pow(((double)Vref[i_math]-mean_vref),2);
				}
				std_v=std_v/counter;
				std_v=sqrt(std_v);
                                std_vref=std_vref/counter;
                                std_vref=sqrt(std_vref);

Is it possible to determine the time needed for this calculation?

Thanks a lot guys!

I am not actually an expert on this one, but the principle approach works. However as you play with floating point arithmetic I see an issue. The sqrt and division will cripple your timing. I would store the 100 values and make the calculation at the end.

The other thing you can try is to measure the delay manually and use the time for the calculation:

Take the time Measure Calculation Take the time and check if 50ms passed already if not wait the remaining time.

Anyhow I cannot judge if the arduino can do the calculationin 50ms.

If you aren't fussed about the exact frequency as long as it is about right, and don't need the Arduino to do anything else while this is going on, then the blocking approach you described would work. I haven't looked at the amount of processing you expect to do per cycle, but 100 ms is a very long time in Arduino terms. What might be an issue is that if you are sending messages to the serial port anywhere near the capacity of the serial port then you will soon fill the output buffers and at that point your sketch will be slowed down to match the speed of the serial port - which might result in it running far slower than you intend. Whether that's an issue would depend on what speed you run the serial port at, and how many bytes of output your sketch writes to the serial port for each cycle.

okay, thanks for the answers!

How do you 'measure the time' in Arduino? (probably a funtion, I'll look it up)

I'll try to program the simple way first and just calculate avg and std in excel then.

If you would come up with other tips: shoot!

Thank you