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);
}
}
delay
for your wanted amount of milliseconds, then turn the buzzer off. Little bit harder but more efficient way: Blink without Delay technique (arduino.cc/en/Tutorial/BlinkWithoutDelay)