No free running unblocked loop should be without a heartbeat.
We can use the heartbeat to also run the buzzer when the conditions warrant.
Attention is better paid when the beeping is intermittent; on the same hand the beeping should not turn itself off.
Da rulz
OSHA Regulation 1290.210 s/s 1381 - Alarm Acknowledgement
In compliance with workplace safety standards, alarms shall not automatically silence without human acknowledgment. Acknowledgement must be performed manually by authorized personnel to ensure proper response and safety measures are enacted. Automatic silencing of alarms is prohibited to maintain clear and immediate hazard awareness.
So here is a complete sketch with the buzzer logic, ripped from an earlier sketch on this thread with errors corrected:
The code
// https://wokwi.com/projects/423998501637049345
const byte buzzer = 9;
const byte ldHeart = 8; // heartbeat LED
const byte alarm = 7; // pushbutton proxy for alarm condition
const byte shutUp = 6; // silence the beeping
const byte reset = 5; // pushbutton to reset action
# define PRESST LOW // active low pushbuttons
// heartbeat and alarm
bool beeping; // do we want noise with our heartbeat?
bool hasAlarmed; // have we said so once?
void setup() {
pinMode(alarm , INPUT_PULLUP);
pinMode(shutUp, INPUT_PULLUP);
pinMode(reset , INPUT_PULLUP);
pinMode(ldHeart , OUTPUT);
}
unsigned long now;
void loop() {
now = millis();
// run the heartbeat (and tone if beeping)
heartBeeper(); // run the LED heartbeat, maybe mke noise
// raise alarm when the PU has gone over 15, once only
// here not if (PU > 15.0) {
// but on a pushbutton
bool alarmCondition = digitalRead(alarm) == LOW;
if (alarmCondition) {
if (!hasAlarmed) beeping = true;
hasAlarmed = true;
}
// button kills the beeping
bool shutUpButton = digitalRead(shutUp) == LOW;
if (shutUpButton) beeping = false;
// reset the alarm mechanism for next time
bool resetButton = digitalRead(reset) == LOW;
if (resetButton) {
beeping = false;
hasAlarmed = false;
}
}
//
// heartbeat/buzzer preferences
# define onTime 333
# define offTime 444
# define buzzerFrequency 777
// LED heart beat, and noise if
void heartBeeper()
{
static bool on;
static unsigned lastTime;
if (now - lastTime < (on ? onTime : offTime)) return;
lastTime = now;
on = !on;
if (on) {
if (beeping)
tone(buzzer, buzzerFrequency);
digitalWrite(ldHeart, HIGH);
}
else {
noTone(buzzer);
digitalWrite(ldHeart, LOW);
}
}
The alarm button turns on the alarm. The shhh! silences it, and further alarms are ignored until the reset button is pressed.
Besides the function itself, a few things need to be flown in. This is the beauty of a free running loop - we can just put stuff in there to use up a tiny bit more of the resources available.
Some global boolean variables to control the beeping part of the heartbeat:
// heartbeat and alarm
bool beeping; // do we want noise with our heartbeat?
bool hasAlarmed; // have we said so once?
And in the loop, three four code sections that are independent of one the other and can be placed anywhere in the loop().
Attention for the function that blinks and might beep:
heartBeeper(); // run the LED heartbeat, maybe mke noise\
Logic to turn on the beeping:
if (alarmCondition) {
if (!hasAlarmed) beeping = true;
hasAlarmed = true;
}
Logic to silence it:
// button kills the beeping
bool shutUpButton = digitalRead(shutUp) == LOW;
if (shutUpButton) beeping = false;
and lastly a way to reset it for next time:
bool resetButton = digitalRead(reset) == LOW;
if (resetButton) {
beeping = false;
hasAlarmed = false;
}
HTH
a7