Counter simulation ?

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.

.@dave1707 : very nice timer. Btw, are you a real human being or a coding machine that pretends to be a human? :wink: I am amazed by the amount of code you can produce and share every day!

Thanks a lot for this Timer ( I named it SIMULATED TIMER) .
Let me explain what follows this crucial step of the visualisation of à realistic Timer :

  1. I have to fix the Time of counting by the virtual device to 60 seconds for a determinated expérience. This fixed time Will remain unchanged for all what follows.

  2. I Will perform a measurement WITHOUT any material between the SOURCE of radiation and the VIRTUAL DEVICE (Simulated display of the Geiger Muller counter), the Number of counts lets say is equal to 1002

  3. I will interpose a material of 2 centimeters width, the attenaution of Gamma radiation will result in a number of counts less than 1002.

  4. … 4 centimeters width …

  5. … 6 centimeters width …

.
.
.

I must add to the screen the SIMULATED TIMER (yet created) and A SIMULATED COUNTER taking into account this decrease of counts.

I made this manipulation on my computer with DELPHI, but I was unable to synchronize perfectly the Number of counts with the duration.

I hope that this message is clear.

Thanks one more Time @dave1707.

@letaief

Maybe this timer will interest you. It has an LED type display and goes to 999.99 seconds. Tap the screen to start or again to stop. Triple tap to reset.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    lineCapMode(SQUARE)
    init()
end

function init()
    -- create 5 digit led display
    l1=led(750,250)
    l2=led(600,250)
    l3=led(450,250)
    l4=led(300,250)
    l5=led(150,250)
    
    z=0
    z1=0
    z2=0  
    pause=true        
end

function draw()
    background(40, 40, 50)
    
    -- instructions
    fontSize(40)
    text("Tap to start",WIDTH/2,700)
    text("Tap to stop/pause",WIDTH/2,650)  
    text("Triple tap to reset",WIDTH/2,600) 
    text("(999.99) Seconds",WIDTH/2,120)
    
    -- determine elapsed time with pause 
    if pause then 
        z1 = ElapsedTime - z2
    else
        z2 = ElapsedTime-z1
        z = z2*100
        if z2>1000 then    -- stop timer
            init()
        end
    end
    
    -- always display right 3 digits
    str=string.format("%05d",z)
    d1=tonumber(string.sub(str,5,5))
    d2=tonumber(string.sub(str,4,4))
    d3=tonumber(string.sub(str,3,3))
    
    -- only display left 2 digits if > 0
    str=string.format("%5d",z)
    d4=tonumber(string.sub(str,2,2))
    d5=tonumber(string.sub(str,1,1))                
    
    -- create leds
    l1:create(d1)
    l2:create(d2)
    l3:create(d3)
    l4:create(d4)
    l5:create(d5)
    
    -- decimal point
    noStroke()
    fill(0, 255, 135, 255)
    rect(572,240,15,15)
end

function touched(t)
    if t.state==BEGAN then
        if t.tapCount<3 then
            pause=not pause  -- toggle pause true or false
        elseif t.tapCount==3 then
            init()
        end
    end
end

led = class()

function led:init(x,y)
    self.x=x    -- x position of led
    self.y=y    -- y position of led
end

function led:create(val)
    self:set(val)
    self:draw()
    self:clear()
end
        
function led:set(val)
    -- set an element true based on the value
    if val==0 then
        led1=true;led2=true;led3=true;led5=true;led6=true;led7=true
    elseif val==1 then
        led3=true;led6=true
    elseif val==2 then
        led1=true;led3=true;led4=true;led5=true;led7=true
    elseif val==3 then
        led1=true;led3=true;led4=true;led6=true;led7=true
    elseif val==4 then
        led2=true;led4=true;led3=true;led6=true
    elseif val==5 then
        led1=true;led2=true;led4=true;led6=true;led7=true
    elseif val==6 then
        led1=true;led2=true;led5=true;led7=true;led4=true;led6=true
    elseif val==7 then
        led1=true;led3=true;led6=true
    elseif val==8 then
        led1=true;led2=true;led3=true;led4=true;led5=true;led6=true;led7=true
    elseif val==9 then
        led1=true;led2=true;led3=true;led4=true;led6=true;led7=true
    end
end

