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
}
}
If this is a simple N.O. push button switch, don't use interrupts, just scan the switch(es) every 50ms and look for a change in state.
Of course you will need to make loop( ) non-blocking too.
I assume that you realise that this is the cause of the problem of detecting button presses. The question is, what is it doing there ?
You go to all of the trouble to nearly use millis() for timing then throw in a delay() then mess things up further by turning the LED off immediately afterwards instead of leaving it in its current state
I would also question the need to use an interrupt to read an input for such a mundane purpose as reading the state of the button
Wokwi simulates a bouncing button.
Your sketch does not catch the button too slow, it catches it too fast, inclusive the bouncing.
I have of course added the required pullup resistor.
It works find I for what it should do its a timer that you can set for 1 to 15 be it minutes or seconds you change the part
const long timerInterval = time * 1000;
//to
const long timerInterval = time*60*1000;
Im using 15 seconds cause i didn't want wait for 15 minutes to roll around
just for testing
I had serial.println to print button pressing and it works just sometimes it slow on catching
the first press after you change 1 to 2 it catches the rest one press at time.
thanks for your all time. And the one second on time is all i need then its off for the next count.
It depends on what you are using the interrupt function for:
if using to find the 'edges' on the button press you should initialize the lastButtonState variable to 1, or you'll lose the first front ( consider also that one button press may result in multiple fronts, on button press and release )
if it is used for debouncing... it does not debounce ( you say 'to catch button press better' I'd say you need to debounce )