I want to read the barometric pressure with a BMP085 pressure sensor, at (say) 5 second intervals.
I've checked the sensor with the simple sketch provided with the Adafruit_BMP085 library, and it works fine.
Ideally I want to poll the sensor with interrupts, so with this in mind I tried the example sketch from the TimerOne library, which just flashes an led, and that worked fine.
I then tried to patch the code for reading the BMP085 sensor into the ISR of the TimerOne example, but it doesn't work - it seems to get stuck in the ISR. I left the code for cycling the led for troubleshooting.
There is a pin in the BMP085 module labeled EOC. This stands for End Of Conversion, and it essentially goes LOW when you request the BMP085 to sample something (pressure or temperature), and turns HIGH when the sensor module has finished sampling the requested value. We can take advantage of this by using pin change interrupts on a given digital pin.
So, simply connect the EOC pin from the BMP085 to, say, digital pin 4 of your arduino. Configure the appropriate PCINT registers to allow pin change interrupts on that pin, and handle the EOC event response from inside the ISR routine.
The following is to configure pin change interrupts on digital pin 4 (PD4)
PCICR |= (1 << PCIE2) /* Allow PCINT interrupts on port D */
PCMSK2 |= (1 << PCINT20) /* Allow PCINT on pin PD4 */
The code inside the ISR should ideally execute very fast. It is not advised for you to use i2c within the ISR (since your program will probably freeze). The solution I found was to set a flag within the ISR, and check this in your main loop (outside ISR) through a simple state machine.
void loop()
{
/* The acquire() function acts as a state machine */
if( bmp085.acquire())
{
Serial.println(bmp085.calculateDistance_cm());
}
}
This would be the interrupt routine, where you simply set the End of Coversion flag. You want to avoid putting a lot of code within this function (Let the state-machine do the rest).