Arduino LED takes time to turn on after using push button as a toggle switch

I'm creating a temperature control simulation and there is a LED used to represent the light bulb which is turned on via the toggle switch. I have successfully included state change detection and tried it on a Arduino board where the LED turns on with no problem as the Push button functions as a toggle switch.

Once I move this set of code of the push button into another board with other components included, the LED takes awhile to turn on after clicking the push button, sometimes I need to hold onto the push button around 5 seconds for it to turn on however it was working fine on it's own. It is probably due to all the other code from the other components. Is there a way to get the push button and led to work same as it did on it's own Arduino board.

The part of the code I am talking about is the //pushbutton toggle switch code, I've moved this set of code at different places within the void loop and the problem still persists.

SOLVED!

i ran you code.

i think you need to configure the buttonPin as INPUT_PULLUP.

Thanks for the suggestion but I just tried it and there still is a delay, its only slightly improved.

i suggest to print millis() at various points in you code to determine how long it's taking, along with the state of the button press. make sure you print a timestamp at the very beginning and very end of loop

do you mean to use millis function to determine when the LED turns on after the button is pressed?

no, not just after the button is pressed. at various locations in the code.

sometimes I need to hold onto the push button around 5 seconds for it to turn on

this way you can confirm where the cause of the delay is.

you might also print the button state to confirm the code sees that it is pressed

when you have these so called "bizarre" problems, your need to track things down. it's often something simple

You're not doing yourself any favors with a 9600 baud speed on serial, although that's unlikely to be your problem with a 5 second delay.

Adding some timing as advised above is indeed the next step. I'd be particularly interested in how long this takes:

irrecv.decode