Pages: [1]   Go Down
Author Topic: Replacing delay with millis  (Read 369 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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;
   }
}
}
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

Lacey, Washington, USA
Offline Offline
Faraday Member
**
Karma: 162
Posts: 2500
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Those variables that must hold the value of millis() should be unsigned long.
Logged

Steve Greenfield AE7HD
CET Consumer Electronics and Computer
Please don't read your attitudes into my messages

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 634
Posts: 34563
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
 
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Mike!... I appreciate your guidance
Logged

UK
Offline Offline
Tesla Member
***
Karma: 135
Posts: 7721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: