This is post about Arduino and how to turn on the backlight of the lcd display by pressing a button and how to make that the display backlight will turn off with delay.
Hardware that I used
- Arduino Uno
- 16×2 LCD display HD44780
- Pushbutton
- 10k ohm resistor
- 4k7 ohm potentiometer
Circuit
Image was made using Fritzing.
![Arduino circuit lcd button](https://blog.bozjatorium.com/wp-content/uploads/2012/12/circuit_lcd_button.jpg?w=614)
Program
[sourcecode language=”text”]
/*
LiquidCrystalDisplay_Button
Turn on the backlight of a display for one minute by pressing the button.
Created 28 Dec 2012
by TheUzo007.wordpress.com
*/
#include <LiquidCrystal.h>
// Set pins.
#define BUTTON_PIN A5 // The number of the push-button pin.
#define LCD_LIGHT_PIN A4 // The number of the pin where anode of the display backlight is.
#define LCD_LIGHT_ON_TIME 60000 // How long (in milliseconds) should lcd light stay on?
unsigned int currentLcdLightOnTime = 0;
// For calculating the lcd light on time.
unsigned long lcdLightOn_StartMillis;
boolean isLcdLightOn;
// For checking push-button state.
int buttonState = 0;
// Initialize the LiquidCrystal library with the numbers of the interface pins.
// LiquidCrystal(rs, e, d4, d5, d6, d7)
LiquidCrystal lcd(8, 9, 5, 6, 7, 3);
void setup(){
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// Set the lcd number of columns and rows.
lcd.begin(16, 2);
// Print the message to the lcd.
lcd.setCursor(0, 0); // First row.
lcd.print("Test lcd light.");
lcd.setCursor(3, 1); // Second row.
lcd.print("TheUzo007");
lcd.display();
// Set the push-button pin as an input.
pinMode(BUTTON_PIN, INPUT);
// Set the lcd display backlight anode pin as an output.
pinMode(LCD_LIGHT_PIN, OUTPUT);
// Set the lcd display backlight anode pin to low – lcd light off.
digitalWrite(LCD_LIGHT_PIN, LOW);
isLcdLightOn = false;
}
void loop(){
// Check the state of the push-button.
buttonState = digitalRead(BUTTON_PIN);
if (buttonState == HIGH){
// Button pressed.
Serial.println("Button pressed – HIGH");
lcdLightOn_StartMillis = millis();
currentLcdLightOnTime = 0;
isLcdLightOn = true;
digitalWrite(LCD_LIGHT_PIN, HIGH);
}
else{
// Button not pressed.
//Serial.println("Button not pressed – LOW");
if(isLcdLightOn){
currentLcdLightOnTime = millis() – lcdLightOn_StartMillis;
if(currentLcdLightOnTime > LCD_LIGHT_ON_TIME){
isLcdLightOn = false;
digitalWrite(LCD_LIGHT_PIN, LOW);
}
}
}
Serial.print("Lcd light on time: ");
Serial.println(currentLcdLightOnTime);
}
[/sourcecode]
So I’ve put together everything and written out the code but I keep getting and error stating “Invalid suffix “_” on integer constant what should I do to fix this
Looks like there’s a problem with constants. I’m guessing that with LCD_LIGHT_ON_TIME. I read somewhere that integer constants default to int so may be the number 60000 is to big for signed int. Try replacing the line #define LCD_LIGHT_ON_TIME 60000 with unsigned int LCD_LIGHT_ON_TIME = 60000; or change number 60000 to number lower then 32000.
If that doesn’t work try replacing all the constants with variables, and see what you get.
I’ve tried everything and I still can’t seem to figure out what the problem is.
I do’t know what else to do. Try deleting “line by line” to see where/what the problem is. Or write the script again gradually, not just copy it all at once, but line by line and test it every time you add something new to the script.