#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// DS18B20 Temperature sensor
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Sensor pins
const int currentSensorPin = A0; // Potentiometer simulating current sensor
const int voltageSensorPin = A1; // Potentiometer simulating voltage sensor
// LED pins
const int chargingLED = 7;
const int fullChargeLED = 8;
// Button pins
const int startButton = 2;
const int stopButton = 3;
// Variables for sensor readings
float currentReading = 0.0;
float voltageReading = 0.0;
float tempReading = -127.0; // Initialize to an invalid temperature
// Optimal values
const float optimalTempLow = 20.0;
const float optimalTempHigh = 25.0;
const float optimalCurrent = 10.0; // Example optimal current in amps
const float optimalVoltage = 12.6; // Example optimal voltage in volts
// Function prototypes
void readSensors();
void updateDisplay();
void handleButtons();
void checkChargingStatus();
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the LCD
lcd.begin(16, 2);
lcd.print("Smart Charging");
// Initialize the temperature sensor
sensors.begin();
// Set LED pins as output
pinMode(chargingLED, OUTPUT);
pinMode(fullChargeLED, OUTPUT);
// Set button pins as input
pinMode(startButton, INPUT_PULLUP);
pinMode(stopButton, INPUT_PULLUP);
// Initial LED state
digitalWrite(chargingLED, LOW);
digitalWrite(fullChargeLED, LOW);
}
void loop() {
readSensors();
updateDisplay();
handleButtons();
checkChargingStatus();
delay(1000); // Update every second
}
void readSensors() {
// Read current sensor (potentiometer)
int currentSensorValue = analogRead(currentSensorPin);
currentReading = (currentSensorValue * 5.0 / 1023.0) * 10.0; // Example conversion
// Read voltage sensor (potentiometer)
int voltageSensorValue = analogRead(voltageSensorPin);
voltageReading = (voltageSensorValue * 5.0 / 1023.0) * 11.0; // Example conversion
// Read temperature sensor
sensors.requestTemperatures();
tempReading = sensors.getTempCByIndex(0);
}
void updateDisplay() {
lcd.clear();
// Check if readings are valid
bool validCurrent = currentReading > 0;
bool validVoltage = voltageReading > 0;
bool validTemp = tempReading != -127.0;
if (validCurrent && validVoltage && validTemp) {
lcd.setCursor(0, 0);
lcd.print("Cur: ");
lcd.print(currentReading);
lcd.print(" A");
lcd.setCursor(0, 1);
lcd.print("Volt: ");
lcd.print(voltageReading);
lcd.print(" V");
delay(2000); // Display current readings for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(tempReading);
lcd.print(" C");
delay(2000); // Display temperature reading for 2 seconds
} else {
lcd.setCursor(0, 0);
lcd.print("Opt Temp: ");
lcd.print(optimalTempLow);
lcd.print("-");
lcd.print(optimalTempHigh);
lcd.print("C");
Serial.print("Optimal Temperature: ");
Serial.print(optimalTempLow);
Serial.print(" - ");
Serial.print(optimalTempHigh);
Serial.println(" C");
lcd.setCursor(0, 1);
lcd.print("Opt Cur: ");
lcd.print(optimalCurrent);
lcd.print("A ");
delay(2000); // Display optimal temperature and current for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Opt Volt: ");
lcd.print(optimalVoltage);
lcd.print(" V");
delay(2000); // Display optimal voltage for 2 seconds
}
}
void handleButtons() {
if (digitalRead(startButton) == LOW) {
digitalWrite(chargingLED, HIGH);
digitalWrite(fullChargeLED, LOW);
}
if (digitalRead(stopButton) == LOW) {
digitalWrite(chargingLED, LOW);
digitalWrite(fullChargeLED, HIGH);
}
}
void checkChargingStatus() {
// Simple condition for full charge based on voltage level
if (voltageReading > 12.6) {
digitalWrite(chargingLED, LOW);
digitalWrite(fullChargeLED, HIGH);
} else {
digitalWrite(chargingLED, HIGH);
digitalWrite(fullChargeLED, LOW);
}
}