|
-
Aug 21st, 2012, 10:44 PM
#1
Thread Starter
Junior Member
Timer program in the works...need help
Hey all,
I'm wanting to create a timer to start/stop an event at a selected time.
I have most of the code already written but I'm having trouble with the above.
My timer has 8 individual channels to control 8 devices (lights, sprinklers, etc.).
This is my form to give you a better idea:
http://i81.photobucket.com/albums/j2...face/Timer.png
I'm using a timer tick but I'm stuck. My code has taken an ugly turn as I'm using WAY too many If, then statements as you can see.
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Clock.Text = TimeOfDay
Me.Date1.Text = Date.Today.ToString("dddd")
If cmb1day.Text = "Never" Then
Ch1OFF.Enabled = True
GoTo skip1
Else
If btnConnect.Enabled = True Then
Exit Sub
Else
Ch1OFF.Enabled = False
If cmb1day.Text = "Everyday" Then
Dim TimON1 As Date
Dim TimOFF1 As Date
Dim AMPMON1 As String
Dim AMPMOFF1 As String
If ON1AM.Checked = True Then
AMPMON1 = "AM"
Else
AMPMON1 = "PM"
End If
If OFF1AM.Checked = True Then
AMPMOFF1 = "AM"
Else
AMPMOFF1 = "PM"
End If
If ON1AM.Checked = True AndAlso OFF1AM.Checked = True Then
If cmbH1ON.Text > cmbH1OFF.Text Then
If TimeOfDay.ToString.Contains("PM") Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = ("11:59:59 PM")
Else
TimON1 = ("12:00:00 AM") : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
Else
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
ElseIf ON1AM.Checked = True AndAlso OFF1PM.Checked = True Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
ElseIf ON1PM.Checked = True AndAlso OFF1PM.Checked = True Then
If cmbH1ON.Text > cmbH1OFF.Text Then
If TimeOfDay.ToString.Contains("PM") Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = ("11:59:59 PM")
Else
TimON1 = ("12:00:00 AM") : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
Else
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
End If
If ON1PM.Checked = True AndAlso OFF1AM.Checked = True AndAlso TimeOfDay.ToString.Contains("PM") Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = ("11:59:59 PM")
ElseIf ON1PM.Checked = True AndAlso OFF1AM.Checked = True AndAlso TimeOfDay.ToString.Contains("AM") Then
TimON1 = ("12:00:00 AM") : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
If TimON1 < Me.Clock.Text AndAlso TimOFF1 > Me.Clock.Text Then
SerialPort.Write("1")
Ch1OFF.Text = "Ch 1 On"
ElseIf TimOFF1 <> Me.Clock.Text Then
SerialPort.Write("Q")
Ch1OFF.Text = "Ch 1 Off"
End If
End If
If cmb1day.Text = "Sat and Sun" Then
Dim TimON1 As Date
Dim TimOFF1 As Date
Dim AMPMON1 As String
Dim AMPMOFF1 As String
If ON1AM.Checked = True Then
AMPMON1 = "AM"
Else
AMPMON1 = "PM"
End If
If OFF1AM.Checked = True Then
AMPMOFF1 = "AM"
Else
AMPMOFF1 = "PM"
End If
If ON1AM.Checked = True AndAlso OFF1AM.Checked = True AndAlso TimeOfDay.ToString.Contains("AM") Then
If Date.Now.DayOfWeek.ToString = "Saturday" OrElse Date.Now.DayOfWeek.ToString = "Sunday" Then
If cmbH1ON.Text > cmbH1OFF.Text Then
If TimeOfDay.ToString.Contains("PM") Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = ("11:59:59 PM")
Else
TimON1 = ("12:00:00 AM") : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
Else
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
End If
ElseIf ON1AM.Checked = True AndAlso OFF1PM.Checked = True Then
If Date.Now.DayOfWeek.ToString = "Saturday" OrElse Date.Now.DayOfWeek.ToString = "Sunday" Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
ElseIf ON1PM.Checked = True AndAlso OFF1PM.Checked = True Then
If Date.Now.DayOfWeek.ToString = "Saturday" OrElse Date.Now.DayOfWeek.ToString = "Sunday" Then
If cmbH1ON.Text > cmbH1OFF.Text Then
If TimeOfDay.ToString.Contains("PM") Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = ("11:59:59 PM")
Else
TimON1 = ("12:00:00 AM") : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
Else
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
End If
End If
If ON1PM.Checked = True AndAlso OFF1AM.Checked = True AndAlso TimeOfDay.ToString.Contains("PM") Then
If Date.Now.DayOfWeek.ToString = "Saturday" OrElse Date.Now.DayOfWeek.ToString = "Sunday" Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = TimeValue("11:59:59 PM")
End If
End If
If ON1PM.Checked = True AndAlso OFF1AM.Checked = True AndAlso TimeOfDay.ToString.Contains("AM") Then
If Date.Now.DayOfWeek.ToString = "Sunday" OrElse Date.Now.DayOfWeek.ToString = "Monday" Then
TimON1 = ("12:00:00 AM") : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
End If
If TimON1 < Me.Clock.Text AndAlso TimOFF1 > Me.Clock.Text Then
SerialPort.Write("1")
Ch1OFF.Text = "Ch 1 On"
ElseIf TimOFF1 <> Me.Clock.Text Then
SerialPort.Write("Q")
Ch1OFF.Text = "Ch 1 Off"
End If
End If
If cmb1day.Text = "M-F" Then
Dim TimON1 As Date
Dim TimOFF1 As Date
Dim AMPMON1 As String
Dim AMPMOFF1 As String
If ON1AM.Checked = True Then
AMPMON1 = "AM"
Else
AMPMON1 = "PM"
End If
If OFF1AM.Checked = True Then
AMPMOFF1 = "AM"
Else
AMPMOFF1 = "PM"
End If
If ON1AM.Checked = True AndAlso OFF1AM.Checked = True Then
If Date.Now.DayOfWeek.ToString = "Monday" OrElse Date.Now.DayOfWeek.ToString = "Tuesday" OrElse Date.Now.DayOfWeek.ToString = "Wednesday" OrElse Date.Now.DayOfWeek.ToString = "Thursday" OrElse Date.Now.DayOfWeek.ToString = "Friday" AndAlso TimeOfDay.ToString.Contains("AM") Then
If cmbH1ON.Text > cmbH1OFF.Text Then
If TimeOfDay.ToString.Contains("PM") Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = ("11:59:59 PM")
Else
TimON1 = ("12:00:00 AM") : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
Else
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
End If
ElseIf ON1AM.Checked = True AndAlso OFF1PM.Checked = True Then
If Date.Now.DayOfWeek.ToString = "Monday" OrElse Date.Now.DayOfWeek.ToString = "Tuesday" OrElse Date.Now.DayOfWeek.ToString = "Wednesday" OrElse Date.Now.DayOfWeek.ToString = "Thursday" OrElse Date.Now.DayOfWeek.ToString = "Friday" Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
ElseIf ON1PM.Checked = True AndAlso OFF1PM.Checked = True Then
If Date.Now.DayOfWeek.ToString = "Monday" OrElse Date.Now.DayOfWeek.ToString = "Tuesday" OrElse Date.Now.DayOfWeek.ToString = "Wednesday" OrElse Date.Now.DayOfWeek.ToString = "Thursday" OrElse Date.Now.DayOfWeek.ToString = "Friday" Then
If cmbH1ON.Text > cmbH1OFF.Text Then
If TimeOfDay.ToString.Contains("PM") Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = ("11:59:59 PM")
Else
TimON1 = ("12:00:00 AM") : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
Else
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
End If
End If
If ON1PM.Checked = True AndAlso OFF1AM.Checked = True AndAlso TimeOfDay.ToString.Contains("PM") Then
If Date.Now.DayOfWeek.ToString = "Monday" OrElse Date.Now.DayOfWeek.ToString = "Tuesday" OrElse Date.Now.DayOfWeek.ToString = "Wednesday" OrElse Date.Now.DayOfWeek.ToString = "Thursday" OrElse Date.Now.DayOfWeek.ToString = "Friday" Then
TimON1 = TimeValue(cmbH1ON.Text & ":" & cmbM1ON.Text & ":00" & AMPMON1) : TimOFF1 = ("11:59:59 PM")
End If
End If
If ON1PM.Checked = True AndAlso OFF1AM.Checked = True AndAlso TimeOfDay.ToString.Contains("AM") Then
If Date.Now.DayOfWeek.ToString = "Tuesday" OrElse Date.Now.DayOfWeek.ToString = "Wednesday" OrElse Date.Now.DayOfWeek.ToString = "Thursday" OrElse Date.Now.DayOfWeek.ToString = "Friday" OrElse Date.Now.DayOfWeek.ToString = "Saturday" Then
TimON1 = ("12:00:00 AM") : TimOFF1 = TimeValue(cmbH1OFF.Text & ":" & cmbM1OFF.Text & ":00" & AMPMOFF1)
End If
End If
If TimON1 < Me.Clock.Text AndAlso TimOFF1 > Me.Clock.Text Then
SerialPort.Write("1")
Ch1OFF.Text = "Ch 1 On"
ElseIf TimOFF1 <> Me.Clock.Text Then
SerialPort.Write("Q")
Ch1OFF.Text = "Ch 1 Off"
End If
End If
' the rest of the days Monday - Sunday something like the above
skip1:
' Repeated code for each channel
skip2:
.....
skip8:
Can someone help me simplify and keep the rest of my code able to cope?
-
Aug 22nd, 2012, 10:12 AM
#2
Re: Timer program in the works...need help
One thing that I noticed was...
I know some people that won't help you if you have GoTo in your vb.net code. Which even if you do use the GoTo skip1, if you're telling it to go back to the If cmb1day.Text = "Never" then it would just be an infinate loop and you'd never get anywhere, kinda like running around in a circle very fast without getting dizzy or falling down.
Also, with this being in a Timer's_Tick event, you have to keep in mind that this code will run every single time that the timer is ticking. I'm going to assume that you have the interval set at 1000 ms(1 second). If that's the case then your program will execute all of these if/thens, GoTo's, ect. every second until it's told to stop.
Something else I noticed was...
Code:
cmbH1ON.Text > cmbH1OFF.Text
That should've raised an error, because how can one string be greater than another and vice/versa? Considering that you have that as a condition to be met, I'm sure that is throwing your whole if/then ladder off.
My suggestions for you are to:
1. Get rid of the GoTo statements
2. Does this absolutely need to be in a timer? If not then I would put it into a sub and call that sub when necessary
3. Turn Option Strict/Explicit On to work out some of the errors that can be easily worked out like the string > string example mentioned above
I'm sure that there are other things that need to be worked out in this code, but those first 3 are some urgent corrections that should be done.
-
Aug 22nd, 2012, 10:20 AM
#3
Hyperactive Member
Re: Timer program in the works...need help
Rico
Using: VB.net & MS SQL
-
Aug 22nd, 2012, 10:50 AM
#4
Re: Timer program in the works...need help
This code exhibits large amounts of duplication. Take, for example, the 'If cmb1day.Text = "Sat and Sun" Then ...' block and compare it to the 'If cmb1day.Text = "M-F" Then ...' block. They appeat to be exactly the same apart from the line 'If Date.Now.DayOfWeek.ToString = ...'
This is begging to be extracted to its own method that takes the result of the expression as a parameter, for instance.
I'd take it further though and look for chances to extract entire classes. I think there's some concept buried in there (possibly called something like a TimerSwitch?) that could have one class for each of 'Never', 'Everyday', 'Weekday' and 'Weekend' that implement a common interface (probably a base class that contains all the common code that there appears to be). These can then be treated polymorphically, and you ask the instance to do the right thing, and you don't need all that logic in the UI.
-
Aug 22nd, 2012, 07:56 PM
#5
Thread Starter
Junior Member
Re: Timer program in the works...need help
 Originally Posted by dday9
