attachInterrupt, blink led for 1 seconds than shutdown

Hi to all!
I'm studying the interrupts.

My sketch is looking for a button pressed. If yes, blink a LED for 1 seconds than shutdown.

The only part that doesn't function is that the led never shutdown.

How I can shutdown the LED after 1 second?

You will find also a code part to bypass the debounce.

You will find also some Serial.println. I have in serial also the words ""Lo stato è HIGH!" , so my code read correctly that the new state of PIN is HIGH, but it doesn't write to LOW.

Thank you for your support!

const int LED_PIN = 8;
volatile int state = LOW;

long previous_millis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

long debouncing_time = 1500; //Debouncing Time in Milliseconds
volatile unsigned long last_micros;


// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(115200);
  welcome();
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_PIN, OUTPUT);
  attachInterrupt(0, debounceInterrupt, RISING);
  
}

void welcome() {
  Serial.println("Benvenuto!\n");
}

void debounceInterrupt() {
  if((long)(micros() - last_micros) >= debouncing_time * 1000) {
    blinkLed();
    last_micros = micros();
  }
}

// the loop function runs over and over again forever
void loop() {

  digitalWrite(LED_PIN, state);
 
  unsigned long current_millis = millis();
 
    if( (current_millis - previous_millis) > interval) {

      previous_millis = current_millis;

      Serial.println("Il tempo è passato");

      Serial.println(state);

      int current_state = state;

      if (current_state==HIGH) {
        Serial.println("Lo stato è HIGH!");
        digitalWrite(LED_PIN,LOW);
      }
      
    }

}

void blinkLed() {
  Serial.println("Button pressed!");
  state = !state;
}

EDIT:

Found the "bug" or the programming error. I did not re-chage the volatile state.

This is my code, just to tell you.

[...]

if( current_millis - previous_millis > interval) {

    previous_millis = current_millis;

    if (state==HIGH) {
      state = !state;
      digitalWrite(LED_PIN,state);
    }
      
  }

[...]

In general, don’t call a function from within your ISR like this - it is the same as including the code directly in the ISR

void debounceInterrupt() {
  if((long)(micros() - last_micros) >= debouncing_time * 1000) {
    blinkLed();
    last_micros = micros();
  }
}

In your particular case your function calls Serial.print() which requires interrupts and in an ISR interrupts are disabled. Even if that was not a problem Serial.print() is a slow function that would delay the completion of the ISR. An ISR should be designed to complete as quickly as possible.

In this case your ISR could be

void debounceInterrupt() {
  if((long)(micros() - last_micros) >= debouncing_time * 1000) {
    readyToBlink = true;
    last_micros = micros();
  }
}

and then the code in your blinkLed() function could be

void blinkLed() {
  if (readyToBlink == true) {
     readyToBlink = false;
     Serial.println("Button pressed!");
     state = !state;
  }
}

and just call blinkLed() from loop()

I am assuming this is just a learning exercise and that you know it is not necessary to use an interrupt to detect a human pressing a button.

…R