(Countdown-)Timer for UV exposure unit

Hey there!

Since I want to be able to create my own PCBs, I am in need of a device to expose the copper boards. On the hardware side of things I have all the parts I need to build such a device. Also, most of the stuff on the software side should be rather simple, too.

What I want my code to do is, that I can set a time, which will probably be done using a rotary encoder. A button press will engage the UV-lights, which will be turned off after the desired time. The time of exposure will presumably be somewhere between 300 and 500 Seconds (will need to test that) so there really is no need to hassle around with hours and minutes - using only seconds will be good enough for me. Oh and I will be using one of those 16 by 2 LCD-Displays to display the status (remaining time and so on...)

Now theres the 'but': After some time of googlin' for solutions that other people came up with, I am really unsure about how to do that timing part. A lot of people just threw delays in their code, which I would rather avoid if possible.

So what would probably be "the best way"™ to implement such a countdown timer then? One thing I can think of is using the millis() function, which constantly will be checked against a timer variable. Another possibility might be the use of Timer1 and interrupts but I haven't got my head around that whole TimerX stuff quite yet (I'd try to dig into this if it would make the whole thing a lot easier though).

Greetings - V

The reason why people throw delays in is because it is simple, it works, and is entirely appropriate. When you use delay, Arduino just sits there and does nothing but count off the delay, but that's fine, you haven't got anything for it to do anyway. It's just a timer.

Except for the time left display on the LCD.....

The reason people throw in delay()'s is usually because they don't understand what they are doing.

Use the millis() approach.

Except for the time left display on the LCD.....

Well, that's just a delay. I imagine a delay of one second would be quite appropriate . People who understand what they are doing are quite capable of thinking their way through that, and doing it 500 times.

You don't suppose that he actually wants to see the timer display "count down", do you?

Or, are you suggesting that he builds a loop (or function) that writes to the LCD, delays for one second, loops back to the display part and puts that in a loop of 500 iterations????

Give that man a cigar!

Well, maybe a diet Pepsi,,,, wait, that's not too healthy either.

GIVE THAT MAN A BEER!

123Splat:
You don't suppose that he actually wants to see the timer display "count down", do you?

Yes

Or, are you suggesting that he builds a loop (or function)

Probably, but it might be all in setup. It is only a only a one-shot darkroom timer and will be quite accurate enough. I'm not saying OP has to use delay, I'm saying it is OK to do so, which goes some way to explain why other people do it. There are times when using delay can actually get you into trouble. This is not one of them.

Well yes, I know that delays do come in handy sometimes but personally I really don't like them. I guess I'll try to come up with a solution using the millis()-function. I'll post my code here once I have it up and running (or bumped in some kind of problem :smiley: ).

What do you guys think about this approach?

int timeOfExposure = 300;                          // time to expose in seconds
long elapsedTime = 0;                              // elapsed time in milliseconds
long tempTime = 0;                                 // temporary variable


while ((elapsedTime/1000) <= timeOfExposure)       // count down until the desired time of exposure is reached
{
  tempTime = millis();                             // fetch the actual time and
  if ((tempTime - elapsedTime) >= 1000)            // check if a second has passed by
  {
    lcd.setCursor(0, 0);
    lcd.print(timeOfExposure-(elapsedTime/1000));  // if so, print the time that is left and
    elapsedTime += (millis()-tempTime);            // add the time that has been spent
  }
}

I guess there is still some room for optimization.

Edit: I tested that code (the sketch is just doing a countdown, nothing else yet) and it is off by a mere second. That's good enough for the girls I go out with. :smiley: