PadCodedocs

A timer

Build a 25-minute focus timer that counts down every second and plays a sound at the end.

What you'll build: a focus timer, like the Pomodoro app. It shows 25:00, counts down each second after you tap Start, pauses and resumes, and plays a sound when it reaches 0:00.

What you'll learn:

  • Run steps every second with Start Timer, and end them with Stop Timer.
  • Write seconds as minutes:seconds with a formula.
  • Use one button for two jobs with an If.
  • Share the end-of-session steps through a reusable action.

#The variables

NameTypeStarting value
secondsLeftWhole Number1500 (25 minutes)
runningTrue / FalseOff
sessionsWhole Number0
soundOnTrue / FalseOn

Make them in Data & Logic with Add variable. Each is named after its type, so tap each name and type the one from the table.

#Steps

#Show the time

  1. Add a Text. Next to its Content, tap ⚡︎ → Formula… and type:

    (secondsLeft < 600 ? '0' : '') + fixed(floor(secondsLeft / 60), 0) + ':' + (secondsLeft % 60 < 10 ? '0' : '') + fixed(secondsLeft % 60, 0)

  2. Give it a big Font, and add the Even-Width Numbers modifier so the digits don't jump around as they change.
  3. Add a second Text for the status. Next to Content, tap ⚡︎ → Formula… and type:

    running ? 'Focusing' : (secondsLeft < 1500 ? 'Paused' : 'Ready')

The canvas shows 25:00 and Ready.

#Finish a session

These steps run when the time runs out. They're a reusable action so the timer can run them.

  1. In Data & Logic, under Reusable actions, tap New action. Add these steps in the sheet that opens:
StepSettings
Stop TimerId focus
Set ValueState running, To off
Set ValueState sessions, To ⚡︎ → Formula… sessions + 1
IfCondition soundOn. Under Then: Play Sound, Sound Alarm.
Play HapticStyle Success
Show MessageText Session done. Take a 5-minute break., Style Success, Seconds 5
Reset ValueState secondsLeft
  1. Tap Done, then tap the action's name, newAction, and change it to finishSession.

#Start and pause

  1. Add a Button. Set its Title to:

    {running ? 'Pause' : (secondsLeft < 1500 ? 'Resume' : 'Start')}

  2. In its Actions tab, tap Add action → If and set Condition to running.
  3. Under Then (the timer is running, so pause it), add:
    • Stop Timer, Id focus.
    • Set Value, State running, To off.
  4. Under Else (it isn't running, so start it), add:
    • Set Value, State running, To on.
    • Start Timer, Id focus, Every 1. Under its Do, add:
      • Set Value, State secondsLeft, To ⚡︎ → Formula… max(secondsLeft - 1, 0).
      • If, Condition secondsLeft == 0. Under Then: Run Action, Action finishSession.
A Start / pause button's Actions tab: When Tapped, then one If step whose condition is running.

#Reset and sound

  1. Add a second Button titled Reset, with these steps: Stop Timer (Id focus), Set Value (running, off), Reset Value (secondsLeft).
  2. Add a Switch. Set its Title to Play sound at the end and its Is On to soundOn.
  3. Optional: add a Text with {sessions} sessions done.

Tap Preview and Start. To try the end without waiting 25 minutes, set secondsLeft's starting value to 5 for a moment.

#How it works

  • Start Timer waits Every seconds, runs its Do steps, and repeats until a Stop Timer with the same Id ends it. The first run comes one second after the tap. Starting a timer whose Id is already running replaces it.
  • Each second, secondsLeft drops by one; max(…, 0) keeps it from going below zero. When it reaches 0, the If runs finishSession, which stops the timer, counts the session and plays the sound if soundOn is on.
  • Reset Value puts secondsLeft back to its starting value, 1500.
  • The time formula: floor(secondsLeft / 60) is the whole minutes and secondsLeft % 60 the seconds left over. fixed(…, 0) writes each without decimals, and the two ? '0' : '' parts add a leading zero, so 65 seconds reads 01:05.
  • One button, two jobs: the If looks at running, so the same tap pauses a running timer and starts a stopped one. The title uses the same idea.

#See also