The following code is @dave1707 code , I need help on it to allow the INCREASE or DECREASE of values for fixed predefined TIME (ex: 1 minute, 90 seconds, 120 seconds, etc …) otherwise a possible use " I want to get a Number of 1200 cpm(read count per minute) while performing à measurement of radioactivity decay of à source during à fixed Time".
Is it possible to embed a font that simulates a real counter.
-- timer class and an example of 3 countdown timers
function setup()
--displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
a=Timer(200,600,100,50) -- set x, y position of timer button
b=Timer(200,500,100,50)
c=Timer(200,400,100,50)
fontSize(24)
end
function draw()
background(40, 40, 50)
text("Example of 3 countdown timers",WIDTH/2,940)
text("Press red button to start, it turns green when running",WIDTH/2,900)
text("Press green button to pause, it turns yellow",WIDTH/2,860)
text("Press yellow button to continue, it turns green",WIDTH/2,820)
text("Double tap button to reset to 0, it turns red",WIDTH/2,780)
if a:showTimer() then -- show timer and check for 0
print("timer a reached 0") -- add code here for 0
end
if b:showTimer() then
print("timer b reached 0")
end
if c:showTimer() then
print("timer c reached 0")
end
end
function touched(t)
if t.state==BEGAN then
if a:touched(t) then -- check if button pressed
a:startTimer(200) -- set countdown time
end
if b:touched(t) then
b:startTimer(30)
end
if c:touched(t) then
c:startTimer(40)
end
end
end
Timer = class()
function Timer:init(x,y) -- initialize timer values
self.x=x
self.y=y
self.w=100
self.h=50
self.timer=0
self.xx=0
self.pause=0
self.color=color(255,0,0,100)
end
function Timer:showTimer()
local str
local tmr
if self.pause>1 then -- timer paused
str="Start"
tmr=self.pause
self.color=color(229, 251, 9, 200)
elseif self.timer > 0 then -- timer is running
str="Pause"
self.color=color(0, 255, 0, 100)
tmr=math.ceil(self.xx - (ElapsedTime-self.timer))
if tmr==0 then
self.timer=0
return true -- timer reached 0
end
else -- timer was reset
str="Start"
self.color=color(255,0,0,100)
tmr=0
end
pushMatrix()
fill(self.color)
rectMode(CENTER)
rect(self.x,self.y,self.w,self.h)
fill(255)
tmr=string.format("%3d %s",tmr,str)
text(tmr,self.x,self.y)
popMatrix()
return false -- timer is running or was reset
end
function Timer:startTimer(val)
if self.pause>0 then
self.xx=(ElapsedTime-self.timer)+self.pause
self.pause=0
elseif self.timer>0 then
self.pause=math.ceil(self.xx - (ElapsedTime-self.timer))
else
self.xx=val
self.timer=ElapsedTime
end
end
function Timer:pauseTimer()
self.pause=math.ceil(self.xx - (ElapsedTime-self.timer))
end
function Timer:touched(t)
if t.state==BEGAN then -- screen was touched
if t.x>self.x - self.w/2 and t.x<self.x+ self.w/2 and
t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
if t.tapCount==2 then -- 2 taps, reset timer
self.timer=0
self.pause=0
return false
end
return true -- button pressed
end
end
return false -- no button pressed
end
Thanks in advance.