Arduino LCD contrast changes / flickers with different input.

I'm building a Arduino project that uses buttons / switches to control a relay. This will be used to control a piece of large 1m+ wide laminator. Due to the way the machine is used, we need two modes ‘Laminate Mode' which needs a button for ‘start’ and another button for ‘stop’. We also need a 'Mount Mode' in which the start / stop is controlled from a single foot pedal. It also needs a LCD to display status.

I've got a working prototype which uses the attached code, but the contrast in the LCD (I2C) changes depending on the mode in the code.

In // Laminate 'Run' the contrast is good (this is the first option) but in // Mount 'Stop' the LCD is very dark and the type flickers (this is the last option in the code). The middle trio options also effect the LCD.

Does anyone have any idea what could be causing this?
-Any advice would be greatly appreciated.

Laminator_Control_v5.ino.ino (2.47 KB)

I would monitor the supply voltage as you run the code to see if the supply voltage fluctuates.

If you post your code as described in the how to use this forum stickies more members will be able to see it and offer help.

Thank you for the feedback. Sorry I was not sure how to add the code but should have read the stickies to learn before posting. The code I'm using follows.

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

// LCD 2ic definitions

#define I2C_ADDR    0x3F // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);




int buttonPin1 = 10; //Start button 
int buttonPin2 = 11; //Stop button
int buttonPin3 = 12; //Mode swictch
int buttonPin4 = 13; //Foot Pedel
int ledPin = 8; 
int buttonStatus1 = 0; 
int buttonStatus2 = 0;
int buttonStatus3 = 0;
int buttonStatus4 = 0;


void setup() { 
Serial.begin (9600);
lcd.begin(16,2);//Defining 16 columns and 2 rows of lcd display
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);

lcd.setCursor(0,0); 
lcd.print(" WAITING...");
lcd.setCursor(0,1);
lcd.print(" STATUS: STOPPED");;

pinMode(ledPin, OUTPUT); 
pinMode(buttonPin1, INPUT); 
pinMode(buttonPin2, INPUT); 
pinMode(buttonPin3, INPUT); 
pinMode(buttonPin4, INPUT); 

}

void loop() { 
buttonStatus1 = digitalRead(buttonPin1); 
buttonStatus2 = digitalRead(buttonPin2);
buttonStatus3 = digitalRead(buttonPin3);
buttonStatus4 = digitalRead(buttonPin4);

//Laminate Mode

//Laminate Mode Run

//Check(==) if the first button(START) is HIGH, AND(&&) the second button (STOP) is LOW, if yes turn the LED on. 

if (buttonStatus1 == HIGH && buttonStatus2 == LOW && buttonStatus3 == HIGH) 
{ digitalWrite(ledPin, HIGH); 
lcd.clear();//Clean the screen
lcd.setCursor(0,0); 
lcd.print(" LAMINATE MODE");
lcd.setCursor(0,1);
lcd.print(" STATUS: RUNNING");}

//Laminate Mode Stop

//Check(==) if the first button (START) is LOW, AND(&&) the second button (STOP is HIGH, if yes turn the LED off. 

if (buttonStatus1 == LOW && buttonStatus2 == HIGH && buttonStatus3 == HIGH)
{digitalWrite (ledPin, LOW);
lcd.clear();//Clean the screen
lcd.setCursor(0,0); 
lcd.print(" LAMINATE MODE");
lcd.setCursor(0,1);
lcd.print(" STATUS: STOPPED");} 


//Mount Mode

//Mount Mode Run

if (buttonStatus4 == HIGH && buttonStatus3 == LOW) 
{ digitalWrite(ledPin, HIGH); 
lcd.clear();//Clean the screen
lcd.setCursor(0,0); 
lcd.print(" MOUNT MODE");
lcd.setCursor(0,1);
lcd.print(" STATUS: RUNNING");}

//Mount Mode Stop

if (buttonStatus4 == LOW && buttonStatus3 == LOW)
{digitalWrite (ledPin, LOW);
lcd.clear();//Clean the screen
lcd.setCursor(0,0); 
lcd.print(" MOUNT MODE");
lcd.setCursor(0,1);
lcd.print(" STATUS: STOPPED");} 


}

LondonStudio:
Does anyone have any idea what could be causing this?

As always, bad coding.

But then we knew that and I fancy you did too. :astonished:

The loop() runs extremely fast - in case you did not realise that. :grinning: Your code checks input status with every cycle - as indeed it should, but then it mucks about with the display on every cycle also at a ridiculously rapid rate.

Game rules:

  • lcd.clear(); should probably not occur in loop() - it wipes the screen, also known as severe flickering! (It is very slow!)
  • Do not refresh the screen until and unless you want to show something different from the previous.
  • Rewrite only the area needed.

I see you have no "delay()"s. Good! Keep it that way! :roll_eyes:

Thank you for the feedback, removing the lcd.clear(); from the loop solved the problem.

LondonStudio:
removing the lcd.clear(); from the loop solved the problem.

Almost ... but you really need to implement the other changes. :sunglasses: