Countdown Timer coding

jackberg

New Member
Hello everybody,
By reading multiples articles on this forum, I've just wrote some test code for a Countdown Timer, to run on a 08m2+, It's working fine,
I'm concern about, if there's a way to simplify the codes, making it shorter or faster.

Thank's to all for the good work.
Code:
=============================================
;countdown timer Jul-18-2020
;countdown var.
SYMBOL secs = b0
SYMBOL mins = b1
SYMBOL hours = b2
;leading "0" var.
SYMBOL secs1 = b3
SYMBOL mins1 = b4
SYMBOL hours1 = b5

;start countdown 00:01:10  HH:M:SS
b2=0:b1=1:b0=10

Main:
hours1="":mins1="":secs1=""  ;leading "0" if needed
if secs<10 then
    secs1="0"
    endif
if mins<10 then
    mins1="0"
    endif
if hours<10 then
    hours1="0"
    endif
;send format HH:MM:SS 
serout 0,N2400,(hours1,#hours,":",mins1,#mins,":",secs1,#secs,cr,lf)

if secs=0 and mins=0 and hours=0 then
    end
endif
    if mins=0 and hours >0 then  ;hours
        dec hours
        mins=59
        goto main
    endif
   
    if secs=0 and mins >0 then  ;minutes
        dec mins
        secs=59
        goto main
    endif
   
    if secs >0  then           ;seconds
        dec secs
    endif
Goto Main
==================================================================================================
24027
 
Last edited:

lbenson

Senior Member
Your code in the screenshot is formatted with indentations. If you want to retain that formatting in your forum posts, put the code within the forum tags, "[ code]" and "[ /code]" (but remove the space after the "[" which I've put in so the forum doesn't act on that text as if it were a tag).

You can edit your post to put in these tags. It will make it easier for viewers to understand the flow of your program.
 

Aries

New Member
It's easier for us on the forum to read your code if you enclose it with [ CODE ] at the start and [ /CODE ] at the end (without the spaces inside the brackets). This keeps the formatting which you obviously lose when posting directly as text.

I don't think your code actually works - if you run it starting from 01:00:10, you get the following:
Code:
01:00:10
00:59:10
00:59:09
00:59:08
so there's something wrong with your logic. You test for mins being zero, but not secs.

Also, this:
Code:
hours1=""
actually sets hours1 to zero, not an empty string. Although this seems to have worked for you, if you use sertxd to output the string, you see the interspersed zeroes.

This is a smaller piece of code, which decodes the single variables secs, mins and hours each time into two digits
Code:
;countdown timer Jul-18-2020
;countdown var.
SYMBOL secs = b0
SYMBOL mins = b1
SYMBOL hours = b2
;leading "0" var.
SYMBOL secs1 = b3
SYMBOL mins1 = b4
SYMBOL hours1 = b5

;start countdown 00:01:10 HH:M:SS
b2=1:b1=1:b0=10

Main:
b6 = secs // 10
b7 = secs / 10
b8 = mins // 10
b9 = mins / 10
b10 = hours // 10
b11 = hours / 10
;send format HH:MM:SS
serout 0,N2400,(#b11,#b10,":",#b9,#b8,":",#b7,#b6,cr,lf)

if secs=0 and mins=0 and hours=0 then
end
endif

if secs = 0 then        ' change of minute
    secs = 59
    if mins = 0 then    ' change of hour
        mins = 59
        dec hours
    else
        dec mins
    endif
else
    dec secs
end if

Goto Main
 

AllyCat

Senior Member
Hi,
It's working fine,
Does it work with a "real" PICaxe ?

I can't see where the one second "ticks" are timed; I'd expect to see a PAUSE 950 or similar (allowing around 40 ms for the SEROUT)? What level of timing accuracy do you want to achieve?

Normally I'd use the "time" variable (one second ticks), but that is upset by the Serial Commands. By far the best solution is an external Real Time Clock (RTC) chip or at least a one-second pulse for polling or an interrupt. If I HAD to do it with only a PICaxe, I'd probably break the SEROUT/SERTXD into individual characters (and handle the leading zeros at the same time) to send via HSEROUT, that shouldn't upset the time variable.

This was my first attempt (without the leading-zero feature) until I realised there are so many complications with timing accuracy. BTW, it's also limited to 18 hours. :(
Code:
#picaxe 08m2        ; Or any other M2
#no_data
#terminal 4800
symbol now = b1
symbol togo = w1
symbol hours = b4
symbol mins = b5
symbol secs = b6

hours = 1            ; Set as required
mins = 3
secs = 5
togo = hours * 60 + mins * 60 + secs
do
    dec togo
    secs = togo // 60
    mins = togo / 60 // 60
    hours = togo / 3600
    sertxd(#hours,":",#mins,":",#secs,cr,lf)
;    now = time : do : loop until time <> now  ; Runs slow when Serial communications are used
; Other stuff that can take up to 1 second
    pause 950        ; Adjust by trial and error with the final program
loop until togo = 0
    sertxd("DONE !")
Ah, I see you've added a PAUSE, but 1000 is too long.

Cheers, Alan.
 
Top