One thing that I noticed was...
I know some people that won't help you if you have GoTo in your vb .net code. Which even if you do use the GoTo skip1, if you're telling it to go back to the If cmb1day.Text = "Never" then it would just be an infinate loop and you'd never get anywhere, kinda like running around in a circle very fast without getting dizzy or falling down.
Actually the is the beginning of code for the next channel to be conditional tested.
Something else I noticed was...
Code:
cmbH1ON.Text > cmbH1OFF.Text
That should've raised an error, because how can one string be greater than another and vice/versa? Considering that you have that as a condition to be met, I'm sure that is throwing your whole if/then ladder off.
Actually, this works quite well since the strings are numbers. In VB coding, the string value is based on text sort order.
My suggestions for you are to:
1. Get rid of the GoTo statements
.. and use what instead?
2. Does this absolutely need to be in a timer? If not then I would put it into a sub and call that sub when necessary
How would I trigger a call to a sub if I need to constantly check the time for a trigger to run the sub?
3. Turn Option Strict/Explicit On to work out some of the errors that can be easily worked out like the string > string example mentioned above
Tried that, but I get stupid errors on coding from examples I got directly from http://msdn.microsoft.com library.
-
Aug 22nd, 2012, 08:02 PM
#6
Thread Starter
Junior Member
Re: Timer program in the works...need help
 Originally Posted by Evil_Giraffe
