Hello,
I've built an LCD display on Arduino, that should show variety of text depending on which pin shows voltage. However, when one of the pins is active the text will remain true on the screen for a couple of seconds and then start blinking just to show text from the other if statements. It should remain true all the time, I don't know why it does this. Can anyone notice why? This is the code I've used:
#include <LiquidCrystal.h>
// Constants for LCD and pins
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
// LCD object initialization
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Function declarations
void initializePins();
void updateLCD(const String& line1, const String& line2);
void checkSensorStates();
void displayAllInfo();
// Store the last displayed message
String lastLine1 = "";
String lastLine2 = "";
void setup() {
Serial.begin(9600);
lcd.begin(LCD_COLS, LCD_ROWS);
initializePins();
displayAllInfo(); // Initial display to avoid blank start
}
void loop() {
checkSensorStates();
delay(1200); // Delay between reads, adjust as needed
}
void initializePins() {
// Setup pin modes
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(10, HIGH); // Fans on initially
digitalWrite(13, HIGH);
}
void updateLCD(const String& line1, const String& line2) {
if (line1 != lastLine1 || line2 != lastLine2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
lastLine1 = line1;
lastLine2 = line2;
}
}
void displayAllInfo() {
updateLCD("TEMP BALANCE", "Current SoC");
}
void checkSensorStates() {
int val_0 = analogRead(A0);
int val_1 = analogRead(A1);
int val_2 = analogRead(A3);
int val_3 = analogRead(A5);
if (val_0 >= 1023) {
updateLCD("TEMP HIGH", "CHECK SYS");
} else if (val_1 >= 1023) {
digitalWrite(10, LOW);
digitalWrite(13, LOW);
updateLCD("Shade needed", "Fan rest");
} else if (val_2 >= 1023) {
updateLCD("Levels uneven", "Align cells");
} else if (val_3 >= 1023) {
updateLCD("Energy warning", "Seek charge");
} else {
displayAllInfo();
}
}
This is the schematics that I've tried IRL that still fails: