#include <TM1637Display.h>
#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Pin definitions for TM1637
#define CLK 2
#define DIO 3
// Pin definitions for DS18B20
#define ONE_WIRE_BUS 4 // DS18B20 data pin
// Pin definitions for buttons and LED
#define BUTTON_UP 5 // Increase setpoint
#define BUTTON_DOWN 6 // Decrease setpoint and toggle mode
#define LED_PIN 7
// EEPROM address for setpoint
#define EEPROM_ADDRESS 0
// Setup OneWire and DallasTemperature
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Variables
TM1637Display display(CLK, DIO);
const uint8_t C[] = {
SEG_B | SEG_A | SEG_F | SEG_G,
SEG_A | SEG_D | SEG_E | SEG_F
};
float temperature;
int setPoint = 25; // Default setpoint in Celsius
bool inSetPointMode = false; // Set to true when setpoint mode is active
// Button state and debouncing
bool upButtonState = false;
bool downButtonState = false;
unsigned long lastDebounceTime = 0;
const long debounceDelay = 50;
void setup() {
// Initialize serial
Serial.begin(9600);
// Initialize DS18B20 sensor
sensors.begin();
// Initialize display
display.setBrightness(0x0f);
// Button inputs with internal pull-ups
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
// LED pin
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Ensure LED is off initially
// Load setpoint from EEPROM
int storedSetPoint;
EEPROM.get(EEPROM_ADDRESS, storedSetPoint);
if (storedSetPoint >= -9 && storedSetPoint <= 99) {
setPoint = storedSetPoint;
} else {
setPoint = 25; // Default if EEPROM value is invalid
EEPROM.put(EEPROM_ADDRESS, setPoint);
}
}
void loop() {
// Check for button presses
checkButtons();
// Read temperature from DS18B20
temperature = readTemperature();
// Display either setpoint or temperature
if (inSetPointMode) {
displaySetPoint();
} else {
displayTemperature();
}
// Control LED based on temperature vs setpoint
if (temperature > setPoint) {
digitalWrite(LED_PIN, HIGH); // Turn LED on if temp > setpoint
} else {
digitalWrite(LED_PIN, LOW); // Turn LED off if temp <= setpoint
}
}
// Function to read temperature from DS18B20
float readTemperature() {
sensors.requestTemperatures();
float tempCelsius = sensors.getTempCByIndex(0);
if (tempCelsius == DEVICE_DISCONNECTED_C) {
return -127.0; // Error value if sensor is disconnected
}
return tempCelsius;
}
// Function to display the temperature on the TM1637 display
void displayTemperature() {
int displayTemp = (int)(temperature * 1); // Convert to 1 decimal place
// display.showNumberDecEx(displayTemp, false, 2, 0); // Display with decimal point
display.showNumberDecEx(displayTemp, 0b0100, false, 2, 0);
display.setSegments(C, 2, 2); // Show "C" symbol
}
// Function to display the setpoint
void displaySetPoint() {
display.showNumberDec(setPoint, false, 2, 0);
// Flash to indicate setpoint mode
if (millis() % 1000 < 500) {
display.clear();
}
}
// Function to check button presses and adjust setpoint
void checkButtons() {
unsigned long currentMillis = millis();
// Read button states
int upReading = digitalRead(BUTTON_UP);
int downReading = digitalRead(BUTTON_DOWN);
// Debounce UP button (increase setpoint)
if (upReading != upButtonState) {
lastDebounceTime = currentMillis;
}
if ((currentMillis - lastDebounceTime) > debounceDelay && upReading == LOW) {
if (!inSetPointMode) {
inSetPointMode = true; // Enter setpoint mode
} else {
setPoint++;
if (setPoint > 99) setPoint = 99; // Max limit
EEPROM.put(EEPROM_ADDRESS, setPoint); // Save to EEPROM
}
}
upButtonState = upReading;
// Debounce DOWN button (decrease setpoint or exit mode)
if (downReading != downButtonState) {
lastDebounceTime = currentMillis;
}
if ((currentMillis - lastDebounceTime) > debounceDelay && downReading == LOW) {
if (!inSetPointMode) {
inSetPointMode = true; // Enter setpoint mode
} else {
setPoint--;
if (setPoint <-9 )
{
setPoint = 0; // Min limit
inSetPointMode = false; // Exit setpoint mode when reaching 0
}
EEPROM.put(EEPROM_ADDRESS, setPoint); // Save to EEPROM
}
}
downButtonState = downReading;
// Auto-exit setpoint mode after 5 seconds of inactivity
if (inSetPointMode && (currentMillis - lastDebounceTime) > 2000) {
inSetPointMode = false;
}
}