Reset counter after 10 seconds

Hee guys,

Does anyone know how I can reset the button presses to 0 every 10 seconds.

int LED = D7; // digital pin D6
int BUZZER = D6;
int BUTTON = D2; // digital pin D2
int buttonPressed = 0; // default value for times that button is pressed
bool toggleButton = true; // var to check if the button has been clicked

// setup() runs once, when the device is first turned on.
void setup() {
// Initialization of pinModes
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
// read value of input pin
if (digitalRead(BUTTON) == HIGH)
{
if (toggleButton)
{
buttonPressed ++;
// turn of the trigger of the button click to avoid trigger in next loop until releasing
toggleButton = false;
}
// check if button is pressed 3 times or more
if (buttonPressed > 1)
{
// write outputvalues, set to high
digitalWrite(LED, HIGH);
digitalWrite(BUZZER, HIGH);
delay(2000);
buttonPressed = 0;
}
}
else
{
// write outputvalues, set back to low
digitalWrite(LED, LOW);
digitalWrite(BUZZER, LOW);
// button has been released, so trigger can be catched again in next loop
toggleButton = true;
}
}

When you use delay(), the computer can't do anything else, like check buttons. In order to learn how to time events, it is very important to study and absorb the Blink Without Delay tutorial.

Use millis() based timing.

unsigned long lastReset;

if (millis() - lastReset > 10000) {
  lastReset += 10000;
  counter = 0;
}

Thanks a lot wvmarie, I got it working :slight_smile: :slight_smile: :slight_smile: