Go Down

Topic: Replacing delay with millis (Read 462 times) previous topic - next topic

Lavan

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 !");
delay(1000);
lcd.clear();
}


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)
lcd.print(text);
else
{
int pos;
for( pos = 0; pos < numCols; pos++)
lcd.print(text[pos]);
delay(2000); // allow time to read the first line before scrolling
pos=1;
/*unsigned long currentMillis1 = millis();   // need help here, its not working.
if (currentMillis1 - previousMillis1 >4*interval)
  {
     previousMillis1 = currentMillis1;
      pos=1; 
}
*/

while(pos <= length - numCols)
{
lcd.setCursor(0,0);
for( int i=0; i < numCols; i++)
lcd.print(text[pos+i]);
//delay(300);
//pos = pos + 1;
unsigned currentMillis = millis();
if (currentMillis - previousMillis > interval)
  {
   pos = pos + 1;
   previousMillis = currentMillis;
   }
}
}
}

jn-wp

hi lavan!

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

http://arduino.cc/en/Tutorial/BlinkWithoutDelay

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

hope i could help?


polymorph

Those variables that must hold the value of millis() should be unsigned long.
Steve Greenfield AE7HD
Nick Gammon on multitasking Arduinos:
http://gammon.com.au/blink
http://gammon.com.au/serial
http://gammon.com.au/interrupts

Lavan

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.

Grumpy_Mike

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

No it is not because as you said:-
Quote
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.
See:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
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.

Lavan

Thank you Mike!... I appreciate your guidance

Robin2

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

http://forum.arduino.cc/index.php?topic=223286.0

...R

Go Up