function led:draw()
    stroke(0, 255, 135, 255)
    strokeWidth(14)
    
    -- draw each element if true
    if led1 then
        line(self.x+15,self.y+220,self.x+115,self.y+220)    -- top element
    end
    if led2 then
        line(self.x+5,self.y+115,self.x+10,self.y+215)    -- top left element      
    end
    if led3 then  
        line(self.x+115,self.y+115,self.x+120,self.y+215)    -- top right element
    end
    if led4 then
        line(self.x+10,self.y+110,self.x+110,self.y+110)    -- middle element   
    end
    if led5 then
        line(self.x,self.y+5,self.x+5,self.y+105)    -- bottom left element       
    end
    if led6 then
        line(self.x+110,self.y+4,self.x+115,self.y+105)    -- bottom right element     
    end
    if led7 then
        line(self.x+5,self.y,self.x+105,self.y)    -- bottom element    
    end
end

function led:clear()  
    -- clear all elements
    led1=false
    led2=false
    led3=false
    led4=false
    led5=false
    led6=false   
    led7=false
end

The simulation is about Geiger Muller counter

http://en.wikipedia.org/wiki/File:Geiger_Mueller_Counter_with_Circuit-en.svg

I am about to count Gamma radiation by this counter, and I am about to interpose different width s of à material (2 millimeters , 4 milimeters, etcs …), between the source of radiation and the counter ? The Number of counts is designed in French by counts per minute.

http://en.wikibooks.org/wiki/Basic_Physics_of_Nuclear_Medicine/Attenuation_of_Gamma-Rays#Effect_of_Thickness

The font is effectivly the DBLCDTTempBlack.

I am sorry for thé lack of explanations.

Thanks in advance.

Hello @letaief. Are the digits in font("DBLCDTTempBlack") what you are looking for?

@letaief

Can you explain a little more of what you want. Do you want this program changed to give you multiple timers of 60 sec., 90 sec, 120 sec, 150 sec. etc. Also, I don’t understand what you mean with the 1200 cpm. Do you want a timer that will display a resolution of 1200 cpm, which is 20 counts per second, which is equal to .05 seconds. So instead of displaying whole seconds, you want it to show xxx.xx seconds.

@Jmv38

Thanks for the comment. I generally code while I’m watching TV. I find it relaxing and enjoyable. I also like to help others by writing small example code. I guess my head is just full of code waiting to get out.

@letaief

If you want a timer for a specific length of time, you can add the 4 lines of code below. In this example, the timer makes a sound at 60 seconds and resets. For any other value, just change the value 60 to what you want.

        -- existing code
        if z2>1000 then    -- stop timer
            init()
        end


        -- add this code to the above program
        if z2>=60 then
            sound(SOUND_BLIT, 13441)
            init()
        end

.@dave1707 : I added 3 parameters WIDTH,HEIGHT and THICKNESS of Led segments
I am unable to retrieve a variable enabling the positionning of leds when I use small values of width.
Thanks in advance.

-- COUNTER3

-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    lineCapMode(SQUARE)
    init()
end

function init() 
    xw=20
    xh=20
    xt=2
    
    -- create 5 digit led display
    l1=led(750,250,xw,xh,xt)
    l2=led(600,250,xw,xh,xt)
    l3=led(450,250,xw,xh,xt)
    l4=led(300,250,xw,xh,xt)
    l5=led(150,250,xw,xh,xt)
    
    a=90 b=30 c=8
    
    xl1=led(750,450,a,b,c)
    xl2=led(600,450,a,b,c)
    xl3=led(450,450,a,b,c)
    xl4=led(300,450,a,b,c)
    xl5=led(150,450,b,b,c)
    
    z=0
    z1=0
    z2=0  
    pause=true       
end

function draw()
    background(40, 40, 50)
    
    -- instructions
    fontSize(40)
    text("Tap to start",WIDTH/2,700)
    text("Tap to stop/pause",WIDTH/2,650)  
    text("Triple tap to reset",WIDTH/2,600) 
    text("(999.99) Seconds",WIDTH/2,120)
    text("Size 1",120,450)
    text("Size 2",120,250)
    
    -- determine elapsed time with pause 
    if pause then 
        z1 = ElapsedTime - z2
    else
        z2 = ElapsedTime - z1
        z = z2*100
        if z2>=20 then     -- set the number of seconds example : 20 
            sound(SOUND_BLIT,13441)
            init()
        end
    end
    
    -- always display right 3 digits
    str=string.format("%05d",z)
    d1=tonumber(string.sub(str,5,5))
    d2=tonumber(string.sub(str,4,4))
    d3=tonumber(string.sub(str,3,3))
    
    -- only display left 2 digits if > 0
    str=string.format("%5d",z)
    d4=tonumber(string.sub(str,2,2))
    d5=tonumber(string.sub(str,1,1))                
    
    -- create leds
    l1:create(d1)
    l2:create(d2)
    l3:create(d3)
    l4:create(d4)
    l5:create(d5)
    
    xl1:create(d1)
    xl2:create(d2)
    xl3:create(d3)
    xl4:create(d4)
    xl5:create(d5)
      
    -- decimal point
    noStroke()
    fill(0, 255, 135, 255)
    rect(572,240,15,15)
