Page 1 of 1

Up-Down Timer

Posted: Thu Aug 31, 2017 12:13 am
by rbytes
This timer program uses much of the same code as Big Clock. I found a very nice Chrome colorfont to display the digits. All of the attached images other than the two screenshots need to be downloaded into a RESOURCES folder in the same location as the program.

There is an onscreen keyboard issue. Sometimes it won't appear automatically, and it must always be hidden manually before you start the timer. I will post this finding in Bug Reports.

Code: Select all

''
UD Timer Chrome by rbytes
August 2017
Up-Down timer
Uses graphic images of the 10 digits, the colon
and the 'Done' message
''
' ########################  Set Count Duration ###############################
maxt = 20 'total seconds to be timed
' ############################################################################

cnt = 0
'#.drawrect(160,255,343,500)        ' used only for setting digit sizes and offsets
xn=[31,200,455,620,865,1035]          ' reference x positions using p variable
hn=110;yn=230                       ' reference height and y position
#.scrclear(1,1,1)

' the character images vary in size and xy offsets so this array stores correction data
q=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]

' the "s" flags prevent #.image and show being executed more than once per image object
s1,s2,s3,s4,s5,s6 = 0

' the "flag" flags only allow any digit to be refreshed once when a second, minute or hour changes
flag1,flag2,flag3,flag4,flag5,flag6 = 0

' runflag starts the timing loop when it = 1
runflag = 0

#.output("Enter the total seconds to be timed, then press Return")
maxtt = #.input()
#.output("You entered "+maxtt+" seconds")

maxt=#.val(maxtt)

#.output("Press 0 to count down or 1 to count up, then press Return")
udflag = #.input()
#.output("You entered "+udflag)
udflg = #.val(udflag)

#.delay(1)
#.clear()
#.scrclear(.5,.5,.5)

' start button
butt=#.button
  butt.height = 50
  butt.text = "S T A R T"
  butt.width = 120
  butt.x = 550
  butt.y = 450
#.show(butt)

' draw the left colon
b = #.image                       ' colon image
b.source = "resources\Ccolon.png"
b.height = 80
b.x = 380
b.y = 250
#.show(b)

' draw the right colon
c = #.image                       ' colon image
c.source = "resources\Ccolon.png"
c.height = 80
c.x = 795
c.y = 250
#.show(c)

' set t depending on whether the count is down or up
? udflg = 1
  t=0
!
  t=maxt
.

>
' MAIN PROGRAM LOOP

' convert seconds into hours, minutes and seconds
hor,min,sec=gett(t)

' increment the loop counter
cnt += 1

' units of seconds
sc = #.int(sec-(sec>9)*#.lower(sec/10)*10)
? sc=0, sc = 10
e6 = sc
p = 6
? e6 != flag6
  nm="resources\C"+e6+".png"
  h=hn+q[1,e6];x=xn[p]+q[2,e6];y=yn+q[3,e6]
  f6(s6,nm,h,x,y)
.
flag6 = e6
s6 = 1

' tens of seconds
sect = #.lower(sec/10)
? sect=0, sect = 10
e5 = sect
p=5
? e5 != flag5
  nm="resources\C"+e5+".png"
  h=hn+q[1,e5];x=xn[p]+q[2,e5];y=yn+q[3,e5]
  ? e5 = 1, x += 95
  f5(s5,nm,h,x,y)
.
flag5 = e5
s5 = 1

' units of minutes
mn = #.int(min-(min>9)*#.lower(min/10)*10)
? mn=0, mn = 10
e4 = mn
p=4
? e4 != flag4
  nm="resources\C"+e4+".png"
  h=hn+q[1,e4];x=xn[p]+q[2,e4];y=yn+q[3,e4]
  f4(s4,nm,h,x,y)
.
flag4 = e4
s4 = 1

' tens of minutes
mint = #.lower(min/10)
? mint=0, mint = 10
e3 = mint
p=3
? e3 != flag3
  nm="resources\C"+e3+".png"
  h=hn+q[1,e3];x=xn[p]+q[2,e3];y=yn+q[3,e3]
  ? e3 = 1, x += 90
  f3(s3,nm,h,x,y)
