I want it to be crystals every 20 minutes, and cocoa every 15 minutes, so that’s why I made it 20 and 15. I just didn’t change the variable name.
In that case, you would need to restructure the code a little bit. The example I gave was specifically targeted to reset every half an hour, because the if TimeToWait < 0
statement only checks once. It only needed to check once because 1 hour divided into half hours gives 2 (there are 2 half hours in an hour), so it needed to check once (n - 1, where n is how many times an hour can be divided into the sections of the length you want). If we were going by 20 minutes, we would need to check twice, because 1 hour can be divided into 3 20 minute sections.
The restructured code would look a bit like this:
local CurrentSec = date.min * 60 + date.sec
local AlignMinutes = 20 -- align with 20 minute intervals
local AlignMinutesSec = 60 * AlignMinutes -- the number of seconds in each 20 minute section
-- Align the time to the AlignMinutes intervals
local TimeToWait = AlignMinutesSec - CurrentSec
print(TimeToWait)
for i = 1, math.floor(60 / AlignMinutesSec) - 1 do
if TimeToWait < 0 then
TimeToWait = (i + 1) * HalfHourSec - CurrentSec
print(TimeToWait)
else break
end
end
The TimeToWait is obviously under 0, but somehow this code doesn’t run (I think):
for i = 1, math.floor(60 / AlignMinSecs) - 1 do
if TimeToWait < 0 then
TimeToWait = (i + 1) * AlignMinSecs - CurrentSec
print(TimeToWait)
else warn("TimeToWait is not under 0") break
end
end
I didn’t get the warn message when TimeToWait was over 0 either.
Really sorry, my bad. I mis-wrote one of the variables.
It should be AlignMinutes
here, not AlignMinSecs
, here is the code:
local DateTable = os.date("*t", os.time())
local CurrentSec = DateTable.min * 60 + DateTable.sec
local AlignMinutes = 20 -- align with 20 minute intervals
local AlignMinutesSec = 60 * AlignMinutes -- the number of seconds in each 20 minute section
-- Align the time to the AlignMinutes intervals
local TimeToWait = AlignMinutesSec - CurrentSec
print(TimeToWait)
for i = 1, math.floor(60 / AlignMinutes) - 1 do
if TimeToWait < 0 then
TimeToWait = (i + 1) * AlignMinutesSec - CurrentSec
print(TimeToWait)
else break
end
end
print("You need to wait " .. TimeToWait .. " seconds until the next segment!")