I am quite new to this but have learned lots and am very close to achieving my goal for this project. I have written a scipt for esp32 using 2 ds18b20 sensors and an oled. The oled displays temp from both sensors. If either sensor exceeds a temp threshold then it starts beeping using hardware pwm for 30 seconds or until the temp falls back below the threshold. If temp goes above threshold again it starts beeping again. It all seems to work as expected with one exception. Once the temp goes over the threshold, the temp displayed on the oled stops updating. It just stays at 35 even if temp continues to rise. The alarm sequence seems to be conflicting with the display updating. Can someone please help by telling me why?
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <OneWire.h>
#include <DallasTemperature.h>
int temp1;
int temp2;
const char* ssid = "";
const char* password = "";
/* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
#define ONE_WIRE_BUS 13 // Pin for both temperature sensors
#define PIEZO_PIN 18 // Pin where the piezo buzzer is connected
#define BEEP_DURATION 30000
#define THRESHOLD 35
#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
bool alarmActive = false;
void setup() {
Serial.begin(115200);
pinMode(PIEZO_PIN, OUTPUT);
sensors.begin();
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
display.begin(0x3c, true);
display.clearDisplay();
display.setTextColor(SH110X_WHITE);
}
void loop() {
ArduinoOTA.handle();
sensors.requestTemperatures();
int temp1 = sensors.getTempCByIndex(0);
int temp2 = sensors.getTempCByIndex(1);
display.setCursor(0,0);
display.setTextSize (1);
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Left Bank: ");
display.setTextSize(2);
display.setCursor(0,13);
display.print(temp1);
display.setTextSize(1);
display.cp437(true);
display.write(248);
display.setTextSize(2);
display.print("C");
display.setTextSize(1);
display.setCursor(0,37);
display.print("Right Bank: ");
display.setTextSize(2);
display.setCursor(0,50);
display.print(temp2);
display.setTextSize(1);
display.cp437(true);
display.write(248);
display.setTextSize(2);
display.print("C");
display.display();
if (temp1 >= THRESHOLD || temp2 >= THRESHOLD) {
activateAlarm();
} else {
if (alarmActive) {
deactivateAlarm();
}
}
}
void activateAlarm() {
alarmActive = true;
unsigned long startTime = millis();
while (alarmActive && millis() - startTime < BEEP_DURATION) {
tone(PIEZO_PIN, 1000); // Beep at 1000Hz
delay(500); // Beep for 500ms
noTone(PIEZO_PIN); // Turn off the beep
delay(500); // Wait for 500ms before the next beep
sensors.requestTemperatures(); // Update temperature readings
int temp1 = sensors.getTempCByIndex(0);
int temp2 = sensors.getTempCByIndex(1);
if (temp1 < THRESHOLD && temp2 < THRESHOLD) {
alarmActive = false; // If temperature drops below threshold, stop beeping
}
}
}
void deactivateAlarm() {
alarmActive = false;
noTone(PIEZO_PIN); // Turn off the beep
}