.
flag3 = e3
s3 = 1

' units of hours
hr = #.int(hor-(hor>9)*#.lower(hor/10)*10)
? hr=0, hr = 10
e2 = hr
p=2
? e2 != flag2
  nm="resources\C"+e2+".png"
  h=hn+q[1,e2];x=xn[p]+q[2,e2];y=yn+q[3,e2]
  f2(s2,nm,h,x,y)
.
flag2 = e2
s2 = 1

' tens of hours
hort = #.lower(hor/10)
? hort=0, hort = 10
e1 = hort
p=1
? e1 != flag1
  nm="resources\C"+e1+".png"
  h=hn+q[1,e1];x=xn[p]+q[2,e1];y=yn+q[3,e1]
  ? e1 = 1, x += 100
  f1(s1,nm,h,x,y)
.
flag1 = e1
s1 = 1
#.delay(1)
? udflg = 1
  t += 1
  loopflg = maxt + 1
!
  t -= 1
.

' wait until the start button is pushed 
:test
? #.act(butt) = 1 | runflag = 1
  ' when the start button is pushed, we need a 1 second delay before the count changes
  ? runflag = 0, #.delay(1)
  ' the runflag variable keeps the timing loop running even though the Start button is pressed just once
  runflag = 1
  again ->
! 
  ' add a short delay to slow down the test loop
  #.delay(.1)
  test ->
.

' repeat the timing loop
:again
< cnt < maxt + 1

#.scrclear(1,0,0)

' draw the Done message
dn = #.image                       ' Done image
dn.source = "resources\CDone.png"
dn.width = 500
dn.x = 365
dn.y = 600
#.show(dn)
'd.height = 500

#.end()

' calculates hours, minutes and seconds from t (total seconds)
gett(t)=
  hor = #.lower(t/3600)
  min = #.lower((t-hor*3600)/60)
  sec = t%60
  <= hor,min,sec  ' returns values
.

f1(s1,src,h,x,y) =      ' hours tens digit
  ? s1=0
    i1 = #.image
  .
  i1.source = src
  i1.height = h
  i1.x = x
  i1.y = y
  ? s1=0, #.show(i1)
.

f2(s2,src,h,x,y) =      ' hours units digit
  ? s2=0
    i2 = #.image
  .
'  i2 = #.image
  i2.source = src
  i2.height = h
  i2.x = x
  i2.y = y
  ? s2=0, #.show(i2)
.

f3(s3,src,h,x,y) =      ' minutes tens digit
  ? s3=0
    i3 = #.image
  .
'  i3 = #.image
  i3.source = src
  i3.height = h
  i3.x = x
  i3.y = y
  ? s3=0, #.show(i3)
.

f4(s4,src,h,x,y) =      ' minutes units digit
  ? s4=0
    i4 = #.image
  .
'  i4 = #.image
  i4.source = src
  i4.height = h
  i4.x = x
  i4.y = y
  ? s4=0, #.show(i4)
.

f5(s5,src,h,x,y) =      ' seconds tens digit
  ? s5=0
    i5 = #.image
  .
'  i5 = #.image
  i5.source = src
  i5.height = h
  i5.x = x
  i5.y = y
  ? s5=0, #.show(i5)
.

f6(s6,src,h,x,y) =      ' seconds units digit
  ? s6 = 0
    i6 = #.image
  .
  i6.source = src
  i6.height = h
  i6.x = x
  i6.y = y
  ? s6 = 0, #.show(i6)
.

Re: Up-Down Timer

Posted: Thu Aug 31, 2017 12:39 am
by Mr. Kibernetik
Suggestion: to create setup mode when user sets timer time before run, for example using special buttons above and below digits.

Re: Up-Down Timer

Posted: Thu Aug 31, 2017 12:44 am
by rbytes
Good idea. Thanks.