Simple Stopwatch

I made a simple stopwatch program which shows how to use DeltaTime. The time can be changed to a decimal or a whole number via a parameter.

--# Main
-- Stopwatch
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
    print("Stopwatch")
    timer=0
    t=0
    running=false
    btpy1=119
    btpy2=119
    parameter.boolean("Decimal", false, function() resetTimer() end)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 0, 0, 255)
    fill(255, 255, 255, 255)
    -- This sets the line thickness
    strokeWidth(5)
    font("HelveticaNeue-UltraLight")
    fontSize(90)
    -- Do your drawing here
    text(timer, WIDTH/2, HEIGHT/2 + 200)
    text("START", WIDTH/4, HEIGHT/3)
    if running==false then
                text("RESET", WIDTH/4*3, HEIGHT/3)
            else
                text("STOP", WIDTH/4*3, HEIGHT/3)
            end
    pushStyle()
    noStroke()
    fill(255, 255, 255, btpy1)
    ellipse(WIDTH/4, HEIGHT/3, 300)
    fill(255, 255, 255, btpy2)
    ellipse(WIDTH/4*3, HEIGHT/3, 300)
    popStyle()
    if not Decimal then    
        if running then
            t = t + DeltaTime
            if t>1 then
                timer = timer + 1
                t=0
            end
        end
    end
    if Decimal then
        if running then
            timer = timer + DeltaTime
        end
    end
end

function resetTimer()
    timer=0
end

function touched(t)
    if t.state==BEGAN then
        if t.x>WIDTH/4-150 and t.x<WIDTH/4+150 and t.y>HEIGHT/3-150 and t.y<HEIGHT/3+150 then
            running=true
            btpy1=200
        end
        if t.x>WIDTH/4*3-150 and t.x<WIDTH/4*3+150 and t.y>HEIGHT/3-150 and t.y<HEIGHT/3+150 then
            btpy2=200
            if running==false then
                resetTimer()
            else
                running=false
            end
        end
    end
    if t.state==ENDED then
        btpy1=119
        btpy2=119
    end
end

@Saturn031000 I made minor changes to your code so decimal didn’t show so many digits. Look up string.format for future use.


--# Main
-- Stopwatch
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
    print("Stopwatch")
    timer=0
    t=0
    running=false
    btpy1=119
    btpy2=119
    parameter.boolean("Decimal", false, function() resetTimer() end)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 0, 0, 255)
    fill(255, 255, 255, 255)
    -- This sets the line thickness
    strokeWidth(5)
    font("HelveticaNeue-UltraLight")
    fontSize(90)
    -- Do your drawing here
    text("START", WIDTH/4, HEIGHT/3)
    if running==false then
                text("RESET", WIDTH/4*3, HEIGHT/3)
            else
                text("STOP", WIDTH/4*3, HEIGHT/3)
            end
    pushStyle()
    noStroke()
    fill(255, 255, 255, btpy1)
    ellipse(WIDTH/4, HEIGHT/3, 300)
    fill(255, 255, 255, btpy2)
    ellipse(WIDTH/4*3, HEIGHT/3, 300)
    popStyle()
    if running then
        timer = timer + DeltaTime
    end
    if Decimal then  
        text(string.format("%.01f",timer), WIDTH/2, HEIGHT/2 + 200)
    else
        text(string.format("%d",timer), WIDTH/2, HEIGHT/2 + 200)
    end
end

function resetTimer()
    timer=0
end

function touched(t)
    if CurrentTouch.state==BEGAN then
        if CurrentTouch.x>WIDTH/4-150 and CurrentTouch.x<WIDTH/4+150 and CurrentTouch.y>HEIGHT/3-150 and CurrentTouch.y<HEIGHT/3+150 then
            running=true
            btpy1=200
        end
        if CurrentTouch.x>WIDTH/4*3-150 and CurrentTouch.x<WIDTH/4*3+150 and CurrentTouch.y>HEIGHT/3-150 and CurrentTouch.y<HEIGHT/3+150 then
            btpy2=200
            if running==false then
                resetTimer()
            else
                running=false
            end
        end
    end
    if CurrentTouch.state==ENDED then
        btpy1=119
        btpy2=119
    end
end

@dave1707 The inbuilt reference didn’t say much about string.format(). As far as I know, %d returns a whole number, but what is %0.1f?

@Saturn031000 Do a google search for string.format. You’re correct about %d, it shows an integer, but %0.1f will print a floating point number with 1 digit to the right of the decimal point. It will also print 1 zero to the left of the decimal point. String.format is a powerful function that will let you format strings and numbers just about any way you want.

@dave1707 Looked it up. Thanks.

@dave1707 huh, I should have thought about this… I learnt about string formatting in Swift but for decimals in Lua I’ve always used for example:

local n = 10.5325
--1 d.p.
n = math.ceil(n*10)/10
--2 d.p.
n = math.ceil(n*100)/100

Which isn’t as accurate as it always rounds up,

The user and all related content has been deleted.

@Luatee Are you saying you never used the “%f” in string.format. There’s a lot that can be done with it.

@dave1707 Never formatted a string in Lua no, never had a need to… Then again I’ve not read up about it in Lua so I don’t know everything it’s capable. There are probably bits of code I’ve had to hack around where I could have used string.format so thanks for enlightening me on this.

@Luatee You’ve been missing out on a very powerful function. I would suggest that you and others reading this post to learn how to use it. It makes formatting string and numbers extremely easy and there are other uses than for printing.

@Saturn031000 Here’s an example of a stopwatch I wrote back in Nov 2012. See the last program at the link below.

http://codea.io/talk/discussion/1908/counter-simulation-/p1

Nice stopwatch, but why are you using CurrentTouch in function touched(touch)? The touch paramater is exactly why you use touched(touch) instead of CurrentTouch.

@SkyTheCoder — I used CurrentTouch because it can not be in two places at once, as touch can. I put it in the function touched(touch) because I want it to run whenever the screen is touched, not 60 times per second.

@Saturn031000 what SkyTheCoder means is when you have this:

function touched(t)
 CurrentTouch.x 
 t.x
end

Both CurrentTouch.x and t.x are the same thing, except t.x is only executed when the screen is being touched, and CurrentTouch remains as a consistent variable which is why it is hardly used.

@Luatee I never knew that CurrentTouch was a consistent variable. The code in the original post has been updated using t instead of CurrentTouch (I should probably get into the habit of not using CurrentTouch).

CurrentTouch is used when you just want the x,y screen position and you don’t care about the touch starting or ending. CurrentTouch and the function touched both have their uses. You just need to know which one suits the situation you’re coding.

@dave1707 Even using CurrentTouch as a consistent variable (as it’s use is different) I would rather make a variable and update it through the touched function… I don’t find CurrentTouch to be very useful as it will take the latest touch only as well.

@Luatee I agree. I haven’t use CurrentTouch since I understood how to use the “touched” function. But CurrentTouch has it uses and it’s easy to use by the new coders that don’t understand the full capacities of “touched”.

I don’t know about you guys, but when I’m trying to record an app demonstration and my touched function is used up with coding, I do a simple

pushstyle()
Stroke(255)
Fill(180,60,210)
ellipse(CurrentTouch.x, CurrentTouch.y,25)
popstyle()

As the last few lines of my draw function on main.

That’s my perceived only use aha

I just wrote a game using CurrentTouch instead of touch (can be found in the Acorn Mayhem discussion) and that’s a place where touch can not be used.