2

This is a question about programming. Mainly about the loop() function

As an example Consider code where you get data from an accelerometer, and if the Gs are high enough you do something.

Consider the following pseudocode:

void setup()
{
pinMode(1, INPUT);
bool flag = false;
}

void loop
{
int x= analogRead(1);
if (x>2)   //if the Gs we get from accelerometer
  flag = true;
if (button)  // if user hits the emergency button
  flag = true;

if (flag == true)
{
//GET SERIAL DATA (EG GPS COORDINATES)
//DO SOME HEAVY PROCESSING
}
}

Now loop runs repeatedly That means all the commands are executed in a continuous/serial style

Since that happens, what will happen if the accelerometer reading where (x>2), which is what we look for, happen in the REAL world, at a time where in the CPU, the cpu is at that time processing or checking following commands in the program flow, like

if (button)  // if user hits the emergency button
      flag = true;

    if (flag == true)
    {
    //GET SERIAL DATA (EG GPS COORDINATES)
    //DO SOME HEAVY PROCESSING
    }
    }

Is there any chance that the cpu will miss real world events, because at that time the real world event occured, the cpu was processing other code. Or rather, not checking for that at that time. For our example, when the event occured, it wasnt checking the analogread() at that time, but was processing following code.

Should i worry about that?

2

Yes, in theory you can always miss events if you use a polling approach like that. Whether or not that's a problem depends on how accurate/frequent your data collection needs to be.

You can often mitigate the issue to an acceptable level if you are able to poll fast enough. That's determined largely by what else your main loop is doing. The more work it has to do elsewhere, the longer it will take between calls to analogRead() (and the more likely it is to miss something). Optimising your code can help.

You may also find that you can compensate sufficiently by interpolating between samples. Linear interpolation is very easy, although more complex options may also be possible, especially if you're processing the data offline afterwards.

If polling isn't working for you, then interrupts are the obvious alternative. Since you're reading an analog signal, you'd probably need some external circuitry which does a voltage comparison. You can get dedicated comparator IC's from all sorts of vendors, or use an op-amp circuit if you prefer.

If the signal goes over a pre-determined threshold, the comparator would have to pulse or assert a digital line. That could trigger an external interrupt pin on your microcontroller, which will execute custom handler code. See attachInterrupt() for more information.

2

one should take into account the frequency response of the signal versus the sample rate. Per Arduino's web site AnalogRead "It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second." or 5K/S for 2 AnalogRead's.

Where I read the output response of the accelerometer used on the Esplora is 300-400Hz. Note often there are additional filters between the accelerometer and the A2D input.

So in short there should be enough time to sample both X & Y along with a Z, leaving a margin of 10x. To do other things.

Just be careful not to do too much, with routines that block.

As a matter of practice, it is best not to rely on a loop for what ever timing it loops at. But rather setup a timer, either by interrupt or by watching the millis(), to trigger the analog reads at a known interval.

#include "TimerOne.h"
volatile int x,y,z;
void setup()
{
  Timer1.initialize(3000);         // initialize timer1 to overflow at 333hz
  Timer1.attachInterrupt(callback);  // attaches callback() as a timer overflow interrupt
}
void callback()
{
  x = analogRead(A0);
  y = analogRead(A1);
  z = analogRead(A2);
}
void loop()
{
  noInterrupts(); // to ensure they are all in sync.
  // do something with x,y&z
  interrupts();
  // do something else, that is not blocking.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.