I tried to follow the answer here: https://arduino.stackexchange.com/a/18545/51302
Unfortunately I can only get the interrupt to go once (on serial monitor is the time of that first interrupt), then nothing. I know this has already been answered but I don't understand the mistake and would appreciate help.
What I'm trying to do: get a button to set an interrupt to trigger foo and thus write the steps on the monitor.
I've left remarks to try and explain what I try to do, so it would be easier to understand what I left out or used incorrectly. Thanks.
#define IRF520 A2
#define BUTTON 3
static unsigned long last_press = 0;
static bool button_state = HIGH; //pull up resistor
volatile bool interrupt_flag = 0; //when the interrupt fires, button press or noise
bool is_pressed = false; //after checking that the button was really pressed
bool was_pressed = false; //last state of the button
unsigned long button_pause_duration = 20; //pause required to accept press in ms
unsigned long interrupt_time=0;
void button_interrupt() {
interrupt_flag = true; //can be a false alarm
Serial.print("interrupt time: "); //this is the onlt step that works
Serial.print(millis());
Serial.print("\n");
}
void setup() {
Serial.begin(9600);
pinMode(IRF520, OUTPUT); //A2 controls IRF520
pinMode(BUTTON, INPUT_PULLUP); //pin D2 and D3 are sutible for interrupts
attachInterrupt(digitalPinToInterrupt(BUTTON), button_interrupt, FALLING);
}
void debounce_button() {
if (digitalRead(BUTTON) != button_state) {
button_state = digitalRead(BUTTON); //if button changed state remember the new state
interrupt_time=millis();
}
if (button_state == LOW) {
last_press = millis();
Serial.print("last_press was updated to: ");
Serial.print(last_press);
Serial.print("\n");
} else {
last_press = 0;
interrupt_flag = false;
}
if ( last_press && (millis() - last_press) > button_pause_duration) {
last_press = 0; //set to 0 for the next check
is_pressed = true; //button was really pressed
Serial.print("last_press was set to 0 and is_pressed to true - BUTTON WAS PRESSED at: ");
Serial.print(millis());
Serial.print("\n");
}
}
unsigned int foo(bool is_pressed) {
if (is_pressed) {
Serial.print("foo is active, MOSFET does its thing");
Serial.print("\n");
}
}
}
void loop() {
if(interrupt_flag){
Serial.print("the button is now being pressed: ");
Serial.print(millis());
Serial.print("\n");
}
debounce_button();
foo(is_pressed);
}
&&
in theif
statement looks wrong ... what are you trying to do?