Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Simple Timer

edited September 23 in Code Sharing Posts: 9

I was frustrated with the lack of simple timers that work, so I devised this bit of code:

`
-- Timer

function setup()
t=00
s=0
m=0
textAlign(CENTER)
fontSize(50)
end

function draw()
t=t+1
if t == 60 then
t=00
s=s+1
print(s)
end

if s==60 then
    s=00
    m=m+1
    print(m)
end
text(t..":"..s..":"..m,WIDTH/2,HEIGHT/2)

end
`

Comments

  • dave1707dave1707 Mod
    Posts: 8,622

    @DJMoffinz Heres an update to your code.

    -- Timer
    
    function setup()
        t=00
        s=0
        m=0
        textAlign(CENTER)
        fontSize(50)
    end
    
    function draw()
        background(0)
        t=t+1
        if t == 60 then
            t=00
            s=s+1
            print(s)
        end
    
        if s==60 then
            s=00
            m=m+1
            print(m)
        end
        text(t..":"..s..":"..m,WIDTH/2,HEIGHT/2)
        text(string.format("%02d:%02d:%02d",t,s,m),WIDTH/2,HEIGHT/2-50)
    end
    
  • edited September 23 Posts: 9

    @dave1707
    Thanks! That looks much better visually!

  • edited September 23 Posts: 9

    Added a way to pause the timer if necessary:

    ```-- Timer

    function setup()
    t=00
    s=0
    m=0
    p=0
    textAlign(CENTER)
    fontSize(50)
    end

    function touched(touch)
    if touch.state == BEGAN then
    if p==0 then
    p=1
    elseif p==1 then
    p=0
    end
    end
    end

    function draw()
    background(0)
    if p==0 then
    t=t+1
    if t == 60 then
    t=00
    s=s+1
    print(s.."s")
    end

        if s==60 then
            s=00
            m=m+1
            print(m.."m")
        end
    end
    
    text(string.format("%02d:%02d:%02d",t,s,m),WIDTH/2,HEIGHT/2)
    
    if p==1 then
        text("PAUSED",WIDTH/2,HEIGHT/2 - 50)
    end
    

    end ```

  • dave1707dave1707 Mod
    edited September 23 Posts: 8,622

    @DJMoffinz I made minor changes to your code.

    function setup()
        t,s,m=0,0,0
        fontSize(50)
    end
    
    function touched(touch)
        if touch.state == BEGAN then
            p = not p   -- toggle p between true or false
        end
    end
    
    function draw()
        background(0)
        if p then
            text("PAUSED",WIDTH/2,HEIGHT/2 - 50)
        else
            t=t+1
            if t == 60 then
                t=0
                s=s+1
                if s==60 then
                    s=0
                    m=m+1
                end
            end
        end    
        text(string.format("%02d:%02d:%02d",t,s,m),WIDTH/2,HEIGHT/2)
    end
    
  • Thanks again!

  • edited October 18 Posts: 127
    tween.delay(3, function()
    print("Hello again, after 3 second!")
    end)
    
  • edited October 20 Posts: 9
    The issue with creating a timer by incrementing in the draw function is that there's no guarantee this will actually run 60 times per second. Heavy processing could cause the frame rate to drop and the timer would then be inaccurate.

    I'd suggest using Jarc's suggestion or the 'ElapsedTime' variable to make sure this is actually accurate.

    Splitting the timer into seconds & minutes also complicates the loop needlessly. It would be better (and easier) to just use seconds and calculate the duration when initialising the timer (seconds + minutes * 60).
  • dave1707dave1707 Mod
    edited October 20 Posts: 8,622

    @Steppers ElapsedTime is also affected by heavy processing. Using tween delay doesn’t give you the ability to pause/show the time. It’s mainly used for process delays. If you want a timer unaffected by heavy processing then use the socket function gettime. Under heavy processing you won’t get a smooth timer display because the draw function will be slowed down, but the time values will be correct.
    Here’s an example using the socket function gettime.

    viewer.mode=FULLSCREEN
    
    function setup()
        font("Courier")
        s=require("socket")
        h,m,sec,dd=0,0,0,0
        st=0
    end
    
    function draw()
        background(0)
        fill(255)
        format()
        fontSize(100)
        text(string.format("%02d:%02d:%02d.%02d",h,m,sec,dd),WIDTH/2,HEIGHT/2)
        fontSize(30)
        text("Tap screen to start/pause/lap",WIDTH/2,HEIGHT-100)
    end
    
    function touched(t)
        if t.state==BEGAN then
            if st==0 then 
                st=s:gettime()
            end
            run=not run
        end
    end
    
    function format()
        if run then
            diff=s:gettime()-st
            h=diff//3600
            m=(diff%3600)//60
            sec=(diff%60)//1
            dd=((diff*100)%100)//1
        end
    end
    
    
    
  • Posts: 727

    My newest iPad runs at 120 per second.

  • dave1707dave1707 Mod
    Posts: 8,622

    @RonJeffries Any timer that adds 1 per draw cycle and divides by 60 to get seconds will be wrong on your iPad. But the above code that uses socket gettime will still be correct on yours.

  • Posts: 727

    correct. i was just confirming the need for that. would ElapsedTime not work as well?

  • dave1707dave1707 Mod
    edited October 21 Posts: 8,622

    @RonJeffries I believe ElapsedTime would work on the 120. As I mentioned above, under a heavy load, ElapsedTime loses time. So it’s not as accurate under all circumstances. Under a normal load, ElapsedTime works well.

  • Posts: 727

    interesting, didn't know that. btw, where did you find "socket" and what's in it? thanks!

  • dave1707dave1707 Mod
    Posts: 8,622

    I’m not sure where or how long ago I found socket, but you can try this link and look under socket. Tap each command for more info.

    http://w3.impa.br/~diego/software/luasocket/reference.html
    
  • edited October 21 Posts: 727

    super, thanks! it's built in to codea? what else is in there?

Sign In or Register to comment.