Sensor reading inside loop -best way

Dear All,

I have a program which loop runs every 20 ms. Inside the loop,

1, There is index variable which will increase the every time loops runs ( Index ++) 2. there is analog read function reads the Noise sensor every time loops runs 3. If index ==2, i.e 40 ms, I will call another function which will receive data from Xbee ( connected to FIO board) and control Air-condition via IR LED attached to FIO. 4. Some other function also executed when index reach certain number like index ==10

Now, the question is, 1. Is the loops runs every 20ms, and Analog read function executed for every 20ms and other functions inside also executed depending on index? Will it affect the performance of the program? or some reading may missed. Is it right design of to do? I believe that, Analog read happens in Micro seconds . Loop runs in Milliseconds. Right? Any issues here?

  1. Another idea, instead of Index, i can use time, to call function and read Sensor pin with 2 different loops from main. Like explained here: http://forum.arduino.cc/index.php?PHPSESSID=34u9s47j2l44vdbtmodfln43u7&topic=45649.15&token=uEaeck32QUfH8FsnVejp

Any thoughts /suggestion welcome? Thanks a lot

With regards, Chandra

yeh.. Use timebase to do procedures at different intervals e.g.

long timenow; nexta;nextb;
.....
void loop()
{
  timenow=millis();
  if (timenow>nexta) proca();
  if (timenow>nextb) procb();
}
void proca()
{
  nexta = timenow + interval;
  do stuff....
} 
void procb()
{
  nextb = timenow + interval2;
  do stuff....
}
  1. Is the loops runs every 20ms, and Analog read function executed for every 20ms and other functions inside also executed depending on index? Will it affect the performance of the program? or some reading may missed. Is it right design of to do?

How are you ensuring that the loops run every 20ms?

  1. Another idea, instead of Index, i can use time, to call function and read Sensor pin with 2 different loops from main. Like explained here:

That's a far better idea.

Thanks for reply PaulS/Knut_ny. It motivates me to contribute more to community.. :D Got the information. But wonder, why this (using timer instead of Index ++ for every loop runs) >>That's a far better idea.

I have an argument with my colleague who said, it not much difference between index++ and using timer (Micros() / Millis()). Any thought?

Further, I agree, it is not easy to say - how much time the the loop takes for each. Assuming there is no delay statement in side main loop. Wondering, whether, using INLINE function will help to execute the function faster? ( I am using FIO)

Cheers! :) Chandra

The example you've seen above runs your code at the intevals you decide. This will not be true if you have a "procedure pace" that makes it impossible to execute all code within the given period. (cpuload 100%+) The code example makes code easy to read. The 'lost time' to do a call measures in microseconds....

Counting makes sense only if you count things that happen at regular intervals. If you are, instead, doing something every 2th pass through loop, you have NO control over how often that happens. Add a few Serial.print() statements, and the time it takes to iterate through loop() changes, making the time taken for n iterations non-constant.

Doing something every 20 milliseconds might make that happen on every pass through loop, or every 10,000th pass, or after 10 passes then 100 passes then 11745 passes then 443 passes.