// Include necessary libraries
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Data wire is connected to pin 2 on the Arduino Mega
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Set the LCD I2C address (usually 0x27 or 0x3F)
// Parameters: (I2C address, columns, rows)
LiquidCrystal_I2C lcd(0x27, 20, 4); // Change 0x27 to 0x3F if needed
// Define pins
#define RELAY_PIN 8 // Relay control pin connected to digital pin 8
#define RELAY_PIN_2 9 // Second relay control pin connected to digital pin 9
#define LED_PIN 13 // LED connected to digital pin 13
#define BUTTON_PIN 7 // Pushbutton connected to digital pin 7
#define BATTERY_PIN A0 // Potentiometer connected to analog pin A0
// Temperature and battery voltage thresholds
const float TEMP_THRESHOLD = 60.0; // Temperature threshold in Celsius
const float VOLTAGE_THRESHOLD = 3.5; // Voltage threshold in Volts
// Variables for button state and system status
bool systemActive = false; // Monitoring system state (active/inactive)
// Debounce variables
bool buttonState = HIGH; // Current debounced button state
bool lastButtonReading = HIGH; // Previous reading from the input pin
unsigned long lastDebounceTime = 0; // Last time the button state changed
unsigned long debounceDelay = 50; // Debounce time in milliseconds
// Timing variables
unsigned long previousMillis = 0; // Stores last time readings were updated
const unsigned long interval = 1000; // Interval at which to read sensors (milliseconds)
// Variables for second relay timing
bool relay2Active = false; // Whether the second relay is currently active
unsigned long relay2StartTime = 0; // Time when the second relay was activated
const unsigned long RELAY2_DURATION = 5000; // Duration to keep the second relay active (milliseconds)
// Variables to detect overheat event transitions
bool overheat = false; // Current overheat status
bool previousOverheat = false; // Previous overheat status
// Custom characters for up and down arrows
byte upArrow[8] = {
0b00100,
0b01110,
0b10101,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100
};
byte downArrow[8] = {
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b10101,
0b01110,
0b00100
};
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
sensors.begin();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Ensure relay is off initially
pinMode(RELAY_PIN_2, OUTPUT);
digitalWrite(RELAY_PIN_2, LOW); // Ensure second relay is off initially
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Ensure LED is off initially
pinMode(BUTTON_PIN, INPUT_PULLUP); // Enable internal pull-up resistor
// Create custom characters
lcd.createChar(0, upArrow); // Store up arrow at position 0
lcd.createChar(1, downArrow); // Store down arrow at position 1
// Display welcome message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Monitoring System");
lcd.setCursor(0, 1);
lcd.print("Press Button to");
lcd.setCursor(0, 2);
lcd.print("Start Monitoring");
}
void loop() {
// Read the state of the pushbutton
bool reading = digitalRead(BUTTON_PIN);
// Check if the button state has changed (due to noise or pressing)
if (reading != lastButtonReading) {
lastDebounceTime = millis(); // Reset the debouncing timer
}
// If the button state has been stable for longer than debounceDelay
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the button state has changed
if (reading != buttonState) {
buttonState = reading;
// Only toggle the system state if the button is pressed (transition from HIGH to LOW)
if (buttonState == LOW) {
systemActive = !systemActive;
if (systemActive) {
// System is now active
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Monitoring Started");
Serial.println("Monitoring Started");
previousMillis = millis(); // Reset the update timer
delay(2000); // Display the message for 2 seconds
lcd.clear();
} else {
// System is now inactive
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Monitoring Stopped");
Serial.println("Monitoring Stopped");
// Turn off relays and LED
digitalWrite(RELAY_PIN, LOW);
digitalWrite(RELAY_PIN_2, LOW);
digitalWrite(LED_PIN, LOW);
// Reset overheat variables
overheat = false;
previousOverheat = false;
relay2Active = false;
delay(2000); // Display the message for 2 seconds
lcd.clear();
// Display paused message
lcd.setCursor(0, 0);
lcd.print("Monitoring Paused ");
lcd.setCursor(0, 1);
lcd.print("Press Button to");
lcd.setCursor(0, 2);
lcd.print("Start Monitoring");
}
}
}
}
// Save the current reading for next time
lastButtonReading = reading;
// If the system is active, perform monitoring
if (systemActive) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Request temperature readings from all devices on the bus
sensors.requestTemperatures();
// Fetch the temperature in Celsius
float temperatureC = sensors.getTempCByIndex(0);
// Read the battery voltage
int batteryRaw = analogRead(BATTERY_PIN);
float batteryVoltage = (batteryRaw / 1023.0) * 5.0; // Assuming a 0-5V range
// Determine which arrow to display for temperature
byte tempArrow = (temperatureC > TEMP_THRESHOLD) ? byte(0) : byte(1); // Up arrow if over threshold
// Determine which arrow to display for voltage
byte voltageArrow = (batteryVoltage > VOLTAGE_THRESHOLD) ? byte(0) : byte(1); // Up arrow if over threshold
// Check for overheat condition (temperature or voltage exceeds threshold)
if (temperatureC > TEMP_THRESHOLD || batteryVoltage > VOLTAGE_THRESHOLD) {
overheat = true;
// If this is a new overheat event
if (overheat && !previousOverheat) {
// Activate the second relay
digitalWrite(RELAY_PIN_2, HIGH);
relay2Active = true;
relay2StartTime = millis();
// Activate the first relay
digitalWrite(RELAY_PIN, HIGH);
// Turn on the LED
digitalWrite(LED_PIN, HIGH);
// Optional: Print to serial monitor
Serial.println("WARNING: Overheat event!");
}
// Keep the first relay and LED on as long as overheat condition persists
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
// Clear the LCD screen
lcd.clear();
// Display "System Status"
lcd.setCursor(0, 0);
lcd.print("System Status:");
lcd.setCursor(0, 1);
lcd.print("WARNING: Overheat!");
// Display only the data of the sensor that exceeded its threshold
if (temperatureC > TEMP_THRESHOLD && batteryVoltage <= VOLTAGE_THRESHOLD) {
// Only temperature exceeded
lcd.setCursor(0, 2);
lcd.print("Temp:");
lcd.print(temperatureC, 1);
lcd.print("C ");
lcd.write(tempArrow);
} else if (batteryVoltage > VOLTAGE_THRESHOLD && temperatureC <= TEMP_THRESHOLD) {
// Only voltage exceeded
lcd.setCursor(0, 2);
lcd.print("Voltage:");
lcd.print(batteryVoltage, 2);
lcd.print("V ");
lcd.write(voltageArrow);
} else {
// Both exceeded
lcd.setCursor(0, 2);
lcd.print("Temp:");
lcd.print(temperatureC, 1);
lcd.print("C ");
lcd.write(tempArrow);
lcd.setCursor(0, 3);
lcd.print("Voltage:");
lcd.print(batteryVoltage, 2);
lcd.print("V ");
lcd.write(voltageArrow);
}
} else {
overheat = false;
// Temperature and voltage are normal
digitalWrite(RELAY_PIN, LOW); // Ensure first relay is off
digitalWrite(LED_PIN, LOW); // Ensure LED is off
// Reset overheat variables
relay2Active = false;
// Clear the LCD screen
lcd.clear();
// Display the current temperature with arrow
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.print(temperatureC, 1);
lcd.print("C ");
lcd.write(tempArrow);
// Display the battery voltage with arrow
lcd.setCursor(0, 1);
lcd.print("Voltage:");
lcd.print(batteryVoltage, 2);
lcd.print("V ");
lcd.write(voltageArrow);
// Display "System Status"
lcd.setCursor(0, 2);
lcd.print("System Status:");
lcd.setCursor(0, 3);
lcd.print("OK");
}
// Handle the second relay timing
if (relay2Active) {
if (millis() - relay2StartTime >= RELAY2_DURATION) {
// Deactivate the second relay after the specified duration
digitalWrite(RELAY_PIN_2, LOW);
relay2Active = false;
}
}
// Update previous overheat status
previousOverheat = overheat;
}
}
// No need for an else block here since the "Monitoring Paused" message is handled during state change
}
Loading
ds18b20
ds18b20