// PROJECT :Int0Test // PURPOSE :Review of External Interrupts and reminder to ALWAYS debounce PBs // RESULT :Launch Serial Monitor and monitor occasional bounces // AUTHOR :C. D'Arcy // DATE :2022 12 03 // uC :328P // STATUS :Working // REFERENCE:Gammon on Interrupts: http://gammon.com.au/interrupts // : http://darcy.rsgc.on.ca/ACES/PCBs/images/DebounceSchematic.png // NOTE :Press a button (with no debouncing) a few times to see if you can detect bouncing... #define INT0 2 // monitor button presses on D2 (INT0) uint8_t state = 0; // current state of button volatile boolean triggered = false; //volatile: required for ISR access to any global variable void setup() { Serial.begin(9600); while (!Serial); attachInterrupt(digitalPinToInterrupt(2), report, CHANGE); //also RISING or FALLING } //External Interrupt Service Routine (ISR) // try to do as little as possible. Get in. Get out. void report() { triggered = true; } void loop() { if (triggered) { //was an interrupt detected? noInterrupts(); //if so, TEMPORARILY suspend interrupts while we complete service... state = digitalRead(INT0); //read the current button state Serial.println(state); //publish triggered = false; //prepare for the next interrupt interrupts(); //...RESTORE interrupt capability } }