I have some existing code that works and now I want to be able to make something happen when I press a button. I’ve attached a 12k pullup resistor between 3V3 and D0, then a momentary normally open pushbutton between D0 and GND.
I get no response when I click the button (it should be blinking my onboard led).
Relevant portions of my code are below. Thanks!
int button = D0;
void calibrate(void); // saw this in the documentation, not sure it's required
void setup()
{
attachInterrupt(button, calibrate, CHANGE);
...
}
void calibrate() {
blinkLed(4); // calls a function that blinks on-board LED
}
I think you should try to debounce your button, since that might mess things up. Then, you should try getting the function call out of your interrupt. Interrupts should be kept as short as possible. Rather set a flag which gets checked in the main loop.
@BulldogLowell beat me to it, but I’m posting this anyway, since I worked SO hard on it
as for getting the function call out of the interrupt: my loop has a multi-minute delay() call in it (this is a system that check a sensor every so often). What is the design pattern for best handling an interrupt for such code? such a thing is hard to google for.
Well, get that out of there, delays are kinda aweful ;(
Rather, use a millis() implementation, since that wont block anything, or better yet, use software timers!
The last (I think!) question: currently because I have a delay() in my loop that lasts for a few minutes, how would I arrange the code so that the interrupt gets detected more or less immediately?