Page 1 of 1

Talking Alarm Clock Making a talking alarm clock Rate Topic: -----

#1 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Post icon  Posted 16 December 2009 - 09:12 PM

The Concept
This application will allow the user to enter a string, and a time - when the time is reached, the computer speaks to the user, and reminds them about the alarm. Then, the user is asked to enter a confirmation string if he wants to turn the alarm off - if the user fails to confirm the termination - then, after the user specified deadline (in minutes), the alarm gets really annoying. The application will then speak to the user every 7 seconds, beep, and turn the volume up. The main purpose is to demonstrate how to make an alarm clock.

What you'll learn
By the end of this tutorial, if you already weren't - you should now be experienced with the following things
  • Using the Microsoft Speech Object library to give vocal instructions to your users
  • Adding an icon in the system tray, and making notifications appear from it
  • Various ways on how to make SURE the user sees your form
  • Manipulating strings
  • And more...

The Controls
Here are the controls you'll need:

- Two textboxes; named "AlarmName", and the other "message"
- A DateTimePicker named "DateTimePicker1"
⇒ with property "ShowUpDown" set to "True"
⇒ with property "Format" set to "Time"

- A NumericUpDown named "NumericUpDown1"
⇒ with property "Maximum" set to "5"
⇒ with property "Value" set to "1"

- A Button named "setAlarm"
⇒ with property "Name" set to "Set Alarm"
- A timer named "timer1"
⇒ with property "Interval" set to "25"
- A timer named "timer2"
- A timer named "timer3"
⇒ with property "Interval" set to "7000"
- A bunch of labels to label the textboxes if you want
- A NotifyIcon named "NotifyIcon1"
⇒ Make sure the icon is set to something, anything - any icon

Here is what my UI looks like:
Posted Image

The code

Since this program will talk, we will need to use the Microsoft Speech Object Library - so add a reference to it.
Project > Add Reference... > COM tab > Microsoft Speech Object Library > OK

Next, before your class declaration, import what you have just referenced like so:
imports SpeechLib



Then, declare these variables:
	Dim alarmTime As Date  'The time of the alarm
	Dim deadline As Integer 'The amount of time after which the alarm will become extremely annoying
	Dim messageText As String  'The alarm's message
	Dim alarmNameText As String  'The alarm's name
	Dim speaker As New SpVoice 'This will instantinize a new "voice" for this app to speak through
	Dim alarmIsSet As Boolean = False 'Will determine whether alarm is set or not
	Dim annoyingMode As Boolean = False 'Will force the user to pay attention to what the alarm is telling him to do



Add a quick event handler for the text event of the alarm name's text box changing - so that the message textbox changes with it. This is because most people would want the alarm name and message to be the same... however if they don't - they can change it.

AlarmName's text changed event handler code
   Private Sub AlarmName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlarmName.TextChanged
		message.Text = AlarmName.Text
	End Sub



Code for the "setAlarm" button
Private Sub setAlarm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setAlarm.Click
	alarmIsSet = True
		Timer1.Enabled = True
		Me.Visible = False 'Hides the form

		alarmTime = DateTimePicker1.Value
		deadline = NumericUpDown1.Value
		messageText = message.Text
		alarmNameText = alarmName.Text

		'Convert's the user's "deadline" (minutes) to milliseconds for use in timer2
		Timer2.Interval = deadline * 60 * 1000

		'Demo of how to use NotifyIcons
		NotifyIcon1.BalloonTipTitle = "Alarm Set - - Please make sure your volume is unmuted"
		NotifyIcon1.BalloonTipText = "An Alarm has been set for " & alarmTime & ". Named : " & alarmNameText & ". With the message : " & messageText
		NotifyIcon1.ShowBalloonTip(2000)
	End Sub


- Populates various vars
- Set's timer2's interval to the user's "deadline" specification
- Displays a notify icon balloon in the system tray


Code for Timer1's tick event:
  If My.Computer.Clock.LocalTime.TimeOfDay.ToString.Substring(0, 5) = alarmTime.TimeOfDay.ToString.Substring(0, 5) Then
			speaker.Speak("Don't be alarmed, this is just an alarm ha ha ha.  You have set an alarm named. " & alarmNameText & ". with the message. " & messageText)
			Timer1.Enabled = False

			If InputBox("say 'yes I want to stop this alarm' to stop this alarm").ToLower.Contains("yes i want to stop this alarm") Then
				End
			Else
				speaker.Speak("This will get really annoying in " & deadline & " minutes")
			End If

			Timer2.Enabled = True
		End If



- Sees if the hour/minute on the user's computer's clock matches with his alarm's hour and minute
- If it does, it says a message - which includes the alarm's name and message
- Asks the user to type a confirmation message if he/she wants to end the alarm
- If he/she confirms it, then all is good and the program ends
- If he/she enters something else, then he will be notified again, more rudely this time, after the "deadline" has passed

- Also activates timer 2

Timer 2's code (only happens if user chooses not to cancel first notification)
 Timer2.Enabled = False
		annoyingMode = True
		Me.Visible = True
		Me.WindowState = FormWindowState.Maximized
		Me.TopMost = True

		For a As Integer = 0 To 5 'It seems like there is a max limit to how many controls can be removed from a form at a time o.O, safe way = do it 6 timese
			For Each lol As Control In Me.Controls 'Removes each control on the form
				Me.Controls.Remove(lol)
			Next
		Next

		Timer3.Enabled = True


