Uno, INT0, INT1 and I2C interrupts issues related question...

Hi there.

I made a sketch using INT0 and INT1 both connected to IR Breakbeams and an lcd connected to i2c (adafruit).

I noticed something i can't really explain:
INT0 seems always working as expected, no issue there.
INT1 seems working most of the time but goes "Missing in actions" during few seconds from time to time then resume its operations like nothing happened.

I swapped the physicals parts and still get the same results.

I somewhat suspects something related to i2c but can't really find anything relevant and furthermore, is there any 'solution' to that behaviour ?

Thanks
Laurent

Inside Setup()

attachInterrupt(0, GateLeft, CHANGE);
attachInterrupt(1, GateRight, CHANGE);

GateLeft:

///
void GateLeft() {
ActiveGate = digitalRead(GLEFT);
if (ActiveGate==LOW){
if (Bounced==false) {
if (LtrNdx==0)
LtrNdx=10;
else LtrNdx--;
}
Bounced=!Bounced;
}
}

GateRight

void GateRight() {
ActiveGate = digitalRead(GRIGHT);
if (ActiveGate==LOW){
if (Bounced==false) {
if (LtrNdx==10)
LtrNdx=0;
else LtrNdx++;
}
Bounced=!Bounced;
}
}

If you suspect it has something to do with something else, why didn't you post your whole code like you're supposed to?

Thanks for you reply and hints...

#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
// initialize the library with the numbers of the interface pins
Adafruit_LiquidCrystal lcd(0);

#define GLEFT 2
#define GRIGHT 3

bool Bounced=false;
volatile byte LtrNdx=0;

void setup()
{

lcd.begin(16, 2);
pinMode(GLEFT, INPUT_PULLUP);
digitalWrite(GLEFT, HIGH);
pinMode(GRIGHT, INPUT_PULLUP);
digitalWrite(GRIGHT, HIGH);
attachInterrupt(0, GateLeft, CHANGE);
attachInterrupt(1, GateRight, CHANGE);
}

loop(){
lcd.setCursor(1, 0);
lcd.print(LtrNdx);
delay(500);
}

void GateRight() {
ActiveGate = digitalRead(GRIGHT);
if (ActiveGate==LOW){
if (Bounced==false) {
if (LtrNdx==10)
LtrNdx=0;
else LtrNdx++;
}
Bounced=!Bounced;
}
}

void GateLeft() {
ActiveGate = digitalRead(GLEFT);
if (ActiveGate==LOW){
if (Bounced==false) {
if (LtrNdx==0)
LtrNdx=10;
else LtrNdx--;
}
Bounced=!Bounced;
}
}

By the way, quotes are not the same as code tags...

Why do you want to count every second break of the beam only?

Why do you use the same Bounced variable in both ISRs?

And why read the pin in the ISR?

    ActiveGate = digitalRead(GRIGHT);
    if (ActiveGate==LOW){

If you're only interested in the beam becoming LOW you can just set the interrupt to FALLING

attachInterrupt(0, GateLeft, FALLING);

Btw, the const parameter has the preference to use over a #define for a pin define. Same goes for starting a variableName without a capital if it's non-const.

Why are you providing code that does not compile? The else's are wrong and ActiveGate is not declared!

Mark

I solved my issue by changing the attach interrupt for INT1 to FALLING and removed the pin reading.
Now INT1 behaves like it should...

Thanks for the lecture about coding and just for the reflection, #define doesn't use memory and that's a plus...

sachaMagne:
#define doesn't use memory and that's a plus...

A const does not use memory.

More technically correct: A const does not use SRAM memory.
Because it does end up in flash.

Probably embedded in the instruction, where the #define lands too.