detatchInterrupt() not working, and delay stopping loop()

Hallo! I´m trying to make a simple program: When i press a button, spark starts blinking a LED (D2) each second, and when pressed again, it stops blinking.
Problem is, during the delay() Spark stops reading the button. I also tryed using detatchInterrupt() but it doesn´t even compiles :frowning:

const int buttonPin = D1;     // the number of the pushbutton pin
const int ledPin =  D2;      // the number of the LED pin

volatile bool buttonPressed = false;
volatile bool turnOnTheLight = false;

void setup() {

    pinMode(ledPin, OUTPUT);      
    pinMode(buttonPin, INPUT_PULLDOWN);    // sets pin as input
    Serial.begin(9600);         // Open serial over USB.

}

void loop(){
    if(digitalRead(buttonPin) == HIGH) buttonDown();
    if(digitalRead(buttonPin) == LOW) buttonUp();
    if (turnOnTheLight) light();
}

void light() {
    digitalWrite(ledPin, HIGH);   // Turn ON the LED pins
    delay(1000);
    digitalWrite(ledPin, LOW);   // Turn OFF the LED pins
    delay(1000);
}

void buttonDown() {
    if (!buttonPressed) {
        Serial.print("buttonDown!");
        buttonPressed = true;
    }
    digitalWrite(D7, HIGH);
}

void buttonUp() {
    if (buttonPressed) {
        Serial.print("buttonUp!");
        if (!turnOnTheLight) turnOnTheLight=true;
        else turnOnTheLight = false;
    }
    buttonPressed = false;
    digitalWrite(D7, LOW);
}

You probably want to approach this problem a slightly different way. Warning, untested and probably not complete code ahead.

long lastChange = 0;
long debounceDelay = 50;    // ms
int currState = 0;
int lastState = -1;

void setup()
{
    pinMode(btnPin, INPUT_PULLDOWN);    // Active HIGH
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);  // Turn off the LED
}

void loop()
{
    now = millis();         // Grab the current time

    currState = digitalRead(btnPin);        // Grab the current state of the button

    if((currState == lastState) && (now - lastChange > debounceDelay))  // Check to make sure that both the button is being pressed and HAS been being pressed for debounceDelay ms
    {
        if(currState == HIGH)   // button is active (bing pressed)
            blink();
    }
    if(currState != lastState)  // If the button state has changed (from pressed to not pressed or bouncing)
    {
        lastChange = now;
        lastState = currState;
    }
}

void blink()
{
    digitalWrite(ledPin, HIGH); // Turn ON the LED
    delay(1000);
    digitalWrite(ledPin, LOW);  // Turn off the LED
    delay(1000);
}

Read through that and led me know if you don’t understand anything or have more questions or problems