// 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);
}
}