end

function touched(t)
    if t.state==BEGAN then
       if t.tapCount<3 then
            pause=not pause  -- toggle pause true or false
        elseif t.tapCount==3 then
            init()
        end
    end
end


led = class()

function led:init(x,y,w,h,t)
    self.x=x    -- x position of led
    self.y=y    -- y position of led
    self.w=w    -- w width of led segment
    self.h=h    -- h height of led segment
    self.t=t    -- t thickness of led segment
                -- LOOKING for the variable determinating the distance between two leds ???
end

function led:create(val)
    self:set(val)
    self:draw()
    self:clear()
end
        
function led:set(val)
    -- set an element true based on the value
    if val==0 then
        led1=true;led2=true;led3=true;led5=true;led6=true;led7=true
    elseif val==1 then
        led3=true;led6=true
    elseif val==2 then
        led1=true;led3=true;led4=true;led5=true;led7=true
    elseif val==3 then
        led1=true;led3=true;led4=true;led6=true;led7=true
    elseif val==4 then
        led2=true;led4=true;led3=true;led6=true
    elseif val==5 then
        led1=true;led2=true;led4=true;led6=true;led7=true
    elseif val==6 then
        led1=true;led2=true;led5=true;led7=true;led4=true;led6=true
    elseif val==7 then
        led1=true;led3=true;led6=true
    elseif val==8 then
        led1=true;led2=true;led3=true;led4=true;led5=true;led6=true;led7=true
    elseif val==9 then
        led1=true;led2=true;led3=true;led4=true;led6=true;led7=true
    end
end

function led:draw()
    stroke(0, 255, 135, 255)
    
    strokeWidth(self.t)
    -- fill(self.couleur)
    -- draw each element if true
    if led1 then
       rect(self.x+self.t,self.y+2*self.h+2*self.t,self.w,self.t)        -- top element 
    end
    if led2 then
        rect(self.x,self.y+self.h+2*self.t,self.t,self.h)                -- top left element 
    end
    if led3 then  
        rect(self.x+self.t+self.w,self.y+self.h+2*self.t,self.t,self.h)  -- top right element
    end
    if led4 then
        rect(self.x+self.t,self.y+self.h+self.t,self.w,self.t)           -- middle element 
    end
    if led5 then
        rect(self.x,self.y+self.t,self.t,self.h)                         -- bottom left element 
    end
    if led6 then
        rect(self.x+self.w+self.t,self.y+self.t,self.t,self.h)           -- bottom right element 
    end
    if led7 then
        rect(self.x+self.t,self.y,self.w,self.t)                         -- bottom element  
    end 
end

function led:clear()  
    -- clear all elements
    led1=false
    led2=false
    led3=false
    led4=false
    led5=false
    led6=false   
    led7=false
end

@letaief

The distance between the LEDs is based on the x parameter. For the LEDs l1 thru l5, change the x values from 750,600,450,300,150 to 750,700,650,600,550. The decimal point will be out of place, it’s size and location will have to be changed. A later version of the code I have has the decimal point as part of the LED. I’m also adding a scaling factor for the LEDs, but I got side tracked with the holiday so that isn’t finished yet.

@letaief

Apparently I was farther along than I remembered. Here is my latest version that has the decimal point as part of the LED and the scaling factor. The smallest scaling factor that’s readable is .04 and the largest where 1 digit fills the screen in portrait mode is 4. I didn’t set up the distance between the digits as scalable, that’s still set based on the x,y values when the digit is created. That distance could be scalable, but that would depend on the counter that’s created and the number of digits to fit on the screen. To try different scaling factors, change the value of sf in setup().

EDIT: I have all the digits with the same scaling factor here, but each digit can have it’s own size. For example, the fractional part could have a scaling factor of half the whole part.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    lineCapMode(SQUARE)
    init()
end

function init()
    -- create 5 digit led display (x,y,dp,sf)
    -- dp = decimal point 0=off 1=on
    -- s = scaling factor  .04 smallest that's readable
    sf=1
    l1=led(830,200,0,sf)
    l2=led(630,200,0,sf)
    l3=led(430,200,1,sf)
    l4=led(230,200,0,sf)
    l5=led(30,200,0,sf)
    
    z=0
    z1=0
    z2=0  
    pause=true        
