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);
  }
}
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);
}
  • 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. – Maximilian Gerhardt Feb 28 '18 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

  • I keep getting timestamp was not declared in this scope? – Chris Feb 28 '18 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 Feb 28 '18 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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