BackLight Delay on I2C LCD using buttons Help

Hi everyone

Im working on a project and I want to have the backlight off all the time then when one of the 3 buttons is pressed the backlight comes on for 5 secs then goes off again. Ive been researching how to do this but im new to arduino and im not sure how to go about it. I think I need a loop code of lcd.backlight and lcd.nobacklight etc etc. Could someone help me find a simple way to do this on an I2C controlled LCD.
PS the three buttons being set, mod and cge
Thanks

#include <Wire.h>
#include<EEPROM.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
int buttonState = 0;

#define I2C_ADDR 0x27 // <<- Add your address here.
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

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

RTC_DS1307 RTC;
int tmp, Inc, hor, mIn, add = 11;
int set = 8;
int cge = 9;
int mod = 10;
int off = 0;
#define buz 11
int Hor, Min, Sec;
///////////////////////////////////////Function to adjust the time//////////////////////////////////
void time()
{
  int tmp = 1, mins = 0, hors = 0, secs = 0;
  while (tmp == 1)
  {
    off = 0;
    if (digitalRead(cge) == 0)
    {
      Hor++;
      if (Hor == 24)
      {
        Hor = 0;
      }
    }
    lcd.clear();
    lcd.begin(16, 2);
    lcd.setCursor(0, 0);
    lcd.print("Set Alarm Time ");

    lcd.setCursor(0, 1);
    if (Hor <= 9)
      lcd.print("0");
    lcd.print(Hor);
    lcd.print(":");
    lcd.print(Min);
    lcd.print(":");
    lcd.print(Sec);
    delay(200);
    lcd.setCursor(0, 1);
    lcd.print("  ");
    lcd.print(":");
    lcd.print(Min);
    lcd.print(":");
    lcd.print(Sec);
    delay(200);
    if (digitalRead(set) == 0)
    {
      hor = Hor;
      EEPROM.write(add++, hor);
      tmp = 2;
      while (digitalRead(set) == 0);
    }
  }

  while (tmp == 2)
  {
    if (digitalRead(cge) == 0)
    {
      Min++;
      if (Min == 60)
      {
        Min = 0;
      }
    }
    lcd.setCursor(0, 1);
    lcd.print(Hor);
    lcd.print(":");
    if (Min <= 9)
      lcd.print("0");
    lcd.print(Min);
    lcd.print(":");
    lcd.print(Sec);
    lcd.print("  ");
    delay(200);
    lcd.setCursor(0, 1);
    lcd.print(Hor);
    lcd.print(":");
    lcd.print("  ");
    lcd.print(":");
    lcd.print(Sec);
    lcd.print("  ");
    delay(200);
    if (digitalRead(set) == 0)
    {
      mIn = Min;
      EEPROM.write(add++, mIn);
      tmp = 0;
      while (digitalRead(set) == 0);
    }
  }
  off = 1;
  delay(10);
}
///////////////////////////////////////////function to sound the buzzer//////////////////////////////////
void Buz()
{
  if (digitalRead(set) == 0)
    off = 0;
  if (off == 1)
  {
    digitalWrite(buz, HIGH);
    delay(500);

    digitalWrite(buz, LOW);
    delay(500);
  }
}
//////////////////////////////////////////function to compare the alarm time with current RTC time//////////////////
void TimeCheck()
{
  int tem[17];
  for (int i = 11; i < 17; i++)
  {
    tem[i] = EEPROM.read(i);
  }
  if (Hor == tem[11] && Min == tem[12] && off == 1)
  {
    add = 11;
    Buz();
    Buz();
    lcd.clear();
    lcd.print("alarm...........");
    lcd.setCursor(0, 1);
    lcd.print("...........alarm");
    Buz();
    Buz();
  }
}
////////////////////////////////////////////////////////////setup///////////////////////////
void setup()
{
  Wire.begin();
  RTC.begin();
  lcd.begin(16, 2);
  pinMode(cge, INPUT);
  pinMode(set, INPUT);
  pinMode(mod, INPUT);
  pinMode(buz, OUTPUT);
  digitalWrite(set, HIGH);
  digitalWrite(mod, HIGH);
  digitalWrite(cge, HIGH);

  lcd.setCursor(0, 0);
  lcd.print("THOMAS'S AWESOME");
  lcd.setCursor(0, 1);
  lcd.print("  ALARM CLOCK  ");
  delay(2000);

  if (!RTC.isrunning())
  {
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}
////////////////////////////////////////////////////////////loop/////////////////////////////////////
void loop()
{
  buttonState = digitalRead(set)
;  if (buttonState == HIGH)
  lcd.backlight();
  delay(5000);
  lcd.noBacklight();
}
{
  buttonState = digitalRead(mod)
;  if (buttonState == HIGH)
  lcd.backlight();
  delay(5000);
  lcd.noBacklight();
  }
  {
  buttonState = digitalRead(cge)
 ; if (buttonState == HIGH)
  lcd.backlight();
  delay(5000);
  lcd.noBacklight();


{
  DateTime now = RTC.now();
  if (digitalRead(mod) == 0)
  {
    current();
    time();
    delay(1000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("  Alarm On");
    delay(2000);
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Time:");
  lcd.setCursor(6, 0);
  Hor = now.hour(), DEC;
  if (Hor <= 9)
  {
    lcd.print("0");
    lcd.print(Hor = now.hour(), DEC);
  }
  else
    lcd.print(Hor = now.hour(), DEC);
  lcd.print(":");
  Min = now.minute(), DEC;
  if (Min <= 9)
  {
    lcd.print("0");
    lcd.print(Min = now.minute(), DEC);
  }
  else
    lcd.print(Min = now.minute(), DEC);

  lcd.print(":");
  Sec = now.second(), DEC;
  if (Sec <= 9)
  {
    lcd.print("0");
    lcd.print(Sec = now.second(), DEC);
  }
  else
    lcd.print(Sec = now.second(), DEC);
  lcd.setCursor(0, 1);
  lcd.print("Date: ");
  lcd.print(now.day(), DEC);
  lcd.print("/");
  lcd.print(now.month(), DEC);
  lcd.print("/");
  lcd.print(now.year(), DEC);
  TimeCheck();
  delay(200);
}
///////////////////////////////////////////////////////function to get current RTC time//////////////////////////////
void current()
{
  lcd.setCursor(0, 1);
  lcd.print(Hor);
  lcd.print(":");
  lcd.print(Min);
  lcd.print(":");
  lcd.print(Sec);
}

You need to look into using millis() for timing as in the BlinkWithoutDelay example and Several things at the same time Save the time an event happens, such as the backlight coming on triggered by a button press, then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action.

Cheers helibob but I understand the logic required but I dont know how it would be structured as in the language. How would I go about that! Hope that doesnt sound rude haha

Have a look at the example and link that I gave in my previous post.

Im not trying to do multiple things st the same time though. Just switch on a lcd backlight for 5 secs then off again if a button is pressed. Im not doing or comparing anything at the same time. I dont see how that link you sent applies. Could you elaborate

You are timing 5 seconds (until its time for the light to go off) and checking for button presses. That's two things at the same time. So the example code is relevant. If you used delay(5000) to do the timing, you would miss any button presses during that time. Hope that helps.

Paul

togbourne: Im not trying to do multiple things st the same time though. Just switch on a lcd backlight for 5 secs then off again if a button is pressed. Im not doing or comparing anything at the same time. I dont see how that link you sent applies. Could you elaborate

The strategy used there applies to anything you want to time. The fact that the particular example there is about many things at once is not germane to the subject at hand. See the Blink Without Delay example. It uses the same idea but to do only one thing. Instead of looking for code to copy, read what's there ( it should take a good bit longer than 4 minutes) and try to understand how it works. Once you do you will see how it applies to your situation.

Im not trying to do multiple things st the same time

Yes you are. You want to know when 5 seconds has elapsed (thing 1) whilst reading the input from buttons (thing 2)

Ok guys ill take a look thanks for your help :)

UKHeliBob: Yes you are. You want to know when 5 seconds has elapsed (thing 1) whilst reading the input from buttons (thing 2)

Was the way i said it less easy to understand?

Was the way i said it less easy to understand?

Not at all. I just expressed the same thing in my own words.

One of the problems on the forum is that we have no idea of the background, experience or even sometimes the native language of posters so giving the same advice in more than one way can be helpful. Sorry if I have offended you in doing that.

UKHeliBob: Sorry if I have offended you in doing that.

Not offended. Just concerned you (a fellow Brit) thought i worded it in a strange or ambiguous way or something...