trying to avoid use of delay in Nokia 5110 lcd display- No success-

Dear all,
I am developing a two node water reservoir monitoring system.
After more than one month of trials each node can read the required variables properly and transmit the values to a central module.
This module is responsible for displaying the values of each node and report abnormal, conditions, if they exist.
Up to this point, I have been using delay(nnnn) after the display.display() to allow the proper visualization of the values and messages that were written on the display.
Now I am trying to implement two more functions, to be activated by two pushbuttons:
1 - alarm ON/OFF and 2- acknowledge alarm.
However, since there are a LOT of 4sec delays in the program, I have to keep the buttons pressed for a LOOONG time to allow the program to respond to each press.
Looking up for solutions on the internet I found the use of millis() instead of delay and tried to adapt a snippet of a LED flasher to pause the display, but it only works at the two first instances.
After that, the message on the display is not visible.
I could not find much information about the display.display() command of the Adafruit_PCD8544.h library, and maybe the problem is the.
Here is the shipped of the code I am using to simulate a delay(nnn) command and I have also attached the entire code of this module.
Any suggestions will be greatly appreciated.

// Print a message for debugging purposes
 display.setCursor(0,0);
 display.println("Init Display"); 
 display.println("receptor rev1C");
 display.println("07/01/2020 --00");
 display.display();
   if ( (millis() - displayTime) > display_on_time){ 
    display.clearDisplay(); 
    displayTime = millis();   // restart the timer
  }
//???? delay(2000);

Later I thought that I should initialize the display timer each time the display.display() command was executed, and modified the code to this one, but it does not work also.

  display.display();
  currentLCDMillis = millis();    //-------------checking this idea----------
    // save the last time you changed the display
  if(currentLCDMillis - previousLCDMillis <= lcdInterval) { display.display();}            
 else { previousLCDMillis = currentLCDMillis;
   display.clearDisplay();
  }

Receptor_casa_rev1C.ino (13.4 KB)

Here are a few recommendations:

Remove all delays in your program, they are a waste of CPU cycles.

Use Tools -> Auto Format to indent your code and make it more readable. This can be formated for your taste. Google the forum for "formatter.conf" and check this page

http://astyle.sourceforge.net/astyle.html

Your program should run through loop as fast and often as possible. Make your task as small as possible and do one thing at a time. Try to avoid while loops, they are unpredictable as well.

For the buttons you can read them every time you run through loop and set flags. Then you can control the functions that should be activated separately.

Give your functions better names and simplify the functionality. e.g. what does Buzzer_and_Leds() do?

Try to avoid global variables. You can use them but try to limit them.

Try to use a naming scheme consistently. e.g. pump_On_Pin and buzzerLedPin. PUMP_ON_PIN its a constant value that does not change. Many people use all capital and underscore for that. e.g. HIGH, OUTPUT

Try to limit the millis() technique in the main loop only. Each function should just do what it needs to do and not check the time. Then you can see in the main loop how the time is managed.

Hi,
Thank you for your suggestions.
I am trying to learn this “new” programming language and its best practices.
I tried to comment the code to the best of my habilities, but my previous experience was with Fortran IV and big mainframes like IBM /370 or Borrough 6700.
These were very powerfull machines
From the late 80’s...

You might treat the screens in a round-robin, millis()-timed manner and give priority to error codes.

The attached is a zero-order attempt at that. It compiles but isn’t tested so YMMV.

Receptor_casa_rev1C.ino (15.5 KB)

I suspect that a display_on_time with a value of zero is not really useful :wink:

In setup(), it's acceptable to use delay(). Your current setup() code will probably clear the display within a millisecond.

Thank you all for comments.
Specially sterretje who has noticed that in this version of the code, the display_on_time value is 0!!!
I have tried so many codes with different the variable names for the same function that I have lost track of them.
I will correct that, but I suspect that is not the problem since I have a latter revision of the code where this value is defined as 4000ms and the behavior is not the expected.