how to "digitalPinToInterrupt" ?

Hi everyone
Can please someone guide me. I am trying to do a countdown time with a 4 digit 7 segment display.

When i click on the button the timer starts, when it finishes the display turns blank. It should start to contdown when i click the button again, but it does not.
what i am doing wrong ?

thanks

#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object
const byte buttonPin = 2;
volatile int buttonPressed = 0;
volatile unsigned long lastButtonPress = 0;

static unsigned long timer = millis();
static int deciSeconds = 2000; 
int timerActive = 0;

void setup() {
  byte numDigits = 4;
  byte digitPins[] = {1, 7, 8, 9};
  byte segmentPins[] = {A0, A1, A2, A3, A4, A5, 6,4};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_ANODE; // See README.md for options
  bool updateWithDelays = false; // Default. Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
  sevseg.setBrightness(90);
  

  sevseg.blank();
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonPressInterrupt, CHANGE);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  
  if(buttonPressed) {
    if (deciSeconds == 2000) {
       timerActive = 1;
    }
    buttonPressed = 0;
    timer = millis();
  }
  
  if (timerActive && millis() >= timer) {
    deciSeconds--; 
    timer += 100; 
    if (deciSeconds == 10000) { // Reset to 0 after counting for 1000 seconds.
      deciSeconds=0;
    }
    if (deciSeconds == 1999) 
    { 
      deciSeconds=1599;
    }
    if (deciSeconds == 999) 
    { 
      deciSeconds=599;
    }

    sevseg.setNumber(deciSeconds, 1);
 
  }
  
  if (deciSeconds == 0) 
  { 
    timerActive = 0;
    deciSeconds == 2000;
    buttonPressed = 1;
    sevseg.blank();
  }
  sevseg.refreshDisplay(); // Must run repeatedly
  
}

void buttonPressInterrupt() {
  if (millis() - lastButtonPress >= 300) {
    buttonPressed = 1;
    lastButtonPress = millis();
  }
}

If the button is wired to ground (as it should be), with pin 2 external interrupts, it would make better sense to use LOW instead of CHANGE here:

  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonPressInterrupt, CHANGE);
  pinMode(buttonPin, INPUT_PULLUP);

No don't use LOW. That will keep firing the interrupt over and over and over as long as the pin is LOW. For a one shot use FALLING.

But really, you shouldn't need an interrupt to read a button. I don't see blocking code so you should just be able to read your button in loop.

No don't use LOW. That will keep firing the interrupt over and over and over as long as the pin is LOW. For a one shot use FALLING.

Sorry, that is what I meant.

jremington:
Sorry, that is what I meant.

I figured it was.