MKR Interrupts

Hi,

I have created a really simple code to test interrupts in my MKR1000. The idea is switching the built-in LED when some other PIN is being switched. Yes, the 'Hello World' of interrupts. But I cannot make interrupts being fired using a digitalWrite. Exceptions are fired if I directly change the PIN externally using a cable between PIN 7 and GND.

What am I missing? Should I have considered something additional for an MKR?

Thanks in advance.

// Using PIN 7 for edge detection and interrupts
#define FLAG 7

// state is only changed through the interrupt and controls the LED
volatile boolean state = LOW;
// state2 is the value which makes the PIN switch in order to fire an interrupt
boolean state2 = LOW;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(FLAG, INPUT); // I have tried INPUT, OUTPUT, INPUT_PULLUP

  // Registering the interrupt. I have tried with CHANGE, RAISE and HIGH
  attachInterrupt(digitalPinToInterrupt(FLAG), blink, CHANGE);
}

void loop() {
  // The LED can only be changed using the interrupt code
  digitalWrite(LED_BUILTIN, state);

  //Try to fire an interrupt by changing the PIN value
  state2=!state2;
  digitalWrite(FLAG, state2);

  Serial.print(millis());
  Serial.print(state? " ON ":" OFF ");
  Serial.println(state2? " ON ":" OFF ");
  delay(1000);
  
}

void blink() {
  state = !state;
}

it is 'external' interrupt. the pin is input if configured for external interrupt. digitalWrite should not work

Thanks for your reply, juraj.

Anything else I might be missing or doing wrong?

Thanks.

gnieto:
Thanks for your reply, juraj.

Anything else I might be missing or doing wrong?

Thanks.

what else? you write the interrupt is fired when you activate the pin

Hi juraj,

no. The interrupt should be fired when this lines of code are executed:

  state2=!state2;
  digitalWrite(FLAG, state2);

But it is not fired.

Thanks!

gnieto:
Hi juraj,

no. The interrupt should be fired when this lines of code are executed:

  state2=!state2;

digitalWrite(FLAG, state2);




But it is not fired. 

Thanks!

no it should not. it is an EXTERNAL interrupt

Thanks again, juraj.

So this something I am doing wrong.

Is there a way that I can switch a value so that an interrupt is raised from my MKR. In other words, how could I raise an interrupt from the code?

gnieto:
Thanks again, juraj.

So this something I am doing wrong.

Is there a way that I can switch a value so that an interrupt is raised from my MKR. In other words, how could I raise an interrupt from the code?

call it as function

I completely agree with juraj--as a practical matter, if you need to invoke an external interrupt handler from your code, then you just call it, plain & simple.

But I got curious about the technical issue. When you digitalWrite to an INPUT pin, I think it's supposed to turn on or off the internal pullup. I wrote something like the OP's code for the Arduino UNO and it worked, except that the interrupt pin had to have some pathway to ground (even one as little as my scope probe presents) or else the pin wouldn't actually switch states. But when I tried it on a MKR zero (assuming it's similar to the MKR1000 in this regard), it didn't work at all because for some reason the input pullup only raises the level to something like a diode drop. Not sure why this is happening....

Hmm, I just tested on a MKR1000 (also a SAMD) and the input pullups work fine (and by extension, the OP's indirect way of invoking an ISR works as well as long as there is some pathway to ground at the interrupt pin). I see one unanswered post about INPUT_PULLUP not working from 2017....

Never mind, things work on the MKRZero as well. I was using a way too small load resistor on the external interrupt pin, and looking at the SAMD datasheet confirms it. The internal pullup is on the order of 40k ohms, so you'd probably want a minimum of 470k ohms to ground. So gnieto, I think if you went back to your original setup and simply added a 1M resistor to ground on your external interrupt pin 7 you'd get the behavior you were expecting. But again, this is just a technicality; I can't think of a reason not to just call your ISR directly.