How do i stop the 16x2 LCD backlight flashing

Hi All.

How would I go about stopping the backlight flashing?

I want the backlight to come on every time the button is pushed to show the status and then go off after a set time without delaying the whole loop.

Code:

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

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);




const int  buttonPin = 2;    // The pin that the pushbutton is attached to
const int valvepin = 1;      // The pin that the valves are attached to



// Variables will change:
int buttonState;         // Current state of the button
int lastButtonState;     // Previous state of the button
bool valveStatus;





void setup()
{
  Serial.begin(9600);          //  Setup serial
  // Initialize the button pin as a input with pullup, active low
  pinMode(buttonPin, INPUT_PULLUP);
  //Initialize button states
  buttonState = digitalRead(buttonPin);
  lastButtonState = buttonState;


  lcd.backlight();


  lcd.begin(16, 3);
  lcd.setCursor(0, 0);
  lcd.print(" !!!WELCOME!!! ");
  lcd.setCursor(0, 1);
  lcd.print("Valves Resetting ");
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Valves Closed ");
  lcd.setCursor(0, 1);
  lcd.print("Quiet Mode");
  lcd.noBacklight();


  Serial.println("Exhaust Valves ");

}

void loop()
{
  // Read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // Compare the buttonState to its previous state
  if (buttonState != lastButtonState) //Changed
  {
    if (buttonState == LOW) //New press, so change valve flag
    {
      valveStatus = !valveStatus;
    }
    delay(50);
  }

  lastButtonState = buttonState; // Save the current state as the last state, for next time through the loop


  if (valveStatus) //Positions the valve
  {
    Serial.write(0x56);
    Serial.write(0x31);
    Serial.write(0x0A);
    delay(500);

    Serial.print("Valve Status: ");
    Serial.print(" Valve Open");
    Serial.println(" (Loud Mode)");


    lcd.backlight();
    lcd.begin(16, 3);
    lcd.setCursor(0, 0);
    lcd.print("Valves open ");
    lcd.setCursor(0, 1);
    lcd.print("Loud Mode");
    lcd.noBacklight();


  }
  else
  {
    Serial.write(0x56);
    Serial.write(0x30);
    Serial.write(0x0A);
    delay(500);

    Serial.print("Valve Status: ");
    Serial.print(" Valve Closed");
    Serial.println(" (Quiet Mode)");


    lcd.backlight();
    lcd.begin(16, 3);
    lcd.setCursor(0, 0);
    lcd.print("Valves Closed ");
    lcd.setCursor(0, 1);
    lcd.print("Quiet Mode");
    lcd.noBacklight();


  }
}

Mike.

Very muddled code.

lcd.begin should definitely not be in loop().

You need to study how to time things, you should not be using delay().

Study the "blink without delay" topic.

Hi Paul,

Thanks for your reply.

Apologies for the muddled code, I've only just started coding.

Looking at the blink without delay topic, I can't see how I can use that information for this. The LCD is running on the I2C bus so I can't define a pin for the backlight.

OR am I just reading it all wrong?

Mike.

I have managed to find the pin that controls the backlight and now able to keep it on with the following commands. I just need to work out how to turn it off after a period of time.

#define BACKLIGHT_PIN 3

lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);

lcd.setBacklight(HIGH);

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

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);




const int  buttonPin = 2;    // The pin that the pushbutton is attached to
const int valvepin = 1;      // The pin that the valves are attached to



// Variables will change:
int buttonState;         // Current state of the button
int lastButtonState;     // Previous state of the button
bool valveStatus;
#define BACKLIGHT_PIN     3





void setup()
{
  Serial.begin(9600);          //  Setup serial
  // Initialize the button pin as a input with pullup, active low
  pinMode(buttonPin, INPUT_PULLUP);
  //Initialize button states
  buttonState = digitalRead(buttonPin);
  lastButtonState = buttonState;


  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);

  lcd.begin(16, 3);
  lcd.setCursor(0, 0);
  lcd.print(" !!!WELCOME!!! ");
  lcd.setCursor(0, 1);
  lcd.print("Valves Resetting ");
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Valves Closed ");
  lcd.setCursor(0, 1);
  lcd.print("Quiet Mode");
  lcd.noBacklight();


  Serial.println("Exhaust Valves ");

}

void loop()
{
  // Read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // Compare the buttonState to its previous state
  if (buttonState != lastButtonState) //Changed
  {
    if (buttonState == LOW) //New press, so change valve flag
    {
      valveStatus = !valveStatus;
    }
    delay(50);
  }

  lastButtonState = buttonState; // Save the current state as the last state, for next time through the loop


  if (valveStatus) //Positions the valve
  {
    Serial.write(0x56);
    Serial.write(0x31);
    Serial.write(0x0A);

    Serial.print("Valve Status: ");
    Serial.print(" Valve Open");
    Serial.println(" (Loud Mode)");


    lcd.setBacklight(HIGH);
    lcd.setCursor(0, 0);
    lcd.print("Valves Open  ");
    lcd.setCursor(0, 1);
    lcd.print("Loud Mode ");



  }
  else
  {
    Serial.write(0x56);
    Serial.write(0x30);
    Serial.write(0x0A);

    Serial.print("Valve Status: ");
    Serial.print(" Valve Closed");
    Serial.println(" (Quiet Mode)");


    lcd.setBacklight(HIGH);
    lcd.setCursor(0, 0);
    lcd.print("Valves Closed ");
    lcd.setCursor(0, 1);
    lcd.print("Quiet Mode ");


  }
}