end

function draw()
    background(40, 40, 50)
    
    -- instructions
    fontSize(40)
    text("Tap to start",WIDTH/2,700)
    text("Tap to stop/pause",WIDTH/2,650)  
    text("Triple tap to reset",WIDTH/2,600) 
    text("(999.99) Seconds",WIDTH/2,120)
    
    -- determine elapsed time with pause 
    if pause then 
        z1 = ElapsedTime - z2
    else
        z2 = ElapsedTime-z1
        z = z2*100
        if z2>1000 then    -- stop timer
            init()
        end
    end
    
    -- always display right 3 digits
    str=string.format("%05d",z)
    d1=tonumber(string.sub(str,5,5))
    d2=tonumber(string.sub(str,4,4))
    d3=tonumber(string.sub(str,3,3))
    
    -- only display left 2 digits if > 0
    str=string.format("%5d",z)
    d4=tonumber(string.sub(str,2,2))
    d5=tonumber(string.sub(str,1,1))                
    
    -- create leds
    l1:create(d1)
    l2:create(d2)
    l3:create(d3)
    l4:create(d4)
    l5:create(d5)

end

function touched(t)
    if t.state==BEGAN then
        if t.tapCount<3 then
            pause=not pause
        elseif t.tapCount==3 then
            init()
        end
    end
end

led = class()

function led:init(x,y,dp,sf)
    self.x=x    -- x position of led
    self.y=y    -- y position of led
    self.led1=false
    self.led2=false
    self.led3=false
    self.led4=false
    self.led5=false
    self.led6=false
    self.led7=false
    self.dp=dp
    self.sf=sf
end

function led:create(val)
    self:set(val)
    self:draw()
    self:clear()
end
        
function led:set(val)
    -- set an element on based on the value
    if val==0 then
        self.led1=true;self.led2=true;self.led3=true;self.led5=true;self.led6=true;self.led7=true
    elseif val==1 then
        self.led3=true;self.led6=true
    elseif val==2 then
        self.led1=true;self.led3=true;self.led4=true;self.led5=true;self.led7=true
    elseif val==3 then
        self.led1=true;self.led3=true;self.led4=true;self.led6=true;self.led7=true
    elseif val==4 then
        self.led2=true;self.led4=true;self.led3=true;self.led6=true
    elseif val==5 then
        self.led1=true;self.led2=true;self.led4=true;self.led6=true;self.led7=true
    elseif val==6 then
        self.led1=true;self.led2=true;self.led5=true;self.led7=true;self.led4=true;self.led6=true
    elseif val==7 then
        self.led1=true;self.led3=true;self.led6=true
    elseif val==8 then
        self.led1=true;self.led2=true;self.led3=true;self.led4=true
        self.led5=true;self.led6=true;self.led7=true
    elseif val==9 then
        self.led1=true;self.led2=true;self.led3=true;self.led4=true;self.led6=true;self.led7=true
    end
end

function led:draw()
    stroke(0, 255, 135, 255)
    sf=self.sf
    sw=14*sf
    if sw<3 then
        sw=3
    end
    strokeWidth(sw)
    
    -- draw each element if set
    if self.led1 then
        line(self.x+15*sf,self.y+220*sf,self.x+115*sf,self.y+220*sf)    -- top element
    end
    if self.led2 then
        line(self.x+5*sf,self.y+115*sf,self.x+10*sf,self.y+215*sf)    -- top left element      
    end
    if self.led3 then  
        line(self.x+115*sf,self.y+115*sf,self.x+120*sf,self.y+215*sf)    -- top right element
    end
    if self.led4 then
        line(self.x+10*sf,self.y+110*sf,self.x+110*sf,self.y+110*sf)    -- middle element   
    end
    if self.led5 then
        line(self.x,self.y+5*sf,self.x+5*sf,self.y+105*sf)    -- bottom left element       
    end
    if self.led6 then
        line(self.x+110*sf,self.y+4*sf,self.x+115*sf,self.y+105*sf)    -- bottom right element     
    end
    if self.led7 then
        line(self.x+5*sf,self.y,self.x+105*sf,self.y)    -- bottom element    
    end
    
    -- draw decimal point if selected
    if self.dp==1 then
        noStroke()
        fill(0, 255, 135, 255)
        rect(self.x+115*sf,self.y-10*sf,sw,sw)
    end
end

function led:clear()  
    -- clear all elements
    self.led1=false
    self.led2=false
    self.led3=false
    self.led4=false
    self.led5=false
    self.led6=false   
    self.led7=false
end