#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Pin where the DS18B20 is connected
#define ONE_WIRE_BUS 4
// Pin where the beeper is connected
#define BEEPER_PIN 6
// Pin where the LEDs are connected
#define LED_PIN 2
// Define the screen width and height for your SSD1306 display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for SSD1306 display connected using I2C (SDA, SCL pins)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Create instances of the required classes
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Variables to track temperature readings
int aboveFreezingCount = 0;
bool hasDroppedBelowFreezing = false;
float temperatureF = 0.0;
void setup() {
// Start serial communication for debugging (optional)
Serial.begin(9600);
// Initialize the DS18B20 sensor
sensors.begin();
// Set the beeper and LED pins as output
pinMode(BEEPER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// Ensure beeper is off
digitalWrite(BEEPER_PIN, LOW);
// Screen setup
setupScreen();
// Intialize is done
beep(100,2);
}
void loop() {
updateDisplay(false, 0);
// Check if the temperature is above 32F
if (temperatureF > 32.0) {
if (hasDroppedBelowFreezing) { // Only count if the temp has dropped below freezing at least once
aboveFreezingCount++;
digitalWrite(LED_PIN, HIGH); // Turn on the LED
// If the temperature has been above 32F for 4 consecutive readings
if (aboveFreezingCount >= 4) {
while (temperatureF > 40.0) {
updateDisplay(false, 0);
// Fire the alarm
beep(1000, 1);
// Wait for 30 seconds before recheck
for (int i = 1; i < 31; i++) {
updateDisplay(true, i);
delay(1000);
}
}
}
} else {
// Not Yet Dropped Below 40F"
digitalWrite(LED_PIN, HIGH);
}
} else {
// If temperature drops below 32F
if (hasDroppedBelowFreezing == false){
hasDroppedBelowFreezing = true; // Set flag to true
beep(3,100);
}
aboveFreezingCount = 0; // Reset the count
digitalWrite(LED_PIN, LOW); // Turn off the LED
}
updateDisplay(false, 0);
// Wait for 1 second before the next check
delay(1000);
}
void updateDisplay(bool isRechecking, int recheckCount) {
sensors.requestTemperatures(); // Request temperature readings
temperatureF = sensors.getTempFByIndex(0);
display.clearDisplay();
display.setTextSize(1); // Med text
display.setCursor(0, 0); // Set cursor to the top-left corner
display.print("Temp ");
display.setTextSize(2); // Large text
display.print(temperatureF);
display.setTextSize(1); // Med text
display.setCursor(0, 24); // Move cursor down to write "Temp"
display.print("Over Temp ");
display.setTextSize(2); // Large text
display.print(aboveFreezingCount);
display.print("/4");
if (isRechecking) {
display.setTextSize(1); // Med text
display.setCursor(0, 48);
display.print("Recheck ");
display.setTextSize(2); // Large text
display.print(recheckCount);
display.print("/30");
}
display.display(); // Update the display with the data
}
void beep(int beepDuration, int beepCount) {
for (int i = 0; i < beepCount; i++) {
digitalWrite(BEEPER_PIN, HIGH);
delay(beepDuration);
digitalWrite(BEEPER_PIN, LOW);
delay(beepDuration);
}
}
void setupScreen() {
// Initialize the SSD1306 display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.setTextColor(SSD1306_WHITE); // White text
}