My problem is I want the buzzer to stop after a certain amount of time, regardless if it is still pressed or not.
I have tried to implement a boolean statement to the code but to no avail(I'm not very good at this)
Can someone shed some light as to how I do this?
The code already times out after 1000ms when your finger is lifted off the buttin so I got that far but cant get any further.
Here is what I have.
Any other suggestions are also welcome as I know its not the prettiest or best implemented but i was just happy it worked for me.
// 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
unsigned long timestamp = 0;
unsigned long timeout = 1000;
void setup() {
pinMode(buzzer, OUTPUT); // initialize the buzzer as an output
pinMode(touchsensor, INPUT); // initialize the touchsensor as an input
}
void loop() {
touchsensorState = digitalRead(touchsensor); // read state of the pushbutton value:
if (touchsensorState == HIGH) { // check if the touch sensor is pressed. If it is, the touchsensorState is HIGH:
digitalWrite(buzzer, HIGH); // buzzer will emit sound:
timestamp = millis(); // Timing button press
}
if(millis() - timestamp > timeout) {
digitalWrite(buzzer, LOW); // buzzer will not sound:
}
}