Hello guys, I dont know if this is the proper place to post my question, So if it will fit to somewhere else please move the theard, Thanks!
I am using arduino uno, and lcd 20*4 with I2C driver.
I've created a "Menu" template controlled by 4 buttons.
for some reason the screen is flickering, I've upload a demonstration to show you how it look:
this is the code:
#include <LiquidCrystal_I2C.h>
#define BUTTONCLICKED 0
#define UPBUTTON 8
#define DOWNBUTTON 9
#define ONOFFBUTTON 10
#define SETBUTTON 11
#define LEDPIN 13
#define UP '+'
#define DOWN '-'
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
const int numOfButtons = 4;
const int buttonPins[numOfButtons] = {8, 9, 10, 11};
const int numOfScreens = 8;
int currentScreen = 0;
bool setButtonState = 0;
long lastRenderTime = 0;
String screens[numOfScreens][4] = {
{"Temperature:","Humidity:","Duty Cycle:","Run-Time:"},
{"TEMPERATURE: ","X"},
{"DIFFERETIAL: ","X"},
{"CALIBRATION:","X"},
{"HTA","X"},
{"LTA","X"},
{"HHA","X"},
{"LHA","X"},
};
void screenChanger(char state){
lastRenderTime = millis();
if(state == UP){
if(currentScreen == numOfScreens-1){
currentScreen = 0;
}else{
currentScreen++;
}
}else if(state == DOWN){
if(currentScreen == 0){
currentScreen = numOfScreens-1;
}else{
currentScreen--;
}
}
}
void renderScreen(){
lcd.clear();
if(currentScreen == 0){
lcd.print(screens[currentScreen][0]);
lcd.setCursor(0,1); // second row.
lcd.print(screens[currentScreen][1]);
lcd.setCursor(0,2); // second row.
lcd.print(screens[currentScreen][2]);
lcd.setCursor(0,3); // second row.
lcd.print(screens[currentScreen][3]);
}else{
lcd.print(screens[currentScreen][0]);
lcd.setCursor(0,1); // second row.
lcd.print(screens[currentScreen][1]);
if(setButtonState == 1){
lcd.setCursor(17,3); // second row.
lcd.print("SET");
}
}
}
void setButtonClicked(){
if(currentScreen != 0){
if(setButtonState == 1){
setButtonState = 0;
}else{
setButtonState = 1;
}
}
}
void setup() {
lcd.init();
lcd.backlight();
pinMode(LEDPIN, OUTPUT);
for(int i=0;i<numOfButtons;i++){
pinMode(buttonPins[i], INPUT_PULLUP);
digitalWrite(buttonPins[i], HIGH);
}
Serial.begin(9600);
}
bool ButtonClicked(int id){
return (digitalRead(id) == BUTTONCLICKED);
}
void blinkLed(int id){
digitalWrite(id, HIGH);
Serial.println("LED is on");
delay(300);
digitalWrite(id, LOW);
Serial.println("LED is off");
}
void loop(){
renderScreen();
if(ButtonClicked(UPBUTTON)){
blinkLed(LEDPIN);
Serial.println("UP-Button Clicked!");
if(setButtonState == 0){
screenChanger(UP);
}else{
//value goes up.
}
}
if(ButtonClicked(DOWNBUTTON)){
blinkLed(LEDPIN);
Serial.println("DOWN-Button Clicked!");
if(setButtonState == 0){
screenChanger(DOWN);
}else{
//value goes up.
}
}
if(ButtonClicked(ONOFFBUTTON)){
blinkLed(LEDPIN);
Serial.println("ONOFF-Button Clicked!");
currentScreen = 0;
setButtonState = 0;
}
if(ButtonClicked(SETBUTTON)){
blinkLed(LEDPIN);
Serial.println("SET-Button Clicked!");
setButtonClicked();
}
}
I think its because i clear the screen every time in the loop, I can't find a solution how to fix it without harm make the button work without a delay.
Thanks!