#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int voltagePin = A0;
const int currentPin = A1;
const int buttonPin = 2;
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address and dimensions
int displayMode = 0; // 0: voltage, 1: current, 2: power
bool buttonPressed = false; // Flag to detect button press
void setup() {
lcd.init();
lcd.backlight();
pinMode(buttonPin, INPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && !buttonPressed) {
// Button pressed, toggle display mode
displayMode = (displayMode + 1) % 3;
buttonPressed = true;
lcd.clear();
} else if (buttonState == LOW && buttonPressed) {
// Button released, reset flag
buttonPressed = false;
}
// Display selected mode
if (displayMode == 0) {
// Display solar voltage
int voltageValue = analogRead(voltagePin);
float voltage = (voltageValue * 5.0 / 1024.0);
lcd.setCursor(0, 0);
lcd.print("Solar: ");
lcd.print(voltage, 2); // Print with 2 decimal places
lcd.print("V");
} else if (displayMode == 1) {
// Display solar current
int currentValue = analogRead(currentPin);
float current = (currentValue * 5.0 / 1024.0);
lcd.setCursor(0, 0);
lcd.print("Battery: ");
lcd.print(current, 2); // Print with 2 decimal places
lcd.print("A");
} else {
// Display solar power
int voltageValue = analogRead(voltagePin);
float voltage = (voltageValue * 5.0 / 1024.0);
int currentValue = analogRead(currentPin);
float current = (currentValue * 5.0 / 1024.0);
float power = voltage * current;
lcd.setCursor(0, 0);
lcd.print("Load: ");
lcd.print(power, 2); // Print with 2 decimal places
lcd.print("W");
}
delay(500); // Debounce
}