- Sets annoyingMode true, this is referred to in the FormClosing event, and it will make sure that the form closing event is verified by the user it
- Makes itself visible
- Maximizes itself
- Makes itself topmost - the user is bound to notice it now
- removes each control from itself (we no longer need them)
- Enables timer 3

Timer 3's tick event
		 
	Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
		speaker.Rate = 3 'Makes it speak faster
		speaker.Speak(alarmNameText & " " & messageText)
		Beep() 'Nothing is more annoying than a beep;)
		My.Computer.Keyboard.SendKeys(Keys.VolumeUp) 'Sends a key to turn to volume up
	End Sub


- Makes the speaker speak faster
- Says the message
- Beeps
- Sends a key to turn the volume up

The form's closing event:
  Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
		Me.TopMost = True
		If InputBox("Are you sure you want to close me? Y/N").ToLower = "y" Then
			End
		Else
			e.Cancel = True
			Me.TopMost = False
		End If
	End Sub



The entire code
Imports SpeechLib

Public Class Form1

	Dim alarmTime As Date  'The time of the alarm
	Dim deadline As Integer 'The amount of time after which the alarm will become extremely annoying
	Dim messageText As String  'The alarm's message
	Dim alarmNameText As String  'The alarm's name
	Dim speaker As New SpVoice 'This will instantinize a new "voice" for this app to speak through
	Dim alarmIsSet As Boolean = False 'Will determine whether alarm is set or not
	Dim annoyingMode As Boolean = False

	Private Sub AlarmName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles alarmName.TextChanged
		message.Text = alarmName.Text
	End Sub

	Private Sub setAlarm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setAlarm.Click
		alarmIsSet = True
		Timer1.Enabled = True
		Me.Visible = False 'Hides the form

		alarmTime = DateTimePicker1.Value
		deadline = NumericUpDown1.Value
		messageText = message.Text
		alarmNameText = alarmName.Text

		'Convert's the user's "deadline" (minutes) to milliseconds for use in timer2
		Timer2.Interval = deadline * 60 * 1000

		'Demo of how to use NotifyIcons
		NotifyIcon1.BalloonTipTitle = "Alarm Set - Please make sure your volume is unmuted"
		NotifyIcon1.BalloonTipText = "An Alarm has been set for " & alarmTime & ". Named : " & alarmNameText & ". With the message : " & messageText
		NotifyIcon1.ShowBalloonTip(2000)

	End Sub

  
	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		If My.Computer.Clock.LocalTime.TimeOfDay.ToString.Substring(0, 5) = alarmTime.TimeOfDay.ToString.Substring(0, 5) Then
			speaker.Speak("Don't be alarmed, this is just an alarm ha ha ha.  You have set an alarm named. " & alarmNameText & ". with the message. " & messageText)
			Timer1.Enabled = False
			Timer2.Enabled = True

			If InputBox("say 'yes I want to stop this alarm' to stop this alarm").ToLower.Contains("yes i want to stop this alarm") Then
				End
			Else
				speaker.Speak("This will get really annoying in " & deadline & " minutes")
			End If


		End If
	End Sub


	Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
		Timer2.Enabled = False
		annoyingMode = True
		Me.Visible = True
		Me.WindowState = FormWindowState.Maximized
		Me.TopMost = True

		For a As Integer = 0 To 5 'It seems like there is a max limit to how many controls can be removed from a form at a time o.O, safe way = do it 6 timese
			For Each lol As Control In Me.Controls 'Removes each control on the form
				Me.Controls.Remove(lol)
			Next
		Next

		Timer3.Enabled = True
	End Sub

	
	Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
		speaker.Rate = 3
		speaker.Speak(alarmNameText & " " & messageText)
		Beep() 'Nothing is more annoying than a beep;)
		My.Computer.Keyboard.SendKeys(Keys.VolumeUp)
	End Sub

	Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
		Me.TopMost = True
		If InputBox("Are you sure you want to close me? Y/N").ToLower = "y" Then
			End
		Else
			e.Cancel = True
			Me.TopMost = False
		End If
	End Sub


End Class



Proposed test strategy:
- Set the text and message to a lengthy string that makes sense, like "Go and wash the dishes"
- Change the deadline to a low number, like 1

Enjoy!

Attached File(s)



Is This A Good Question/Topic? 3
  • +

Replies To: Talking Alarm Clock

#2 programmer4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-February 10

Posted 02 March 2010 - 06:01 PM

thanks :w00t:
Was This Post Helpful? 1
  • +
  • -

#3 Guest_saif*


Reputation:

Posted 04 August 2010 - 08:09 AM

You are a wonderful man .. you my thanks and appreciation
:tup: :sweatdrop:
Was This Post Helpful? 0

#4 Herfian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-June 12

Posted 30 June 2012 - 10:14 AM

nice script..

[ask]

But, how get the alarm only by date in database.

[CONDITION]

I'm using :

DateTimePicker1 : is limited date
DateTimePicker2 : to warning alarm before (DateTimePicker1) limited date
CmbBox1 : to give absolute deadline

DateTimePicker2 result = DateTimePicker1 - CmbBox1

So alarm will action in DateTimePicker2 value and will show Message Box warning

How to create this scrip ?

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1