/*
Ember Mug 4
This sketch controls the Ember Mug 4, which maintains the ideal temperature
for beverages using an ESP32. It features a DHT22 temperature sensor, a relay
module to control the heater, a LED bar graph to indicate the desired temperature,
a seven-segment display to show the current temperature, and tactile buttons
to adjust the ideal temperature.
The circuit:
* DHT22 sensor connected to pin 15
* Relay module connected to pin 4
* LED bar graph connected to pins 2, 4, 5, 18, 19, 21, 22, 23
* Seven segment display connected to pins 12, 13, 14, 27, 32, 33, 25
* Ultrasonic sensor connected to pins 5 (trigger) and 18 (echo)
* Push buttons connected to pins 19 (increase) and 21 (decrease)
* LED indicator connected to pin 23
Created 8 November 2024
By Claudio Moreno
*/
// Pin definitions
#define DHT_PIN 15
#define RELAY_PIN 4
#define LED_BAR_PINS {2, 4, 5, 18, 19, 21, 22, 23}
#define SEVEN_SEGMENT_PINS {12, 13, 14, 27, 32, 33, 25}
#define ULTRASONIC_TRIG_PIN 5
#define ULTRASONIC_ECHO_PIN 18
#define BUTTON_INCREASE_PIN 19
#define BUTTON_DECREASE_PIN 21
#define LED_INDICATOR_PIN 23
// Temperature settings
float idealTemperature = 62.0; // Starting ideal temperature
const float MIN_TEMP = 55.0;
const float MAX_TEMP = 70.5;
const float TEMP_STEP = 0.5;
// Include necessary libraries
#include <Arduino.h>
#include <DHTesp.h>
#include "UltrasonicSensor.h"
#include "RelayController.h"
#include "LedBarGraph.h"
#include "SevenSegmentDisplay.h"
// Create instances of the classes
DHTSensor dhtSensor(DHT_PIN);
RelayController relayController(RELAY_PIN);
UltrasonicSensor ultrasonicSensor(ULTRASONIC_TRIG_PIN, ULTRASONIC_ECHO_PIN);
int ledBarPins[] = LED_BAR_PINS;
LedBarGraph ledBarGraph(ledBarPins, sizeof(ledBarPins) / sizeof(ledBarPins[0]));
int sevenSegmentPins[] = SEVEN_SEGMENT_PINS;
SevenSegmentDisplay sevenSegmentDisplay(sevenSegmentPins, sizeof(sevenSegmentPins) / sizeof(sevenSegmentPins[0]));
void setup() {
Serial.begin(115200);
// Initialize components
dhtSensor.begin();
relayController.begin();
ultrasonicSensor.begin();
ledBarGraph.begin();
sevenSegmentDisplay.begin();
// Initialize buttons
pinMode(BUTTON_INCREASE_PIN, INPUT_PULLUP);
pinMode(BUTTON_DECREASE_PIN, INPUT_PULLUP);
pinMode(LED_INDICATOR_PIN, OUTPUT);
}
void loop() {
// Read distance from ultrasonic sensor
float distance = ultrasonicSensor.readDistance();
// Check if the mug is empty
if (distance >= 18.0) {
// Enter standby mode
relayController.deactivate();
digitalWrite(LED_INDICATOR_PIN, LOW);
ledBarGraph.clear();
sevenSegmentDisplay.clear();
delay(1000); // Add a delay to prevent rapid looping
return; // Exit loop if mug is empty
}
// Read temperature from DHT22 sensor
float currentTemperature = dhtSensor.getTemperature();
// Update LED bar graph based on ideal temperature
int ledCount = map(idealTemperature, MIN_TEMP, MAX_TEMP, 0, 8); // Map temperature to LED count
ledBarGraph.clear();
for (int i = 0; i < ledCount; i++) {
ledBarGraph.lightUp(i);
}
// Update seven segment display
sevenSegmentDisplay.display(currentTemperature);
// Control relay based on temperature
if (currentTemperature < idealTemperature) {
relayController.activate(); // Turn on heater
digitalWrite(LED_INDICATOR_PIN, HIGH); // Turn on LED indicator
} else {
relayController.deactivate(); // Turn off heater
digitalWrite(LED_INDICATOR_PIN, LOW); // Turn off LED indicator
}
// Check button states
if (digitalRead(BUTTON_INCREASE_PIN) == LOW) {
if (idealTemperature < MAX_TEMP) {
idealTemperature += TEMP_STEP; // Increase ideal temperature
delay(200); // Debounce delay
}
}
if (digitalRead(BUTTON_DECREASE_PIN) == LOW) {
if (idealTemperature > MIN_TEMP) {
idealTemperature -= TEMP_STEP; // Decrease ideal temperature
delay(200); // Debounce delay
}
}
delay(100); // Short delay to prevent rapid looping
}