Get Current Time and lf then comparisons

Maybe someone can guide with this: I'm trying to have a call to a function when the current time hits 10 seconds, 20 seconds, 30 etc.
For that I used the Lua code:

currentMilliseconds = playdate.getCurrentTimeMilliseconds()
elapsedseconds = math.floor (currentMilliseconds/1000) %60

and for the comparison part:

if elapsed seconds == 10 then
call-to-the-function
end

Problem is that this part is not working well as I missing something (type of variable conversion?) as the condition seems to be true for values between 10, 10.1 , 10.2, 10.3, 10.n milliseconds or at least until it reach 11

Any idea? I'm totally confused :sweat_smile:
Thanks in advance

Would this work?

currentMilliseconds = playdate.getCurrentTimeMilliseconds()
elapsedseconds = math.floor(currentMilliseconds * 1000)
ten_second_tic = elapsedseconds % 10

if ten_second_tic = 0 then
     -- do something
end

Or you could use a delay timer and reset it each time it triggers.

1 Like

i think there needs to be == in your if statement?

2 Likes

Yes, I think so, thanks for the help both of you guys, I'll check it out and back to report! Btw do you know any good code example about delays and reset triggers beyond the ones in the documentation, I'm struggling a little bit with that as well.
Tnx! :grin:

Sadly I'm afraid is not working. Very curious...
The idea is simply: have a time counter and trigger an action each several seconds or minutes, but for some reason the comparison between the timer and a integer number using ==
does not work as expected.
Is there any other way to do this with a delay timer?
Tnx!

Fresh morning eyes!

This works:

import 'CoreLibs/graphics'

local gfx = playdate.graphics

local current_milliseconds = nil
local current_seconds = nil
local ten_second_tic = nil

function playdate.update()
    gfx.clear()
    
    current_milliseconds = playdate.getCurrentTimeMilliseconds()
    current_seconds = math.floor(current_milliseconds / 1000) % 60
    ten_second_tic = current_seconds % 10
    
    gfx.drawTextAligned(current_seconds, 200, 60, kTextAlignment.center)
    
    if ten_second_tic == 0 then
        gfx.fillCircleAtPoint(200, 120, 40)
    end
    
end

But like you said, the if statement will trigger each frame of the 10th second.

This will trigger the timerCallback function just once every 10 seconds:

import 'CoreLibs/graphics'
import 'CoreLibs/timer'

local gfx = playdate.graphics

local current_milliseconds = nil
local current_seconds = nil
local times_triggered = 0
local ten_second_timer = nil
local initialised = false

function playdate.update()
    if not initialised then
        initialised = true
        ten_second_timer = playdate.timer.new(10000, timerCallback)
        ten_second_timer.repeats = true
    end
    
    gfx.clear()
    
    current_milliseconds = playdate.getCurrentTimeMilliseconds()
    current_seconds = math.floor(current_milliseconds / 1000) % 60
    
    gfx.drawTextAligned('seconds: ' .. current_seconds, 200, 60, kTextAlignment.center)
    gfx.drawTextAligned('timer value: ' .. ten_second_timer.timeLeft, 200, 80, kTextAlignment.center)
    gfx.drawTextAligned('times triggered: ' .. times_triggered, 200, 100, kTextAlignment.center)
    
    playdate.timer.updateTimers()
    
end

function timerCallback()
    times_triggered += 1
end
1 Like

FWIW I had a lot of timed delays like that in my Playtime clock app, and I ended up handling them by setting a timer—and then setting it again and again.

(But I didn't set it for, say, "10 seconds" every time—I checked the time, calculated the "next 10 second mark" and then set the timer for less if drawing delay etc. had already taken up time. Otherwise, the delay would eventually drift. Plus, the very FIRST timer would be wrong without that current-time check.)

2 Likes

Thank you so much, all of you!
A lot of good information and examples. Apparently it was not a minor subject as I though.
I will take a deep look to the code above (much appreciated!) and try to implement a solution from it. :smile:

1 Like

This last example works fantastic, easy enough to adapt it to my requirements! I'm already testing it and doing minor tweaks. Nice solution :+1:

1 Like