Failure to make External Interrupts work.

Help !!I am trying to add interrupts to update a project and I have been working, or more accurately failing to work, with the simple button interrupt example provided by Nick Gammon in his useful guide.

I am working with our original project PCB which is a custom Zero based board. I know the board works correctly with the old software. The one thing that may be unusual is that the buttons are active high. The buttons are connected directly to and from the SAMD as per the original Zero schematic. The button is on a membrane touch-switch panel but see later. The button output and the signal output are viewed simultaneously on a dual-trace 'scope.

Here is the test code. It is a variation on Nick's original example because we do not have an onboard LED.

const byte LED = 16;
const byte BUTTON = 28;
volatile int state;

// Interrupt Service Routine (ISR)
void switchPressed (){
 state = digitalRead(BUTTON);
 //digitalWrite(LED, state);
}
void setup (){
  pinMode (LED, OUTPUT);  // so we can update the LED
  pinMode(BUTTON, INPUT);
  digitalWrite (BUTTON, LOW);    // button has pulldown resistor
  attachInterrupt (digitalPinToInterrupt (BUTTON), switchPressed, HIGH);  // attach interrupt handler
}  // end of setup
void loop (){
//  state = digitalRead(BUTTON);
  digitalWrite(LED, state);  
  // loop doing nothing 
}

You will note that I have the lines;

state = digitalRead(BUTTON);
digitalWrite(LED, state);

in both the ISR and in void loop(), this is for testing and the lines are commented out as required.

Pin 16 is SAMD pin 8/PB9. Pin 28 is SAMD pin 33/PA24.

The result of tests is that I always obtain a clean, square waveform at the switch when I press it. 0 volts idle and just under 3v3 when the button is pressed.
With both lines of code in the void loop() section I get a clean, corresponding, waveform on the output pin.
With both lines of code in the ISR section the switch output is still good but I get no output response.
With the digitalRead in the ISR and the digitalWrite in the void loop() I get no output response.
I have experimented with all of the attachInterrupt modes. HIGH is the last one I tried.

I have been spinning my wheels on this problem for two days so I would really appreciate some ideas or help. Thanks.

HIGH probably isn’t the right mode. That will continuously call your ISR over and over as long as the pin stays high. And it will never register a LOW. You probably want CHANGE as the mode so it fires whenever the pin changes.

But that still begs the question, why do you think you need an interrupt for something as slow as a button press?

You know that the ISR is only triggered when the button is pressed (the pin goes HIGH), so try to skip the digitalRead:

const byte LED = 16;
const byte BUTTON = 28;
volatile int state = LOW; //MODIFIED!

// Interrupt Service Routine (ISR)
void switchPressed (){
 state = HIGH; //MODIFIED!
 //digitalWrite(LED, state);
}

void setup (){
  pinMode (LED, OUTPUT);  // so we can update the LED
  pinMode(BUTTON, INPUT);
  digitalWrite (BUTTON, LOW);    // button has pulldown resistor
  attachInterrupt (digitalPinToInterrupt (BUTTON), switchPressed, HIGH);  // attach interrupt handler
}  // end of setup
void loop (){
//  state = digitalRead(BUTTON);
  digitalWrite(LED, state);
  if (state == HIGH) state = digitalRead(BUTTON); //ADDED!
  // loop doing nothing
}

You should also consider to use an 10K external pullup resistor for the button.

I would suspect the IDE pin mapping, the actual capabilities of the samd, and what you have with your custom board.

const byte BUTTON = 28;
attachInterrupt (digitalPinToInterrupt (BUTTON), switchPressed, HIGH);

Pin 28 is SAMD pin 33/PA24.

I see this in the Arduino15 file with the samd core

Arduino15\packages\arduino\hardware\samd\1.8.3\variants\arduino_zero\varient.cpp

PORTA, 24, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // USB/DM

Delta_G
The HIGH is there because I was trying all modes to see if I could get a response. I did try CHANGE with no improvement.
My idea behind using an interrupt is because the final project uses 6 pins. The project does not involve emergency stops or the possibility of injury but I did want the buttons to have priorities, in this case the “Stop” button having priority one so interrupts seemed like a good approach. We have had the project working previously using this exact button in a loop configuration.

Danois
There is an 18k pulldown on the pin that I should have mentioned. The input signal from the button is near perfect on the scope. Clean rise with no discernible bounce. I will try your suggestion.

Cattledog,
An interesting find. I have been under the impression that the Zero can use any pin for external interrupts except pin 4. I should have thought about this more closely. Since my pin 28 is actually CPU pin 33/PA24 it is not actually a Zero pin, only a SAMD pin used on our custom board. I will test with another pin.

Your issue may (as mentioned by cattledog) be as simple as using the wrong pin. I've never used a Zero, but pin 28 is EXTINT 5, in the code this should be referred with PA05:

const byte BUTTON = PA05;

But since your board is custom YMMV!