// https://wokwi.com/projects/423146764908825601
const unsigned long measureEvery = 500; // measure and average every N milliseconds. 6 seconds?
const byte readNTimes = 10; // accumulate PUs after N measurements
const unsigned long puPeriod = 1UL * measureEvery * readNTimes;
// buzzer preferences
# define onTime 125
# define offTime 652
const byte button1 = 7;
const byte buzzer = 9;
const byte ldHeart = 8;
float sensor2;
float PU;
void setup() {
Serial.begin(115200);
Serial.println("puMonitor 0.0\n");
pinMode(ldHeart, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(button1, INPUT_PULLUP);
}
unsigned long now;
int nReadings;
float tAverage;
bool beeping; // do we want noise with our heartbeat?
bool hasAlarmed; // have we said so once?
void loop() {
static unsigned long lastTime;
now = millis();
heartBeeper(); // run the LED heartbeat, maybe mke noise
sensor2 = getTemperature();
if (sensor2 < 60.0) {
nReadings = 0;
tAverage = 0.0;
}
// button kills the beeping
int buttonState = digitalRead(button1) == LOW;
if (buttonState) beeping = false;
// time for a reading?
if (now - lastTime < measureEvery) return;
// yes
lastTime = now;
tAverage += sensor2;
nReadings++;
// enough readings
if (nReadings < readNTimes) return;
float theTemperature = tAverage / nReadings;
nReadings = 0;
tAverage = 0.0;
Serial.print(readNTimes);
Serial.print(" readings over ");
Serial.print(puPeriod);
Serial.print(" milliseconds averaging ");
Serial.print(theTemperature);
Serial.println(" are to be accumulated in the PU.");
// /// /// /// /// /// /// ///
// code to do that should be here. The value added now is fake:
float morePU = 3.14159 + random(10) / 3.33;
// /// /// /// /// /// /// ///
Serial.print(" adding ");
Serial.println(morePU);
PU += morePU;
// raise alarm when the PU has gone over 15, once only
if (PU > 15.0) {
if (!hasAlarmed) beeping = true;
hasAlarmed = true;
}
}
//proxy slide fader 50-100 degrees
float getTemperature()
{
int raw = analogRead(A0);
float temperature = 50.0 + 50.0 * raw / 1024.0;
// occasionally report the process temperature
static unsigned long lastTime;
if (now - lastTime > 1000) {
Serial.println(temperature);
lastTime = now;
}
return temperature;
}
// 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) {
// cat does not like buzzer!
// tone(buzzer, 777);
if (beeping) Serial.println(" BEEP!");
digitalWrite(ldHeart, HIGH);
}
else {
// noTone(buzzer);
digitalWrite(ldHeart, LOW);
}
}
TEMPERATURE
50 . . . . . . . . . . . . . 100
SHHH!
ALIVE!