Go Down

Topic: Replacing delay with millis (Read 1 time) previous topic - next topic


Hello Friends!

I am using below sketch to scroll the test on LCD.  It works fine with delay function, but I want to replace delay with millis.  I could do that in the while loop successfully, but I need help in replacing the delay(2000) in for loop, any help is appreciated.

Thanks in advance.

Code: [Select]

#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int numRows = 2;
const int numCols = 16;
void setup()
// set up the LCD's number of columns and rows:
lcd.begin(numCols, numRows);
void loop()
scroll("A message too long to fit !");

void scroll( char *text)
long interval = 500;
long previousMillis = 0;
long previousMillis1 = 0;
int length = strlen(text); // the number of characters in the text
if(length < numCols)
int pos;
for( pos = 0; pos < numCols; pos++)
delay(2000); // allow time to read the first line before scrolling
/*unsigned long currentMillis1 = millis();   // need help here, its not working.
if (currentMillis1 - previousMillis1 >4*interval)
     previousMillis1 = currentMillis1;

while(pos <= length - numCols)
for( int i=0; i < numCols; i++)
//pos = pos + 1;
unsigned currentMillis = millis();
if (currentMillis - previousMillis > interval)
   pos = pos + 1;
   previousMillis = currentMillis;


hi lavan!

its a standard problem. just have a look at the blink without delay example here on the arduino site:


its a quite simple idea, to use millis() instead of delay().

hope i could help?


Those variables that must hold the value of millis() should be unsigned long.
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts


I appreciate that you are trying to help me, but I believe you did not look at the code I posted.  I am already using millis in my code its working fine.  But I am not able to use it repeatedly.  In this example, I need to call delay of 2000 ms in for loop and again 300 ms while loop.  I have successfully replaced the delay with millis in while loop, what I am looking is some guidance on how to deal the same in the forloop to replace the delay of 2000 ms with millis.


I am already using millis in my code its working fine.

No it is not because as you said:-
But I am not able to use it repeatedly.

This would not be an issue if you used the millis timer correctly.
Do not use it as an absolute timer but a relative one.

You set the value of previousMillis to zero each time you enter the scroll function. What you need to do is use the technique of the blink without delay as you have been told. The way you are using millis is no better than using a delay call.
Make your functions such that you call them and they instantly return if the time to do things is not yet. Make your previousMillis variables static long so you do not keep resetting them.


Thank you Mike!... I appreciate your guidance


I wrote a demo sketch that uses millis() to control several things. You may find it useful.


Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up