Without delay timer

I have written this so i can dimm the backlight of the lcd since i dont need to see it all the time,
and i have some questions.

  1. Will the delay i use here to avoid double clicking interfere with other parts of the program?

  2. I want to make it so that it automaticly turns off after being idle for 5 minutes preferably i want this to happen if i have not pushed any buttons on the ir remote i am attaching within the time limit.

  3. Any suggestions as to how to make this better?

knapp is the digital inputpin: // this is temporary to test since ir is not yet implemented
lcdTemp is a temporary integer to hold the value

void lcdonoff(){
    int tod = digitalRead(knapp);
      if(lcdTemp == 0){
        if(tod == 1){
          lcd.setBacklight(HIGH);
          lcdTemp = 1;
          delay(500);
        }
      }
      else if(lcdTemp == 1){
        if(tod == 1){
          lcd.setBacklight(LOW);
          lcdTemp = 0;
          delay(500);
        }
      }
}

Use the arduino built in millis function.

It counts the time since the sketch start-up, so you can use that, for example, to disable the back-light in 5 minutes from now, or better yet, if you press a button if will give another 5 minutes.

The resolution is 4 micros or 1 milli, depending which one you use ( micros() or millis() );

here is the whole code this faar, kinda learning things step by step on this project.

// Libraries included in design
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS3232RTC.h>    
#include <Time.h>  
#include "DHT.h"
#include <IRremote.h>

// IR Remote Buttons
#define button1 16724175    
#define button2 16718055
#define button3 16743045
#define button4 16716015
#define button5 16726215
#define button6 16734885
#define button7 16728765
#define button8 16730805

#define buttonminus 16769055   
#define buttonplus 16754775

int RECV_PIN = 11; 
IRrecv irrecv(RECV_PIN);
decode_results results;
long IReceived = 0 ; 

// LCD pin Setup and Address
#define I2C_ADDR    0x27  // Define I2C Address to the LCD
#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);

// DHT22 Type sensor Temperature and Moisture
#define DHTTYPE DHT22
// DHT22 Pinnumber
#define DHTPIN1 7
#define DHTPIN2 8
float humidity1;
float temperature1;
float humidity2;
float temperature2;
float timer;

// Initialize Sensor DHT11
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);

// Bus Temperature probes
#define ONE_WIRE_BUS 4
#define TEMPERATURE_PRECISION 11  // Precision down to 0.01 degrees C
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

const int knapp = 13;
int lcdTemp = 0;

void setup()
{
   pinMode(knapp, INPUT);
  Serial.begin(57600);
   setSyncProvider(RTC.get); 
   dht1.begin(); 
   dht2.begin();
   Wire.begin();
   irrecv.enableIRIn();
   lcd.begin (20,4);
  
  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(LOW);

}
 


void sensorRead()
{
  humidity1 = dht1.readHumidity();
  humidity2 = dht2.readHumidity();
  // Read temperature as Celsius
  temperature1 = dht1.readTemperature(); 
  temperature2 = dht2.readTemperature(); 
}

void printDigits(int digits)
{
    lcd.print(':');
    if(digits < 10)
        lcd.print('0');
    lcd.print(digits);
}

void digitalClockDisplay(void)
{
    lcd.home ();
    lcd.print(day());
    lcd.print('.');
    lcd.print(month());
    lcd.print('.');
    lcd.print(year()); 
    lcd.print(' ');
    lcd.print(hour());
    printDigits(minute());
    printDigits(second());

}

void Fukt1(){
    lcd.setCursor ( 0, 1 );
    lcd.print("T1:");
    lcd.print(temperature1);
    lcd.print(" F1:");
    lcd.print(humidity1);
}

void Fukt2(){
    lcd.setCursor ( 0, 2 );
    lcd.print("T2:");
    lcd.print(temperature2);
    lcd.print(" F2:");
    lcd.print(humidity2);
}

void lcdonoff(){
    int tod = digitalRead(knapp);
      if(lcdTemp == 0){
        if(tod == 1){
          lcd.setBacklight(HIGH);
          lcdTemp = 1;
          delay(500);
        }
      }
      else if(lcdTemp == 1){
        if(tod == 1){
          lcd.setBacklight(LOW);
          lcdTemp = 0;
          delay(500);
        }
      }
}


    
//int ti = 0;  
  
void loop()
{
  if (irrecv.decode(&results)) {
    IReceived = results.value;        
    irrecv.resume();
  }

  if (timer > 50){
     sensorRead();
     timer = 0;
   }
  timer++;
  
    digitalClockDisplay();
    Fukt1(); 
    Fukt2(); 
    lcdonoff();

}

For an alternative to delay() look at how millis() is used to manage time in the demo several things at a time.

...R