Use a fixed interrupt timing of 10ms = 100Hz and read all sensors, count the interrupts and and signal that an interrupt happened.
In the main-loop wait for the interrupt and act on the counter which result has to be considered in the current cycle
A - 10ms - ADC 0, 1, 2 each cycle
B - 50ms - I2C 0, 1, 2, 3 cycle %5 == 0
C - 100ms - ADC 3,4,5,6,7 cycle %10 == 0
D - 1000ms - ADC 8,9,10,11 and I2C 4,5,6,7 cycle %100 == 0 and reset cycle
When all sensor reading and calculations can be done within 10ms the time distances will be equal.
Thanks Bob. Reading the senors takes me different amounts of time:
A = 4ms
B = 115ms
C = 8ms
D = 140ms
I could probably optimize this a bit, maybe reduce the B and D to half but they are still very large. There isn't anything I can do, that's the amount I need for the sensor read (specific to the sensing element)
The idea is that I need to sample some sensors faster than others.
What would you say is the best approach in this case?
Problem is in I2C processing speed. How possibly operation B be repeated each 50 ms, while it takes 115ms to execute it? General approach should be: (i) since I2C in non-deterministic, no point to fight for timing accuracy; use buffers to collect incoming data, add some timestamp and process when system is idle. (ii) collect ADC data from all sensors on 10 ms interval into RAM using DMA, process when idle ( ignoring unnecessary data). Other thought: it takes ~100us to acquire 16 bit DelSigADC, why it takes about 1ms in your case? How many bits do you need?
Hi. Thanks for the reply. It is my typo, B is only 11.5 not 115 :D
Since you run interrupt driven you may trigger the I2C-readout 10ms before you need the values will reduce the wait-time for B to 1.5ms.
When D was a typo,too then wait-time will be 4ms.
I wish it was... D is limited from the I2C sensor side to that time length.
Regarding ADC sampling time - I use a moving average on the values and my code will need further improvements so it seems to add delay up.
The way they were measured was using a toggle pin in the main loop before and after each batch of measurements.
So the best way would be to collect everything as fast as I can and just process it at desired times.