Results 1 to 10 of 10

Thread: Time function (turning a pc into an alarm clock!)

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Posts
    2

    Smile

    hi chaps,

    i am trying to turn my pc into an expensive alarm clock.

    i am using the Time function to check system timer. the value of Txt_time is to be formatted to get a 6 figure time eg: 18:35:00


    If Time < Val(Txt_time) Then

    Beep
    Beep
    Beep
    Beep
    Beep

    End If

    i would be very grateful if anyone out there could help me.

    cheers james

    Brighton UK

  2. #2
    Guest
    Hi!

    check out the DateDiff and DateAdd functions in VB.

    also instead of doing a row of beeps, try using the API to play a wake-up call sound (you could record it yourself).

  3. #3
    Lively Member
    Join Date
    Mar 2000
    Posts
    103
    This is easy - just set the interval and then when the interval has elapsed, make your noise.

    'set this to whatver you like - this is 24 hours
    Public const INTERVAL = 60000 * 60 * 24


    public sub StartTimer()
    dim theTimer as long

    theTimer = timer

    do until (timer - theTimer) > INTERVAL
    doevents
    loop

    'code to make your noise here - maybe add a mediaplayer
    'control to your form and then do MediaPlayer1.open somemp3


    'start the timer again recursively
    StartTimer
    end sub

    Now just compile the program and run it when you want it to
    start timing. Say you went to bed at midnight and wanted to get up at 7am. Just set the interval to 7 hours. Or you can actually make a clock but I will leave that up to you. The do until loop allows this to run in the background without pummeling your CPU too much.

  4. #4
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    Here is an example that will beep once and display a message box at 7 AM. Here the 07:00:00 AM is hard coded, but it is easy enough to put a control on the form that lets you set the specific time value that you want the alarm to go. Look up some examples of playing wav files, etc. here on VB World for creating a better sounding alarm than the beep. Adding sound files is actually quite easy.

    Hope this helps.



    Code:
    'This sample project just has a single Timer control
    'added to it called Timer1.
    
    Private Sub Form_Load()
        Timer1.interval = 1000
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        Dim curTime As Date
        
        curTime = Format(Now, "hh:mm:ss AMPM")
        If curTime = "07:00:00 AM" Then
            Beep
            MsgBox "Get up"
        End If
    End Sub
    [Edited by jcouture100 on 05-03-2000 at 09:35 PM]
    JC

  5. #5
    Lively Member
    Join Date
    Aug 1999
    Posts
    89
    I don't think i need to give any suggestion any more. Those methods up there will work fine.

    Just feel that your alarm clock is too expensive..haha...

  6. #6
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217

    Post

    Hey, I just noticed this thread now.
    I did something just like this a while ago, so I did some
    digging and found the code, so here it is:
    Code:
    VERSION 5.00
    Begin VB.Form frmMain 
       Caption         =   "Alarm Clock Test"
       ClientHeight    =   3195
       ClientLeft      =   60
       ClientTop       =   345
       ClientWidth     =   4680
       LinkTopic       =   "Form1"
       ScaleHeight     =   3195
       ScaleWidth      =   4680
       StartUpPosition =   2  'CenterScreen
       Begin VB.TextBox txtWakeUp 
          Height          =   375
          Left            =   240
          TabIndex        =   0
          Top             =   360
          Width           =   4215
       End
       Begin VB.CommandButton cmdStop 
          Caption         =   "&Stop"
          Height          =   495
          Left            =   1680
          TabIndex        =   2
          Top             =   1680
          Visible         =   0   'False
          Width           =   1215
       End
       Begin VB.CommandButton cmdSleep 
          Caption         =   "&Sleep"
          Default         =   -1  'True
          Height          =   495
          Left            =   1680
          TabIndex        =   1
          Top             =   1200
          Width           =   1215
       End
       Begin VB.Label lblWakeUp 
          AutoSize        =   -1  'True
          BackStyle       =   0  'Transparent
          Caption         =   "Set Time To Wake Up"
          Height          =   195
          Left            =   240
          TabIndex        =   3
          Top             =   120
          Width           =   1605
       End
    End
    Attribute VB_Name = "frmMain"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    '============================
    '=== GENERAL DECLARATIONS ===
    '============================
    
    '<<< OPTION STATEMENTS >>>
    Option Explicit
    
    
    '<<< CONSTANTS >>>
    Private Const miMAX_DAYS As Integer = 24
    
    Private Const mlMILLISECONDS_IN_SEC As Long = 1000
    Private Const mlSECONDS_IN_MIN As Long = 60
    Private Const mlMINUTES_IN_HOUR As Long = 60
    Private Const mlHOURS_IN_DAY As Long = 24
    
    
    '<<< ENUMERATIONS >>>
    Private Enum eSetWindowPos
       SWP_NOSIZE = &H1
       SWP_NOMOVE = &H2
       SWP_NOZORDER = &H4
       SWP_NOREDRAW = &H8
       SWP_NOACTIVATE = &H10
       SWP_DRAWFRAME = &H20
       SWP_SHOWWINDOW = &H40
       SWP_HIDEWINDOW = &H80
       SWP_NOCOPYBITS = &H100
       SWP_NOREPOSITION = &H200
    End Enum
    
    Private Enum eZOrder
       HWND_TOP = 0
       HWND_BOTTOM = 1
       HWND_NOTOPMOST = -2
       HWND_TOPMOST = -1
    End Enum
    
    
    '<<< API DECLARES >>>
    Private Declare Function apiSetForegroundWindow _
                    Lib "user32" _
                    Alias "SetForegroundWindow" _
                    (ByVal hwnd As Long) As Long
    
    Private Declare Function apiSetWindowPos _
                    Lib "user32" _
                    Alias "SetWindowPos" _
                    (ByVal hwnd As Long, ByVal hWndInsertAfter As eZOrder, _
                    ByVal x As Long, ByVal y As Long, _
                    ByVal cx As Long, ByVal cy As Long, _
                    ByVal wFlags As eSetWindowPos) As Long
    
    Private Declare Sub apiSleep _
                    Lib "kernel32" _
                    Alias "Sleep" _
                    (ByVal dwMilliseconds As Long)
    
    
    '<<< VARIABLES >>>
    Private mbStopAlarm As Boolean
    
    
    '========================
    '=== EVENT PROCEDURES ===
    '========================
    
    Private Sub cmdStop_Click()
       mbStopAlarm = True
    End Sub
    
    Private Sub cmdSleep_Click()
       Dim lSleepTime As Long
       
       mbStopAlarm = False
       lSleepTime = mlGetSleepCount(mdtValidateDate(txtWakeUp.Text))
       Call Me.Hide
       DoEvents
       Call apiSleep(lSleepTime)
       DoEvents
       Call apiSetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, _
                            SWP_NOSIZE Or SWP_NOMOVE Or SWP_SHOWWINDOW)
       Call apiSetForegroundWindow(Me.hwnd)
       Call mSoundAlarm
    End Sub
    
    
    '==========================
    '=== PRIVATE PROCEDURES ===
    '==========================
    
    Private Function mdtValidateDate(ByVal vsDate As String) As Date
       Dim dtReturnDate As Date
       
       If IsDate(vsDate) = True Then
          dtReturnDate = Format$(vsDate, "dd-mmm-yyyy Hh:NnAM/PM")
       Else
          dtReturnDate = Now
       End If
       
       mdtValidateDate = dtReturnDate
    End Function
    
    Private Function mlGetSleepCount(ByVal vdtWakeUpTime As Date) As Long
       Dim lSeconds As Long
       Dim lWakeUpTime As Long
       
       'Maximum size of Long for milliseconds of time difference.
       If DateDiff("d", Now, vdtWakeUpTime) < miMAX_DAYS Then
          lSeconds = DateDiff("s", Now, vdtWakeUpTime)
          
          If lSeconds > 0 Then
             lWakeUpTime = lSeconds * mlMILLISECONDS_IN_SEC
          End If
          
       End If
       
       mlGetSleepCount = lWakeUpTime
    End Function
    
    Private Sub mSoundAlarm()
       cmdSleep.Visible = False
       cmdStop.Visible = True
       Call cmdStop.SetFocus
       
       Do While mbStopAlarm = False
          Beep
          DoEvents
       Loop
       
       cmdSleep.Visible = True
       cmdStop.Visible = False
    End Sub
    What you do is type in the date and time for the app to
    wake up and press the Sleep button. When the time arrives
    the app will popup above all windows and begin beeping
    until the Stop button is pressed. It will even work through
    the Screen Saver.

    This was only a test that I did when developing a scheduler,
    but it sounds like what you were looking for. You should
    probably place the wake up time in the registry and provide
    an app to set this time. Then you could put the alarm clock
    into the StartUp folder. That way the program could read
    the alarm time from the registry when the computer is
    turned on and the app would automatically TSR until the
    desired alarm time. Also, instead of running the Beep loop
    it would be better to play a .wav file looped.

    Sorry to come in so late, just wanted to add my 2¢
    Dan PM
    Analyst Programmer

    VB6 SP3 (also VB4 16-bit sometimes )

  7. #7
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    Question

    Is it possible to set the timer off until the system beep occurs, when rebuild is concluded, the system beeper beeps,

  8. #8
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    I don't know whether you're a globe-trotter or not, but you may find Robert Smith's comments of use. Check his article "Real Power with No NT" under the heading "Multi-User Fun" at http://www.smithvoice.com/vbfun.htm

  9. #9
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    Thumbs down

    It didnt help but thanks.. anyone else?

    do timer1

    'until i say stop
    loop

  10. #10
    Hyperactive Member JazzBass's Avatar
    Join Date
    Jun 1999
    Posts
    393

    Talking Try This

    I think this is what you're looking for.
    I created this so I would not forget to call my wife by a certain time.

    Create a project with a timer (interval = 1000), 1 form, 1 module, 1 textbox, 4 labels (lblStartTime, lblEndTime, label1 "Start Time" and label2 "End Time")



    Code:
    Module Code(Paste in Declarations section:)
    
    Global starttime
    Global endtime
    
    
    Declare Function mciExecute Lib "mmsystem"(ByVal_ lpstrCommand As String) As Integer
    
    
    
    
    Sub Command1_Click
    starttime = Format(Time, "hh:mm")
    
    endtime = Format(text1.Text, "hh:mm")
    
    lblStartTime.Caption = starttime
    lblEndTime.Caption = endtime
    timer1.Enabled = True
    Me.WindowState = 1
    Me.Caption = "End time: " & endtime
    
    
    
    
    Sub Timer1_Timer
    starttime = Format(Time, "hh:mm")
    If starttime = endtime Then
    x = mciExecute("play C:\winnt\media\passport.mid")
    Me.Caption = "Wife's Wakeup Timer"
    DoEvents
    Me.WindowState = 0
    DoEvents
    AppActivate ("Wife's Wakeup Timer")
    DoEvents
    
    MsgBox "The time has come to call your wife!", 64, "Time has come"
    timer1.Enabled = False
    x = mciExecute("Stop C:\winnt\media\passport.mid")
    
    Exit Sub
    
    ElseIf starttime > endtime Then
    AppActivate ("Wife's Wakeup Timer")
    MsgBox "The time has come to call your wife!", 64, "Time has come and passed"
    x = mciExecute("play C:\winnt\media\passport.mid")
    timer1.Enabled = False
    x = mciExecute("Stop C:\winnt\media\passport.mid")
    Me.WindowState = 0
    Exit Sub
    Else
    End If
    That should be it. You type in the time you want to wakeup and click on the command button. It automatically minimizes and when the time comes, it maximizes, displays, and plays a midi file.

    Please keep in mind this was written in VB3. Make any changes you need.

    Hope that helps.

    Let us know.

    JazzBass
    JazzBass
    In the .NET era
    Trying to remember VB6
    Progress:
    XP Professional @ Home
    and @ the Office

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width