#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define BUZZER_PIN 4
#define MODE_BUTTON 12
#define CONFIRM_BUTTON 13
#define RETURN_BUTTON 14
int mode = 0; // 0: Not selected, 1: Hot Mode, 2: Cold Mode
int activeMode = 0; // The mode that has been confirmed
float temperature = 0.0;
bool isModeActive = false;
unsigned long lastTempRequest = 0;
unsigned long tempInterval = 1000; // 1 second interval for temperature updates
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
pinMode(MODE_BUTTON, INPUT_PULLUP);
pinMode(CONFIRM_BUTTON, INPUT_PULLUP);
pinMode(RETURN_BUTTON, INPUT_PULLUP);
sensors.begin();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
handleButtons();
unsigned long currentMillis = millis();
if (isModeActive && currentMillis - lastTempRequest >= tempInterval) {
lastTempRequest = currentMillis;
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);
}
display.clearDisplay();
if (isModeActive) {
if (activeMode == 1) {
display.setCursor(0, 0);
centerTextOnDisplay("Hot Mode");
checkTemperatureHotMode();
} else if (activeMode == 2) {
display.setCursor(0, 0);
centerTextOnDisplay("Cold Mode");
checkTemperatureColdMode();
}
centerTextOnDisplay("Temp: " + String(temperature) + " C");
} else {
displayModeSelectionScreen();
}
display.display();
}
void handleButtons() {
static unsigned long lastButtonPress = 0;
unsigned long debounceDelay = 50; // 50 milliseconds debounce
// Track the previous and current state of the MODE_BUTTON
static bool lastModeButtonState = HIGH;
bool modeButtonState = digitalRead(MODE_BUTTON);
// Detect the falling edge: button pressed down and released
if (modeButtonState == LOW && lastModeButtonState == HIGH) {
if (millis() - lastButtonPress > debounceDelay) {
mode = (mode + 1) % 3; // Cycle through 0, 1, 2 (0: Not selected)
lastButtonPress = millis(); // Update debounce timer
}
}
// Track the previous and current state of the CONFIRM_BUTTON
static bool lastConfirmButtonState = HIGH;
bool confirmButtonState = digitalRead(CONFIRM_BUTTON);
if (confirmButtonState == LOW && lastConfirmButtonState == HIGH) {
if (millis() - lastButtonPress > debounceDelay) {
if (mode != 0) {
activeMode = mode; // Lock the mode
isModeActive = true; // Activate the mode
}
lastButtonPress = millis(); // Update debounce timer
}
}
// Track the previous and current state of the RETURN_BUTTON
static bool lastReturnButtonState = HIGH;
bool returnButtonState = digitalRead(RETURN_BUTTON);
if (returnButtonState == LOW && lastReturnButtonState == HIGH) {
if (millis() - lastButtonPress > debounceDelay) {
activeMode = 0; // Deactivate the mode
isModeActive = false; // Return to selection
mode = 0; // Reset mode selection
lastButtonPress = millis(); // Update debounce timer
}
}
// Update the last button states
lastModeButtonState = modeButtonState;
lastConfirmButtonState = confirmButtonState;
lastReturnButtonState = returnButtonState;
}
void checkTemperatureHotMode() {
if (temperature > 50.0) {
digitalWrite(BUZZER_PIN, HIGH); // Beep quickly
} else if (temperature < 45.0) {
digitalWrite(BUZZER_PIN, LOW); // Stop buzzer
}
}
void checkTemperatureColdMode() {
if (temperature > 20.0) {
digitalWrite(BUZZER_PIN, HIGH); // Beep quickly
} else if (temperature < 10.0) {
digitalWrite(BUZZER_PIN, LOW); // Stop buzzer
}
}
void displayModeSelectionScreen() {
display.setTextSize(2);
display.setCursor(0, 0);
centerTextOnDisplay("MODE");
display.setCursor(0, 16);
centerTextOnDisplay("SELECTION");
display.setTextSize(1);
display.setCursor(0, 40);
if (mode == 1) {
centerTextOnDisplay("> Hot Mode");
} else {
centerTextOnDisplay(" Hot Mode");
}
display.setCursor(0, 50);
if (mode == 2) {
centerTextOnDisplay("> Cold Mode");
} else {
centerTextOnDisplay(" Cold Mode");
}
}
void centerTextOnDisplay(String text) {
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
display.setCursor((SCREEN_WIDTH - w) / 2, display.getCursorY());
display.println(text);
}