Countdown timer with buzzer output and button input

it wont turn the buzzer off or respond when the buzzer is on

int seconds = 20;
int buzzer = 12;
int btncount;
int btn = 2 ;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
}

void loop() {


if (digitalRead( btn ) == LOW) //check if button is pressed
{
  ++ btncount; // if pressed add one to variable btncount
  delay(200);
}
if (btncount == 1){
 
  Serial.println(seconds);
  seconds -= 1;
  delay(1000);
while (seconds <=-1 && seconds >= -10){
digitalWrite(buzzer, HIGH);
delay(2);
digitalWrite(buzzer, LOW);
delay(2);
}
}
if (digitalRead( btn ) == LOW){
++ btncount;
}
if (btncount > 1){
   btncount=0;
seconds == 20;
digitalWrite(buzzer, LOW);
Serial.println("reset");
}


}

It would help if you describe what the code is supposed to do.

Observations:

  1. You didn't configure btn as an input in setup(). I would suggest:

pinMode(btn, INPUT_PULLUP);

  1. You never modify seconds in this while loop. If you ever enter the loop you will stay in it forever!
    while (seconds <= -1 && seconds >= -10) {
      digitalWrite(buzzer, HIGH);
      delay(2);
      digitalWrite(buzzer, LOW);
      delay(2);
    }
  1. How is your button wired?

What is the intent of this line of code? '=' is assignment, '==' is comparison.

thanks for the quick reply the code starts at 20 seconds counts down fine, when reaches -1 the buzzer goes off. im trying to then reset it when the buzzers on

And:

void loop() {
  if (digitalRead( btn ) == LOW) //check if button is pressed
  {
    ++ btncount; // if pressed add one to variable btncount
    delay(200);
  }
  if (btncount == 1) {
    Serial.println(seconds);
    seconds -= 1;
    delay(1000);
    while (seconds <= -1 && seconds >= -10) {
      digitalWrite(buzzer, HIGH);
      delay(2);
      digitalWrite(buzzer, LOW);
      delay(2);
      if (digitalRead( btn ) == LOW) {
        ++ btncount;
      }
      if (btncount > 1) {
        btncount = 0;
        seconds = 20;       
        Serial.println("reset");
        break;
      }
    }
  }
}
1 Like

the button is wired to digital pin 2 then + and ground

Let me see if I have this correct:

When the Arduino starts the buzzer is on for 20 secs. When the buzzer goes off then press the buzzer to turn on the buzzer for 20 seconds again.

yeah or to just stop the buzzer period

ill run this now thnx :melting_face:

this is perfect thanks so much!!

1 last thing how would i make it pause resume mid way?

Here is a better way without using delays. You can turn the LED on and off at any time:

const int btn = 2;
const int buzzer = 12;
const unsigned long buzzerOnTime = 20000;

void setup(void)
{
  Serial.begin(9600);
  pinMode(buzzer, OUTPUT);
  pinMode(btn, INPUT_PULLUP);
  digitalWrite(buzzer, HIGH);
}

void loop()
{
  static unsigned long prevMillis = millis();
  static int lastBtnState = digitalRead(btn);

  int btnState = digitalRead(btn);
  long currentMillis = millis();

  if (btnState != lastBtnState)
  {
    delay(50); // debounce
    lastBtnState = btnState;

    if (btnState == LOW)
    {
      // If buzzer is on turn it off. If buzzer is off then turn it on.
      digitalWrite(buzzer, !digitalRead(buzzer));
      prevMillis = currentMillis;
    }
  }

  if (digitalRead(buzzer) == HIGH && currentMillis - prevMillis >= buzzerOnTime)
  {
    // If buzzer has  been on for 20 seconds then turn it off
    digitalWrite(buzzer, LOW);
  }
}
1 Like

thanks man this is great i like the logic gonna save it to my buzzer doc for the future ;D

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.