// Forum: https://forum.arduino.cc/t/maybe-i-could-get-you-all-to-look-at-my-code/1288462
// This Wokwi project: https://wokwi.com/projects/405317061144406017

const int ledPin = 13;             // Pin connected to the LED
const int buttonPin = 2;           // Replace with your button pin
unsigned long previousMillis = 0;  // Variable to store the last time LED was updated

//const long interval = 1000;      // Interval at which to blink (milliseconds)
//const long timerInterval = 15 * 1000; // 15 minutes in milliseconds
int buttonState = 0;
int lastButtonState = 0;
int buttonPushCounter = 0;
int time = 0;
bool ledState = LOW;  // Initialize LED state

void setup() {
  pinMode(buttonPin, INPUT);  // Assuming a pull-up resistor
  attachInterrupt(digitalPinToInterrupt(2), ISR_Routine, CHANGE);
  pinMode(ledPin, OUTPUT);  // Initialize the LED pin as an output
  Serial.begin(9600);
}

void ISR_Routine() {
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
  }
  if (buttonState == LOW && lastButtonState == HIGH) {
    buttonPushCounter++;
    time++;
  }
  lastButtonState = buttonState;
  if (buttonPushCounter == 16) {
    buttonPushCounter = 0;
  }
  if (time == 16) {
    time = 0;
  }
  Serial.println(buttonPushCounter);  // Print the counter value
}

void loop() {
  const long timerInterval = time * 1000;  // 15 minutes in milliseconds it sets seconds for now
  unsigned long currentMillis = millis();  // Get the current time
  if (currentMillis - previousMillis >= timerInterval) {
    // It's time to toggle the LED
    previousMillis = currentMillis;
    // Serial.println(ledState);
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // Serial.println(ledState);
    digitalWrite(ledPin, ledState);  // Toggle the LED
    delay(1000);                     // Wait for 1 second
    digitalWrite(ledPin, LOW);

    //Serial.println(Time); // Print the counter value
  }
}