Hi there,
I am trying to set up a custom User Interface for a Project. It includes 5 pushbuttons and a rotary encoder which are connected to an arduino mega over I2C Bus. They should only be read when a user input has occured. Therefore an Interrupt routine has been set up.
Currently it only sets one Bit that is constantly polled to trigger the Wire read routine because Wire read cant be used in the ISR Routine.
This works, but is definitly not an ideal way, as it will lead top timing problems soon.
Is there a better way?
Here is some test code i have written. It works, but it does not in my final code. Serial Output will be replaced by other functions.
#define pcf8575adress 0x20
//is set to true when the ISR has been triggered
volatile bool buttonPressed = 0;
//For storing the current turning Direction
bool encplus = true;
bool encminus = true;
void pcf8575ISR() {
buttonPressed = 1;
}
void setup() {
Wire.begin();
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), pcf8575ISR, FALLING);
Wire.requestFrom(pcf8575adress, 1);
}
void loop() {
if (buttonPressed) {
Wire.requestFrom(pcf8575adress, 1);
byte incomingByte = Wire.read();
if (incomingByte == 0b00000011) {
encplus = true;
encminus = true;
}
if ((incomingByte == 0b00000001) && encminus) {
Serial.println("Enc+");
encplus = false;
}
if ((incomingByte == 0b00000010) && encplus ) {
Serial.println("Enc-");
encminus = false;
}
if (incomingByte == 0b00000111) Serial.println("Enc Click");
if (incomingByte == 0b00001011) Serial.println("Button 1");
if (incomingByte == 0b00010011) Serial.println("Button 2");
if (incomingByte == 0b00100011) Serial.println("Button 3");
if (incomingByte == 0b01000011) Serial.println("Button 4");
if (incomingByte == 0b10000011) Serial.println("Home");
buttonPressed = 0;
}
}