Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Countdown timer (kitchen timer)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Countdown timer (kitchen timer)

    A little thingy I wrote for when I'm busy on the computer and need to remind myself to go check the washing machine...

    The structure of the code is a bit odd, but it reduced the code to only what was needed.

    Code:
    '-------------------------------------------------------------------------------
    ' countdowntimer.bas
    '-------------------------------------------------------------------------------
    
    #DIM ALL
    #COMPILE EXE
    #OPTION VERSION4
    
    #INCLUDE "WIN32API.INC"
    
    %ID_TIME = 100
    %ID_BUTTON = 101
    
    DECLARE CALLBACK FUNCTION MainProc()
    
    GLOBAL hDlg AS LONG
    GLOBAL mode AS STRING
    GLOBAL time AS STRING
    GLOBAL minutes AS LONG
    GLOBAL seconds AS LONG
    GLOBAL mm AS STRING
    GLOBAL ss AS STRING
    
    '------------------------------------------------------------------------------
    
    FUNCTION PBMAIN
    
      DIALOG NEW 0, "Countdown Timer 1.0", 0, 0, 150, 35, %DS_CENTER OR %WS_CAPTION _
        OR %WS_SYSMENU OR %WS_MINIMIZEBOX, 0 TO hDlg
      CONTROL ADD TEXTBOX, hDlg, %ID_TIME, "Enter minutes", 10, 10, 60, 12, %WS_BORDER
      CONTROL ADD BUTTON, hDlg, %ID_BUTTON, "Set", 80, 10, 60, 12
      DIALOG SHOW MODELESS hDlg, CALL MainProc
    
      mode = "wait"
      CONTROL SET FOCUS hDlg, %ID_TIME  ' must have focus for "SETSEL" to work
      CONTROL SEND hDlg, %ID_TIME, %EM_SETSEL, 0, -1  ' select text so typing will replace text
    
      WHILE mode = "wait"  ' wait until time set button
        DIALOG DOEVENTS
      WEND
    
      WHILE mode = "run"   ' keep looping until exit
        DIALOG DOEVENTS
    
        ss = RIGHT$(TIME$, 2)        ' save for initial comparison
        WHILE RIGHT$(TIME$, 2) = ss  ' wait for next second
          DIALOG DOEVENTS
        WEND
        ss = RIGHT$(TIME$, 2)        ' then save it for next comparison
    
        seconds = seconds - 1  ' adjust time
        IF seconds < 0 THEN seconds = 59 : minutes = minutes - 1
    
        ' display time
        SetWindowText hDlg, FORMAT$(minutes, "00") + ":" + FORMAT$(seconds, "00")
        SetDlgItemText hDlg, %ID_TIME, FORMAT$(minutes, "00") + ":" + FORMAT$(seconds, "00")
    
        IF (minutes = 0 AND seconds = 0) THEN  ' alarm if timed out
          BEEP
          DIALOG SHOW STATE hDlg, %SW_RESTORE  ' restore window if minimized
    
          WHILE mode = "run"   ' wait for exit button
            DIALOG DOEVENTS
          WEND
    
        END IF
    
      WEND
    
    END FUNCTION
    
    '------------------------------------------------------------------------------
    
    CALLBACK FUNCTION MainProc()
    
      SELECT CASE CBMSG
    
        CASE %WM_COMMAND
    
          SELECT CASE CBCTL
    
            CASE %ID_BUTTON
              IF mode = "wait" THEN  ' time has bet set, time for run mode
                CONTROL GET TEXT hDlg, %ID_TIME TO time  ' get time entered
                IF INSTR(time, ":") = 0 THEN  ' if minutes only entered
                  minutes = VAL(time)
                  seconds = 1
                ELSE                           ' minutes:seconds entered
                  minutes = VAL(LEFT$(time, INSTR(time, ":") - 1))
                  seconds = 1 + VAL(MID$(time, INSTR(time, ":") + 1, 99))
                END IF
                mode = "run"
                CONTROL SET TEXT hDlg, %ID_BUTTON, "Exit"  ' change "set" button to "exit"
              ELSE
                mode = "exit"  ' must be in run mode, time to exit
              END IF
    
          END SELECT
    
        CASE %IDCANCEL
          mode = "exit"
    
      END SELECT  '  CASE CBMSG
    
    END FUNCTION
    Gary Peek, Industrologic, Inc.

  • #2
    I noticed the timer used a lot of CPU time, and a simple solution is to add a sleep in the inner loop as shown below:

    Originally posted by Gary Peek View Post
    Code:
        ss = RIGHT$(TIME$, 2)        ' save for initial comparison
        WHILE RIGHT$(TIME$, 2) = ss  ' wait for next second
          DIALOG DOEVENTS 200        ' 200mSec sleep added here to reduce cpu usage
        WEND
        ss = RIGHT$(TIME$, 2)        ' then save it for next comparison

    Comment


    • #3
      Originally posted by John Gleason View Post
      I noticed the timer used a lot of CPU time, and a simple solution is to add a sleep in the inner loop as shown below:
      Aha, good idea, thank you!
      Gary Peek, Industrologic, Inc.

      Comment

      Working...
      X