Reading from the ADC and using an interrupt on the same pin A0

I’m trying to do the following on a Photon:

  1. Attach an interrupt to pin A0 with a rising input trigger.
  2. When the A0 ISR is called, I set a volatile bool flag to true.
  3. In loop(), I am checking for the bool flag from #2 to set to true.
  4. When the flag from #2 is true, then I start reading from A0 with analogRead.
  5. At some point I set the flag from #2 back to false.

This all works correctly, the problem then is it will not function a second time. I never see the A0 ISR get called ever again. My guess is there’s some interaction with A0 having and attached ISR and also being read via analogRead(), which from my understanding also uses interrupts. I’ve even tried detaching the ISR when the flag from #2 is true and then reattaching the ISR when the flag from #2 is set back to false. That doesn’t do anything.

Code basically looks like this (I’ve left out a lot of the other stuff):

volatile bool do_read_adc = false;

void ButtonChangeISR(void)
{
    Serial.println("ButtonChangeISR");
    do_read_adc = true;
}

void setup()
{
    pinMode(A0, INPUT);
    attachInterrupt(A0, ButtonChangeISR, RISING);
}

loop
{
    if (do_read_adc)
        val = analogRead(A0);

    // Some code that determines when to set do_read_adc to false
}
void ButtonChangeISR(void)
{
    Serial.println("ButtonChangeISR");
    do_read_adc = true;
}

have you tested removing the Serial.println() from your ISR? Serial uses interrupts.

you could try instead lighting the on-board LED on D7 inside the ISR to debug.

Yes I have tried removing it and it doesn’t seem to make any difference. It’s my understanding that reading from the ADC also uses interrupts for sampling, so it’s my guess that this is the interaction that isn’t cooperating with the RISING trigger interrupt.

I can't find the thread but there may be a need to reset pinMode() to deal with a possible bug...

you could try adding it just before or after you reset the ISR flag to false.

If you’re able to find the thread you’re thinking about that’d be greatly appreciated.