I am trying to read data from a Wire (connected to a gyroscope) from inside a function called from an interrupt service routine. I am using the FlexiTimer2 library from Arduino Playground - FlexiTimer2.
The problem I am having is that whenever this function (my function I'm attaching to the interrupt) is called, the code locks up. I beleive this is due to my Wire.something() function calls, since removing them and just using some Serial.print()s allows it to work properly.
My code:
void timerTask() {
Serial.print("Gyro X: ");
int buff[10];
Wire.beginTransmission(ADDRESS_GYRO); //start transmission to ACC
Wire.write(0x1B); //sends address to read from
Wire.endTransmission(); //end transmission
Wire.beginTransmission(ADDRESS_GYRO); //start transmission to ACC
Wire.requestFrom(ADDRESS_GYRO, G_TO_READ); // request 6 bytes from ACC
int i = 0;
while(Wire.available()) //ACC may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); //end transmission
Serial.print(buff[0]);
Serial.print("\r\n");
}
And I assign my fuction to the interrupt handler here:
Wire.beginTransmission(ADDRESS_GYRO); //start transmission to ACC
Wire.requestFrom(ADDRESS_GYRO, G_TO_READ); // request 6 bytes from ACC
int i = 0;
while(Wire.available()) //ACC may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); //end transmission
You don't need Wire.beginTransmission and Wire.endTransmission around Wire.requestFrom.
Wire.requestFrom returns the number of bytes read.
You shouldn't be doing Serial.print inside an ISR, and the Wire library uses interrupts which is why it doesn't work.
Better is to set a flag, check that flag in the loop function, and do your stuff there.
Rather than guess about how the Wire library works remember you have the source code for it - just have a look
at it's description and comments and code!
Actually for the Wire library you'd need to look at <install>/libraries/Wire/utility/twi.c to discover the use of interrupts,
so perhaps not the most straightforward example.
Built in libraries are either at <install>/libraries/... or a few (including HardwareSerial) are at <install>/hardware/arduino/cores/arduino/...
Even if the code means nothing the big comments at the top of a library file can be very informative.
Yes, I didn't think to look at the included source for the libs...
I have an alternative solution that involves not using the interrupt to handle my Wire calls (keeping track of time in my main loop with if/else and doing what I need to there)..