This code exhibits large amounts of duplication. Take, for example, the 'If cmb1day.Text = "Sat and Sun" Then ...' block and compare it to the 'If cmb1day.Text = "M-F" Then ...' block. They appeat to be exactly the same apart from the line 'If Date.Now.DayOfWeek.ToString = ...'
This is begging to be extracted to its own method that takes the result of the expression as a parameter, for instance.
I'd take it further though and look for chances to extract entire classes. I think there's some concept buried in there (possibly called something like a TimerSwitch?) that could have one class for each of 'Never', 'Everyday', 'Weekday' and 'Weekend' that implement a common interface (probably a base class that contains all the common code that there appears to be). These can then be treated polymorphically, and you ask the instance to do the right thing, and you don't need all that logic in the UI.
Can you give an example. I'm not familiar with any of those terms. If you can't tell, I just sort of jumped into coding and just happened to get some things to 'work' for a lack of a better word.
-
Aug 22nd, 2012, 08:02 PM
#7
Thread Starter
Junior Member
Re: Timer program in the works...need help
-
Aug 22nd, 2012, 09:20 PM
#8
Addicted Member
Re: Timer program in the works...need help
Hi, I had a timing application once where I needed the flexibility of adding or removing "channels". I went the route of a UserControl with one UserControl representing one channel.
To give you an idea of how I compared the set time to current time and raised an event when the set time was reached here is the basic starting point for a UserControl. Add the following to a regular Windows form application that contains one label and one timer with the default values.
Code:
Public Event Minute_elapsed(ByVal e As System.EventArgs)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Tim_now As String
Tim_now = Format(Now, "h:mmtt").ToUpper()
If Tim_now <> Label1.Text Then
Label1.Text = Tim_now
RaiseEvent Minute_elapsed(New System.EventArgs)
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Tim As String
Tim = Format(Now, "h:mmtt").ToUpper()
Label1.Text = Tim
Timer1.Enabled = True
End Sub
Private Sub Form1_Minute_elapsed(ByVal e As System.EventArgs) Handles Me.Minute_elapsed
MsgBox("One Minute Has Elapsed")
End Sub
If this looks like something that would work for you then transfer it to a UserControl with just a few minor modifications.
Gets rid of the GOTO's and should not give errors with Option Strict/Explicit.
-
Aug 22nd, 2012, 09:37 PM
#9
Thread Starter
Junior Member
Re: Timer program in the works...need help
If I'm not mistaken, that's more for a time elapse scenario? I'm not sure how I can make the changes from that to an ON/OFF timer.
I made a mock up of what a user manual might look like so as to give a better idea of what I'm trying to achieve - http://sparky3489.webs.com/Timer8.pdf
Everything works EXCEPT the few bugs I have in the many possibilities a user can select for ON/OFF times.
This is the entire project start to finish - http://sparky3489.webs.com/pcinterfaceproject.htm
-
Aug 22nd, 2012, 09:40 PM
#10
Re: Timer program in the works...need help
Well, I don't know where to start. I think I will let somebody else take this one for ya ;]
-
Aug 25th, 2012, 08:53 PM
#11
Thread Starter
Junior Member
Re: Timer program in the works...need help
Not everyone at once!!
I can't keep up.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|