'Delay' alternative? Need LCD to update!

Good evening to one and all :slight_smile:

Right off the bat I am not a programmer, more the client! Please bear with me and my non-technical language, and numerous errors… I’m sure you’ll know what I mean :wink:

I am posting because a friend of mine who is a programmer has been working on a project for me, but I have a little problem with the finished product and he claims that the entire code will need to be re-written to correct the problem! How much truth is in this I’m not sure, but I’m hoping that someone here can shed some light on the problem, and hopefully offer a solution to it!

To keep it simple, the program (which is based on an Arduino ofc) controls a set of 120 LED’s and adjust their brightness according to the date. They also fade to on or off over a 30 minute period at set times to the desired brightness. The different brightnesses are set using PWM. The rate of fade is controlled using the ‘Delay’ function, in that in divides the number of brightness steps required by 30 minutes, and the result set as a delay between steps.

The problem with this is that when the desired brightness is only 2 or 3, the delay is something like 10 minutes between each step, and this also means that the time on the LCD does not update for 10 minutes as the controller is in delay mode and can’t do anything. So I ultimately have a clock that is wildly inaccurate when the delay is long.

I believe this is the bit of code that does the delaying @ 20:30…

} else if ((hour() == 20 && minute() == 30 && (second() == 0 || intFadingToPhase == 0)) || intFadingToPhase == 1) {

if (intFadingToPhase == 0) {
brightness = 255;
finalbrightness = pgm_read_word(&ledpwm[phase]);

intFadingToPhase = 1;

intStepSecs = 1800 / (255-finalbrightness);

analogWrite(9, brightness);

delay((unsigned long)intStepSecs * 1000UL);

if (brightness <= finalbrightness) {
intFadingToPhase = 0;
} else {

Now I understand that the whole thing works on a loop, that much I get, and that when it gets to the delaying part then the whole controller stops doing anything, including updating the LCD. But is it possible to have it fade to the correct brightness over 30 minutes and still update the LCD, even if the delay for each step is many minutes. What function can be used to do that, and is it really as big a job as he makes out??

Any help much appreciated :slight_smile:

Probably the easiest way would be to write a “delayWithUpdates()” function to use in place of “delay()”. Then the delay can be done in many short segments with LCD updates.

void delayWithUpdates(unsigned long milliseconds)
    unsigned long startTime = millis();
    while (millis() - startTime < milliseconds)
       //  Do whatever LCD updates you like in here

Whilst your code means nothing to me =( if I send him that with your comments, would you expect that should be enough for him to know what to do with it? I don’t expect him to know everything as he is actually an ASP programmer, but has ultimately done OK so far with this project.

He was talking about doing it with ‘millis’ as you have used there and was talking about the time rolling over and causing errors, is that something he will also need to address or is it not really relevant here?

Please forgive my ignorance, I’m trying my best, and many thanks for your help!

The way the math is done, "millis() - startTime > interval", the roll-over is taken care of.

Background: Every 47+ days the millis() counter rolls over to 0 and starts over. If you subtract the earlier time from the current time, even if the timer has rolled over to 0, the answer will be the number of milliseconds that have elapsed since the earlier time.

It seems like magic but it works. :)

OK, I'll take your word for it! I'll send him that info and then hopefully he can get it fixed. I don't think a functioning clock is asking too much, and from your code it doesn't look like too big a job. Perhaps we reached the limits of his knowledge so he told a little white lie!

Many thanks for that :)

K22Spider: OK, I'll take your word for it! I'll send him that info and then hopefully he can get it fixed. I don't think a functioning clock is asking too much, and from your code it doesn't look like too big a job. Perhaps we reached the limits of his knowledge so he told a little white lie!

Many thanks for that :)

More likely, he just didn't think of JohnWasser's cunning way to solve it.

It was not long ago, when I was new to these forums, that I had the same doubts about rollover that your friend had. One of the more experienced members of the Arduino community provided the solution I now use. I checked the math myself and it works.

If your friend wants to double-check the math he can try a StartTime of 0xFFFFFF00 (256 milliseconds before rollover) and an end time of 0x00000100 (256 milliseconds after rollover) and do the subtraction. The answer will be 0x00000200 (512 milliseconds), just as expected.

Many thanks for all your responses :)

I would like to add at this point, that he has since read this thread in its entirety, and now says I will have to get down on my knees and 'do things' to him if I want the code fixed :~ Perhaps I was a bit harsh?!


He said that for your efforts at helping?? I've no idea what you might have said in person, of course. Doesn't sound like someone I'd want working for me.

As an ASP programmer, he's used to an OS and application layer taking care of some of those things like updating a screen during an application delay cycle. Arduino doesn't operate in that way, there's no background layer taking care of things for you, barring interrupts.

Just as a thought also, over long periods the clock will drift a bit, it might be a good time to consider changing things to use a Realtime clock chip with battery backup.

The project you are doing is fairly straightforward.. you are hardly dealing with a professional if he is choosing to act in that way- in fact, I'd suggest hiring one of a myriad of twelve year olds, to gain a little maturity-- and programming skill. The fact is, you are trying to assist him in tracking down a good solution to a technical problem. There's a lot of expertise here, not surprisingly, a workable solution popped up quite quickly. He's not a bad programmer because he didn't think of this particular solution, he's a bad programmer because of his reaction to assistance in finding a solution. I'm willing to bet he's of the "I came up with everything I've ever done all by myself, in fact, I make my own computers with a bucket of sand and a candle!" crowd... more skilled than they think they are, yet so insecure they wrap themselves in nonsense bravado. It's common, unfortunately.

If he's reading this, tell him to relax a bit. If he really wants to be that offensive, I think you're going to be better off shopping for a new developer- before his ego sinks your project. Tell him to take a deep breath, relax, and then if he wants.. post here himself (or even via your account) the issues he's running into. Getting frustrated can make people act like jackasses sometimes.. we get it. We (and you) are not the enemy.. the bug in the project is.....