1

I'm building a basic program that when a button is pressed, a buzzer comes on.

I would like however to make it so that the buzzer stops after a short period of time (e.g. 5-7 seconds).

Here is the code I have so far which is pretty basic I know forgive me but I have no clue on how to go forward.

// Setting The Pin Numbers
const int touchsensor = 2;     // the number of the touchsensor pin
const int buzzer =  4;         // the number of the LED pin

// variables:
int touchsensorState = 0;      // variable for reading the touchsensor status

void setup() {
  // initialize the buzzer as an output:
  pinMode(buzzer, OUTPUT);
  // initialize the touchsensor as an input:
  pinMode(touchsensor, INPUT);
}

void loop() {
  // read state of the pushbutton value:
  touchsensorState = digitalRead(touchsensor);

  // check if the touch sensor is pressed. If it is, the touchsensorState is HIGH:
  if (touchsensorState == HIGH) {
    // buzzer will emit sound:
    digitalWrite(buzzer, HIGH);
  } else {
    // buzzer will not sound:
    digitalWrite(buzzer, LOW);
  }
}
1

2 Answers 2

2

You set a timestamp = millis() when the button is pushed and then you disable the buzzer only when millis() - timestamp > timeout

if (touchsensorState == HIGH) {
    // buzzer will emit sound:
    digitalWrite(buzzer, HIGH);
    timestamp = millis();
} 
if(millis() - timestamp > timeout) {
    // buzzer will not sound:
    digitalWrite(buzzer, LOW);
}
1
  • Note: If you press the button during the time the buzzer is active, it will re-write the timestamp so that it is triggered in "now + timestamp", i.e. prolong the time when it's turned off. You may want to insert a "if button is already active, do not modify timestamp" logic if you need it. Commented Feb 28, 2018 at 16:08
0

You can time this with the millis() function. Refer to the BlinkWithoutDelay example from the Arduino IDE.

Then you set a timestamp, when the button is pressed:

if(touchsensorState == HIGH) timestamp = millis();

And then you are checking this timestamp against the current time:

if(millis() - timestamp > duration){
  digitalWrite(buzzer, HIGH);
} else {
  digitalWrite(buzzer, LOW);
}

duration is the timespan during which the buzzer should stay on after the button was released. If you want to turn it of, regardless of the button state, you can add a boolean flag to your if-statement, to set the timestamp only, when the button was pressed first:

if(!button_pressed && touchsensorState == HIGH){
  timestamp = millis();
  button_pressed = true;
}

In this case you have to reset the button_pressed variable to false, when you are turning of the buzzer in the millis()-else-statement

2
  • I keep getting timestamp was not declared in this scope?
    – Chris
    Commented Feb 28, 2018 at 13:42
  • because you have to declare it as a variable (of type unsigned long, since this is the return type of millis())
    – chrisl
    Commented Feb 28, 2018 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.