How to remove the delays from this LCD function?

#include "Arduino.h"
#include "DFR0554.h"

void lcd();

DFR0554 dfr0554 = DFR0554();

void setup() {
    Serial.begin(115200);
    dfr0554.begin(&Wire);

  //  dfr0554.setCustomSymbol(CUSTOM_SYMBOL_1, customCharHeart);

    dfr0554.turnOn();
    dfr0554.setLdrStateAll(LDR_STATE_IND_GRP);
    dfr0554.setGroupControlMode(GROUP_CONTROL_MODE_DIMMING);
    dfr0554.setRGB(255, 255, 255);
}

void loop() {
  int sensorValue0 = analogRead(A0);
  int sensorValue1 = analogRead(A1);
  int sensorValue2 = analogRead(A2);
  int sensorValue3 = analogRead(A3);

  Serial.print("A0 value: ");
  Serial.print(sensorValue0);
  
  Serial.print("  A1 value: ");
  Serial.print(sensorValue1);
  
  Serial.print("  A2 value: ");
  Serial.print(sensorValue2);
  
  Serial.print("  A3 value: ");
  Serial.println(sensorValue3);
  
  dfr0554.clear();
  lcd();
}

void lcd() {

    dfr0554.clear();
    dfr0554.setCursorPosition(0, COL_COUNT);
    dfr0554.setAutoScrollEnabled(true);

    String longLine = "This is a long string!";
    for (int i = 0; i < longLine.length(); i++) {
        dfr0554.print(longLine.charAt(i));
        delay(205);
    }
 
    dfr0554.setAutoScrollEnabled(false);
    delay(2000);
  
    // progress bar
    dfr0554.setProgressBarEnabled(true);
    dfr0554.setCursorPosition(0, 0);
    dfr0554.print("Timer: ");

    for (int j = 0; j <= 10; j++) {

        dfr0554.setCursorPosition(0, 10);

            char string_rep[6];
            sprintf(string_rep, "%d s", j);
            dfr0554.print(string_rep);

        dfr0554.setProgress(j*10);
        delay(1000); 
    }
    dfr0554.setProgressBarEnabled(false);
    dfr0554.clear();
}

I have the above sketch which prints to the DF Robot I2C LCD and also sends multi ADC values over serial.

The function is currently a demo and the printed text will be changed at a later date, the ADC values will be used later on in the code, they will not be printed to the LCD.

The LCD function is an example from DF Robot but it uses multiple delay functions, I need to remove these so the rest of the code works as intended, I have tried experimenting with millis and if statements as found in the blinkWithoutDelay example but I am not sure where to put them so it is not working.

Currently the delays are crucial to the timing of the scrolling text, the progress bar and the delay between the scrolling text and the progress bar. What is the best way to remove the delays in the above code?

Unfortunately, the required changes are a complete rewrite of the program usin a different strategy to buffer and display the text.

The ‘easiest’ way is to use a different platform that allows multi-threading, but the most direct way is noted above.

QVC

It's not a perfect translation (and may be full of bugs; I didn't even try to compile it) but you might get some ideas from this:

void lcd() 
{
    char string_rep[6];
    static String longLine = "This is a long string!";
    static uint8_t
        i, j,
        state = 0;
    static uint32_t
        timeLCD = 0ul;
    uint32_t
        timeNow = millis();

    switch( state )
    {
        case    0:
            /*
                dfr0554.clear();
                dfr0554.setCursorPosition(0, COL_COUNT);
                dfr0554.setAutoScrollEnabled(true);
    
                String longLine = "This is a long string!";
             */
            dfr0554.clear();
            dfr0554.setCursorPosition(0, COL_COUNT);
            dfr0554.setAutoScrollEnabled(true);
            //String longLine = "This is a long string!";           
            
            i = 0;
            timeLCD = 0ul;
            state++;
            
        break;

        case    1:
            /*
                for (int i = 0; i < longLine.length(); i++) {
                    dfr0554.print(longLine.charAt(i));
                    delay(205);
                }
             */
            if( i < longLine.length() )
            {
                if( (timeNow - timeLCD) >= 205ul )
                {
                    timeLCD = timeNow;
                    dfr0554.print(longLine.charAt(i));
                    i++;
                    
                }//if
                
            }//if
            else
            {
                /*
                 *  dfr0554.setAutoScrollEnabled(false);
                 */
                dfr0554.setAutoScrollEnabled(false);
                
                timeLCD = timeNow;
                state++;
                
            }//else
            
        break;

        case    2:
            /*
                delay(2000);
             */
            if( (timeNow - timeLCD) > 2000ul )
            {
                /*
                    // progress bar
                    dfr0554.setProgressBarEnabled(true);
                    dfr0554.setCursorPosition(0, 0);
                    dfr0554.print("Timer: ");
                 */                
                dfr0554.setProgressBarEnabled(true);
                dfr0554.setCursorPosition(0, 0);
                dfr0554.print("Timer: ");

                timeNow = 0ul;
                j=0;
                state++;
                
            }//if

        break;

        case    3:
            /*
                for (int j = 0; j <= 10; j++) {
                    dfr0554.setCursorPosition(0, 10);
                        char string_rep[6];
                        sprintf(string_rep, "%d s", j);
                        dfr0554.print(string_rep);

                        dfr0554.setProgress(j*10);
                        delay(1000); 
                }
             */
            if( j != 11 )
            {
                if( (timeNow - timeLCD) >= 1000ul )
                {
                    dfr0554.setCursorPosition(0, 10);
                    sprintf(string_rep, "%d s", j);
                    dfr0554.print(string_rep);
                    dfr0554.setProgress(j*10);
                    j++;                    
                    
                }//if
                
            }//if
            else
            {
                /*
                    dfr0554.setProgressBarEnabled(false);
                    dfr0554.clear();
                 */                               
                dfr0554.setProgressBarEnabled(false);
                dfr0554.clear();
                
                timeLCD = timeNow;
                state = 0;
                
            }//else
            
        break;
        
    }//switch
    
}//lcd

Note that every pass of loop() you do this:

dfr0554.clear();

so whatever happens in lcd() is immediately erased on the next pass of loop. I assume this isn't your intention.

the basic principle of doing mutliple things at the same time is to execute
a tiny small step from "thing A"
then
a tiny small step from "thing B"
then
a tiny small step from "thing C"
etc.

etc.

the code you have posted is a demo-code that just shows some things on the LCD. Just fr demonstration.

Your real code will do something different.
You should describe in detail what you really want to do.
The only thing you can re-use from the demo-code a single lines like

dfr0554.print(...
dfr0554.setCursorPosition(0, 10);

The rest of it changes anyway. With using delay() and without using delay().

The demo-code has this principle

stay inside loop "A" to print the character of the long string

stay inside loop "B" to count up a timer

doing multiple things like display some text and values
and reading in ADC or button-presses at the same time
is coded with one big loop

this loop is function loop() itself

void loop()

  read ADC-channels
  do something with ADC-values 

  if "printing long string is active"
    print a single character 
    increase index "i"

  if timer is active
    print new value of timer
    increase timervalue

the lines above are repeated ( "looped" ) through function loop() itself

I'm very sure that you can learn a lot from
reading this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.