How do i stop the I2C 16x2 LCD backlight flashing In Arduino Loop

Thread Starter

s200bym

Joined Aug 9, 2017
78
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:
C:
#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);

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


    lcd.backlight();
    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);

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


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


  }
}

Mike.
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,835
Don't turn the button off in the loop. Turn it off with a timer interrupt. A button press turns it back on. Button press also resets the timer interrupt..
 

spinnaker

Joined Oct 29, 2009
7,835
No. The timer interrupt turns the light off. A button press turns the light on and resets the timer. It shouldn't matter if the timer has not timed out yet.
 

Thread Starter

s200bym

Joined Aug 9, 2017
78
I managed to figure it out in the end.
C:
#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

int period = 15000;
unsigned long time_now = 0;

// 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.begin(16, 3);
  lcd.setCursor(0, 0);
  lcd.print(" !!!WELCOME!!! ");
  lcd.setCursor(0, 1);
  lcd.print("Valves Resetting ");
  delay(3000);
  lcd.clear();



  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;
      lcd.display();
      lcd.backlight();
    }
    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.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.setCursor(0, 0);
    lcd.print("Valves Closed ");
    lcd.setCursor(0, 1);
    lcd.print("(Quiet Mode) ");


  }

  if (millis() > time_now + period) {
    time_now = millis();
    lcd.noDisplay();
    lcd.noBacklight();
  }

}
Mike.

Moderators note : You can use code=c to annotate the code
 
Last edited by a moderator:
Top