Logic needed for temporarily displaying an alternate message on LCD display

Hi all,

I’m working on a stairlight project. It’s going relatively fine but I’m bumping into a logical issue at this time. Hoping to get some assistance here.

So here it is:

To add some extra ‘bling’, I wanted to add an LCD display to the project.
In normal operation, it is displaying the value of an LDR sensor (to visualise the cut-off value between night & day used to make the pretty lights come on)
The LCD ‘refresh’ is managed by a variable.
I also added a tactile switch to be able to switch between different ledstrip modes. This is handled by an interrupt routine.

What I’m aiming for now is that, whenever the tact switch is pressed, the LED mode that is selected appears for about 2 seconds and then the LCD display reverts back to displaying the LDR sensor value.

It’s kind of working. Displaying the sensor value is working fine and whenever I press the tactswitch, I briefly get to see the LED mode but this depends heavily on the time that is left from the ‘pause’ used to refresh the screen (as you will see in the code).

I’m new to Arduino and C++ so please forgive me if the code is messy or SDC.
Code provided is also a snippet from a larger project (I’m reworking it) but I believe all the necessary lines are there.
If more is needed, please let me know.
Other remarks on how to clean up or write better code are also welcome.


codesnippet.ino (4.07 KB)

What's the motivation behind using an interrupt for the pushbutton? That sort of thing is usually handled by writing code somewhere in loop().

lcd.setCursor(0,0) is made redundant by lcd.clear().

There is almost certainly no need to use an interrupt to read the switch as has been pointed out. The loop() function should repeat fast enough to read the input and allow a response in a short time.

It sounds like your program has 2 display modes, LDR mode and LED mode. Declare a boolean variable, lets call it ldrMode. When it is true, which will be most of the time, display the LDR value, but only if it changes. When the button becomes pressed, detected in loop(), set ldrMode to false, save the millis() value, change the LED mode and display the appropriate message. Each time through loop() check whether millis() minus the start time is equal to or greater than the required period. If it is then change ldrMode to true and display the LDR value. If not then keep going round loop() until the period ends.

When the button becomes pressed, detected in loop()...

aka state change detection - found in the IDE at: File/examples/02.digital/state change detection & debounce.

Main reason for using an interrupt is the learning experience. See what is does, how it behaves.

Another was “keeping the main loop tidy”.
Most of my code has been used for the mode where I use two PIR sensors to detect someone walking up or down the stairs. That will be one of the selectable modes. I still need to figure out how to put this in a separate function.

Before I rework this modeselect into an “in loop” dectection: is there a reason why using an interrupt for this is ‘not done’?

Thank you for the assistance both dougp and UKHeliBob

What exactly do you do in the ISR ?