X7123M3-256
New member
I'm working on a project involving an accelerometer which I would like to read values from 100 times per second. Up until now I've been using an IntervalTimer to do that, but now it's no longer working, and from what I've found elsewhere (e.ghttps://forum.pjrc.com/threads/28720-Sending-I2C-message-from-ISR) it seems like the problem is that the Wire library isn't meant to be used within an interrupt service routine (I'm not sure why it was working before). Instead, it seems that the recommended solution is to poll the sensor from within my main loop with a construction like
The problem I have is that I don't think I can guarantee that my main loop will always take less than 10ms to run - I've tried timing how long the loop currently takes, and it seems that it averages about 5us (which is plenty quick enough), but writing output to the serial occasionally takes >10ms. I thought I could use two nested loops like this:
But this doesn't really solve the problem as if any one operation takes longer than 10ms - even if that operation only needs to happen occasionally - I'm still going to miss data points as a result. What I want to do is have the routine that reads the sensor pre-empt whatever else is currently running, and save the values to be processed later so that I don't get gaps in the data if some long-running operation is blocking the main loop. It wouldn't matter too much if the samples aren't exactly 10ms apart, but I do want the sensor to be read exactly 100 times per second.
I am wondering, is there an alternative way to accomplish this if I can't use an interrupt, or is there some workaround that might make it work? (it did seem to be working before and I'm not really sure what changed) Or is there a different way to approach this problem?
Code:
while(running)
{
if(millis()>last_sensor_read+10)
{
read_sensor();
last_sensor_read=millis();
}
}
The problem I have is that I don't think I can guarantee that my main loop will always take less than 10ms to run - I've tried timing how long the loop currently takes, and it seems that it averages about 5us (which is plenty quick enough), but writing output to the serial occasionally takes >10ms. I thought I could use two nested loops like this:
Code:
while(running)
{
for(int i=0;i<100;i++)
{
//Stuff that needs to run often
}
//Stuff that needs to run less often
}
But this doesn't really solve the problem as if any one operation takes longer than 10ms - even if that operation only needs to happen occasionally - I'm still going to miss data points as a result. What I want to do is have the routine that reads the sensor pre-empt whatever else is currently running, and save the values to be processed later so that I don't get gaps in the data if some long-running operation is blocking the main loop. It wouldn't matter too much if the samples aren't exactly 10ms apart, but I do want the sensor to be read exactly 100 times per second.
I am wondering, is there an alternative way to accomplish this if I can't use an interrupt, or is there some workaround that might make it work? (it did seem to be working before and I'm not really sure what changed) Or is there a different way to approach this problem?
Last edited: