delay through buttonstaterequest?

How can the following piece delay my code so much…:
I have put together this code, which works, but it is being delayed for some reason, if i comment in the following piece…

//LCD Backlight initialise
// read the state of the pushbutton value:
void doLCDlightbutton () {
buttonState = digitalRead(backlightbuttonpin);
Serial.print ("backlight button status ");
Serial.println ( buttonState );

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) { // turn backlight on:
lcd.init(); // initialize the lcd
lcd.backlight(); // Backlight on

} else {
lcd.init(); // initialize the lcd
lcd.noBacklight(); // turn Backlight off:
}
}

Does anyone see a reason how this can delay the code?
The complete code is attached, thank you

( code is quite long due to the sundown timer / RGB light. )

chicken_coop_2.8.ino (51.7 KB)

I don't see anything particularly egregious in there. It seems odd to run lcd.init every time you call that function though - I'd expect to see it in setup instead. Can you use millis to find out how long that function actually takes?

Also, I don't see a need to set the backlight on or off on every call. Better to store the state of the backlight and only set it if there is a change.

Initially, I wondered whether the Serial.printing was slowing things down, especially as you're using such a low baud rate, but there's plenty of other prints elsewhere in the program so that's probably not relevant.

Karma++ for elegant use of "eschew egregious ".

:slight_smile:

Seems the word used to mean the exact opposite...

egregious.PNG

egregious.PNG

wildbill:
I don't see anything particularly egregious in there. It seems odd to run lcd.init every time you call that function though - I'd expect to see it in setup instead. Can you use millis to find out how long that function actually takes?

Also, I don't see a need to set the backlight on or off on every call. Better to store the state of the backlight and only set it if there is a change.

Initially, I wondered whether the Serial.printing was slowing things down, especially as you're using such a low baud rate, but there's plenty of other prints elsewhere in the program so that's probably not relevant.

Thanks for looking. Reaon i want to have the backlight off is because my chicken may find it annoying.
You are correct, i can run the lcd.init much less often, but i am still getting the hang of it

I have not gotten the hang of the millis yet.

There are are lot of prints in the program, but it is really this piece that is slowing it down, even that i would like to switch it off to make sure the reed switches top and bottom for the door control stay working.

lcd.init() should only be called once in setup.