Author Topic: Countdown timer  (Read 72 times)

Charlie

  • Newbie
  • *
  • Posts: 9
Countdown timer
« on: May 09, 2025, 06:45:17 AM »
Hello,

I needed some help making a countdown timer.  I wanted to make two separate voice activated countdown timers.  One that countdowns from 1 minute and another that countdowns from 30 seconds.  For instance, I would want to say something like "start 1" and have it countdown and then readback 5,4,3,2,1 in the last 5 seconds and then alert.  Then would need the timer to reset so that I can use again when needed.  Is there a certain script I can follow?


SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 333
  • Upstanding Lunatic
    • My AVCS Homepage
Re: Countdown timer
« Reply #1 on: May 09, 2025, 12:40:42 PM »
That would look like this:




First, we set the command phrase to be spoken - since you'd like your timer to act upon a 1 minute or 30 second max time, you can make this part of the command phrase.  I have found that it helps to account for the word form of a number under ten, but also just to cover all bases, 30 as thirty, too.

Next, we want some UX feedback that the timer has started, so we can use a 'Say Something with Text to Speech' action, with a phrase along the lines of "Countdown timer started" ... or whatever you want -- could be replaced with an audio cue from a sound file if you prefer... something to tell the user (after they speak this command) that it was recognized and so they know a countdown has started.

Next, we grab what was said using the text token "{CMDSEGMENT:1}" which returns the value of the portion within the square brackets at position 1 (where "Start" is position zero).  You can learn more about this text token in the VA Manual - press F1 when VA is open/in focus to open the VA Manual, starting around page 164 -- and this token around page 167.

This will always evaluate to "1" or "one" or "30" or "thirty".  Next, we compare this value to text - checking if it equals "1" or "one" and setting our temporary integer variable (prefaced with a "~" so it is destroyed when this command ends) to 60 for a 60 second countdown.... else if it does not equal "1" or "one", set that integer variable to 30 (seconds).

Next, we want to being a "for" loop which will take our "~maxTime" integer variable as the starting number, with "6" as the ending number, and the only instruction in the loop to pause for 1 second.



Finally, we add yet another "for" loop to handle 5 to 1, so these can be spoken, and we do this by adding another temporary integer variable to keep track of the current value during this loop "~timerIndex" - the actions inside this loop should be a 'Say Something with Text to Speech' action, using the text token to turn our integer variable into a word it can speak via the {INT} token:  {INT:~timerIndex} -- and lastly, another 1 second pause to ensure this loop iterates only once per second.



One very last thing we should do is check the box (if not already checked) to ensure that other commands can execute while this one is running, as we don't want to lock out all other voice commands for the next 30-60 seconds, of course.


If you want to add an alert, it would be any action inserted at the very bottom of this command, as that action will not be reached or executed until the timer loop(s) above it are completed.  This could be yet another text-to-speech phrase, an audio clip that plays, or even a looping audio clip and a "Wait for Spoken Response" action that lets the user say something which will end the looping audio clip (such as an alarm sound) with a phrase like, "Stop timer" or whatever you want.

______

Note that these variable names "~maxTime" and "~timerIndex" could be anything, and I chose to use camelCase with descriptive names which help us easily discern the purpose/function/meaning of these variables.  Variables are just containers for values of a specific type which we can refer to by a word such as "timerIndex" ... and the preface of "~" means the command can discard both of these once it completes, no need to retain these in memory after the command is done - you get the idea.

The concept of variable scope (such as our use of "~") is detailed in that chapter of the VA Manual starting around page 250 with the chapter title: "Advanced Variable Control (Scope and Events)"


While this command example as I have laid out for you touches on some advanced concepts, they're not completely out in space -- learn more about these things in the following posts:
Control flow (If, Else, ElseIf, Loop, Jump) basics

Variables and tokens summed up

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 333
  • Upstanding Lunatic
    • My AVCS Homepage
Re: Countdown timer
« Reply #2 on: May 09, 2025, 01:03:54 PM »
As a small follow up, one of the neat things about logic and flow is that there is often more than one way to express our goals in the form of actions in a VoiceAttack command, and this is true for most every proper programming language, too.

For example, this also works in the exact same way, but with the logic reordered in a way - an added if-statement within the for-loop, and applying our "~timerIndex" there, so that the current seconds is only spoken when the value is 5 or less.

Basically, once you learn how to utilize these loop actions, if-statements (Begin Condition actions), and variables/tokens, you can approach any goal logically and utilize whatever actions best accomplish your goal in the order you feel is most appropriate:



Charlie

  • Newbie
  • *
  • Posts: 9
Re: Countdown timer
« Reply #3 on: May 10, 2025, 09:33:56 AM »
Thank you so much for putting this together.  I'm going through it now.  Silly question.  I'm getting the error "variable names may not contain colons".  Is there something I'm doing wrong?  Sorry I'm so new at this.


SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 333
  • Upstanding Lunatic
    • My AVCS Homepage
Re: Countdown timer
« Reply #4 on: May 10, 2025, 02:08:12 PM »
Thank you so much for putting this together.  I'm going through it now.  Silly question.  I'm getting the error "variable names may not contain colons".  Is there something I'm doing wrong?  Sorry I'm so new at this.

As noted in my description above:
Quote
Next, we grab what was said using the text token "{CMDSEGMENT:1}" which returns the value of the portion within the square brackets at position 1 (where "Start" is position zero).  You can learn more about this text token in the VA Manual - press F1 when VA is open/in focus to open the VA Manual, starting around page 164 -- and this token around page 167.

For this reason, an "Integer" compare will not work - you must use a "Text" compare as described in the VA Manual in the section for Text (and Text-To-Speech) Tokens:

Charlie

  • Newbie
  • *
  • Posts: 9
Re: Countdown timer
« Reply #5 on: May 10, 2025, 03:19:06 PM »
Ok, it's working beautifully now after using the text token.  Also thanks for the clear explanations, it's really helping me learn.  I appreciate this.