-
Mar 3rd, 2012, 11:13 AM
#1
Thread Starter
New Member
A little help with timer issue
Hi
I`m fairly new to coding (2 weeks) and having some trouble with a timer application to control a greenhouse misting system. The problem arose after resolving the 65 second interval limitation for tmrPause, it appears the tmrPulse line is never reached as lblCycleStatus.Text doesnt display "Pulse" where it did before. I may have bitten off more than i can chew for a first project but i`d appreciate any pointers as to where i went wrong so i can learn from it. Thanks in advance.
Code:
Public Class Form1
Dim Pause As Integer
Private lngTimer As Long
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.s1hr
TextBox2.Text = My.Settings.s1min
TextBox9.Text = My.Settings.s1pulse
TextBox10.Text = My.Settings.s1pause
TextBox3.Text = My.Settings.s2hr
TextBox4.Text = My.Settings.s2min
TextBox11.Text = My.Settings.s2pulse
TextBox12.Text = My.Settings.s2pause
TextBox5.Text = My.Settings.s3hr
TextBox6.Text = My.Settings.s3min
TextBox13.Text = My.Settings.s3pulse
TextBox14.Text = My.Settings.s3pause
TextBox7.Text = My.Settings.s4hr
TextBox8.Text = My.Settings.s4min
TextBox15.Text = My.Settings.s4pulse
TextBox16.Text = My.Settings.s4pause
Timer1.Enabled = True
Timer1.Interval = 1
lngTimer = 0
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = TimeOfDay 'this displays system clock time
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
If btnStart.Text = "Start" Then 'single button for start and stop
btnStart.Text = "Stop"
timPause.Enabled = True
Timer2.Enabled = True
Else
btnStart.Text = "Start"
Select Case True
Case timPause.Enabled
timPause.Enabled = False
Case timPulse.Enabled
timPulse.Enabled = False
End Select
End If
End Sub
Private Sub timPause_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timPause.Tick
lblCycleStatus.Text = "PAUSE"
timPause.Enabled = True
lngTimer = lngTimer + 1 'increase the counter
Label8.Text = (lngTimer)
'exit if it isn't high enough yet
If lngTimer <= Pause Then Exit Sub ' Pause is the variable for pause time in seconds
lngTimer = 0 'reset so that the full delay will happen again
End Sub
Private Sub timPulse_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timPulse.Tick
timPulse.Enabled = True 'add digital output code for "relay on" to this line
lblCycleStatus.Text = "PULSE"
timPause.Enabled = False
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If TimeOfDay = (TextBox1.Text) & ":" & (TextBox2.Text) & ":00 " Then 'this timer compares schedule time with system time
timPulse.Interval = TextBox9.Text * 1000 'pulse time from schedule 1
Pause = TextBox10.Text 'pause time from schedule 1
Label4.Text = "Active"
Label5.Text = ""
Label6.Text = ""
Label7.Text = ""
End If
If TimeOfDay = (TextBox3.Text) & ":" & (TextBox4.Text) & ":00 " Then
timPulse.Interval = TextBox11.Text * 1000 'pulse time from schedule 2
Pause = TextBox12.Text 'pause time from schedule 2
Label5.Text = "Active"
Label4.Text = ""
Label6.Text = ""
Label7.Text = ""
End If
If TimeOfDay = (TextBox5.Text) & ":" & (TextBox6.Text) & ":00 " Then
timPulse.Interval = TextBox13.Text * 1000 'pulse time from schedule 3
Pause = TextBox14.Text 'pause time from schedule 3
Label6.Text = "Active"
Label4.Text = ""
Label5.Text = ""
Label7.Text = ""
End If
If TimeOfDay = (TextBox7.Text) & ":" & (TextBox8.Text) & ":00 " Then
timPulse.Interval = TextBox15.Text * 1000 'pulse time from schedule 4
Pause = TextBox16.Text 'pause time from schedule 4
Label7.Text = "Active"
Label4.Text = ""
Label5.Text = ""
Label6.Text = ""
End If
If TimeOfDay >= (TextBox1.Text) & ":" & (TextBox2.Text) & ":00 " And TimeOfDay <= (TextBox3.Text) & ":" & (TextBox4.Text) & ":00 " Then 'this compares schedules 1 & 2 to system time at startup
timPulse.Interval = (TextBox9.Text) * 1000 'if start time falls between schd1 and schd2 then use schd1 cycle timing
Pause = (TextBox10.Text)
End If
If TimeOfDay >= (TextBox3.Text) & ":" & (TextBox4.Text) & ":00 " And TimeOfDay <= (TextBox5.Text) & ":" & (TextBox6.Text) & ":00 " Then 'this compares schedules 2 & 3 to system time at startup
timPulse.Interval = (TextBox11.Text) * 1000
Pause = (TextBox12.Text)
End If
If TimeOfDay >= (TextBox5.Text) & ":" & (TextBox6.Text) & ":00 " And TimeOfDay <= (TextBox7.Text) & ":" & (TextBox8.Text) & ":00 " Then 'this compares schedules 3 & 4 to system time at startup
timPulse.Interval = (TextBox13.Text) * 1000
Pause = (TextBox14.Text)
End If
If TimeOfDay >= (TextBox7.Text) & ":" & (TextBox8.Text) & ":00 " And TimeOfDay <= (TextBox1.Text) & ":" & (TextBox2.Text) & ":00 " Then 'this compares schedules 4 & 1 to system time at startup
timPulse.Interval = (TextBox15.Text) * 1000
Pause = (TextBox16.Text)
End If
If TimeOfDay >= (TextBox7.Text) & ":" & (TextBox8.Text) & ":00 " And TimeOfDay >= (TextBox1.Text) & ":" & (TextBox2.Text) & ":00 " Then 'this also compares schedules 4 & 1 to system time at startup
timPulse.Interval = (TextBox15.Text) * 1000
Pause = (TextBox16.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
My.Settings.s1hr = TextBox1.Text
My.Settings.s1min = TextBox2.Text
My.Settings.s1pulse = TextBox9.Text
My.Settings.s1pause = TextBox10.Text
My.Settings.s2hr = TextBox3.Text
My.Settings.s2min = TextBox4.Text
My.Settings.s2pulse = TextBox11.Text
My.Settings.s2pause = TextBox12.Text
My.Settings.s3hr = TextBox5.Text
My.Settings.s3min = TextBox6.Text
My.Settings.s3pulse = TextBox13.Text
My.Settings.s3pause = TextBox14.Text
My.Settings.s4hr = TextBox7.Text
My.Settings.s4min = TextBox8.Text
My.Settings.s4pulse = TextBox15.Text
My.Settings.s4pause = TextBox16.Text
End Sub
End Class
-
Mar 3rd, 2012, 11:42 AM
#2
Re: A little help with timer issue
I haven't looked at all your code but I see your using Timer1.Interval = 1. That is 1ms! The vb timer is not that accurate, most recommend not setting vb timers below 50ms, so you might want to consider setting your timer intervals to a minimum of 100 for starters.
-
Mar 3rd, 2012, 12:02 PM
#3
Re: A little help with timer issue
1)Timer1.Interval has no need to be even as fast as 100. I suspect you might have thought that interval was in seconds, in which case you want 1000, but considering what you are doing with that time, you might go for something faster, such as 500.
2)You really should turn Option Strict ON and fix the resulting problems. You have a series of implicit conversions where you multiply strings by numbers. For that to work, the string has to be implicitly turned into a number, and implicit conversions are both risky and slow. Option Strict ON will teach you better habits by forcing you to do proper explicit conversions rather than rely on implicit conversions. I'd say that you might look up Integer.TryParse to fix some of the errors you will have, but the next point may change that.
3) You appear to be using lots of textboxes for situations where the user won't be entering anything. If that is the case, you should use labels. I may be mistaken on some of your textboxes, as they may really be for input. If that is the case, then you will need to look up Integer.TryParse for the conversions (just try leaving one of those textboxes blank and you will see why, implicit conversions or not). If you are converting strings that are NOT user input, you can use CInt, which is easier to implement, and faster, than TryParse, but it will throw exceptions if it gets the wrong input.
4) You are doing some odd things with starting and stopping the timers. I see no point in enabling the timer in the timer.Tick event, especially on the first line. The timer must have been enabled to reach that line. This suggests that you are either enabling the wrong timer, or thinking about something different.
Those are my initial thought. Gotta run, too.
My usual boring signature: Nothing
-
Mar 3rd, 2012, 12:18 PM
#4
Thread Starter
New Member
Re: A little help with timer issue
Hi Edgemeal, Shaggy
I`ve set the interval for 500 now as shaggy suggested, it handles the Timeof day event (system clock).
The basic overview of the program is it compares the system clock against 4 user adjustable timing schedules and sets the mist pulse/pause time accordingly. The textboxes are hr, minute, mist pulse duration (in seconds which are further converted into milliseconds) and pause duration (seconds, converted to "Pause" variable)
lngTimer is incrementing by 1 second as i added a label to monitor the count, it appears to loop back to 0 without reaching the timPulse_Tick line as "Pulse" is not being displayed in lblCycleStatus label.
Last edited by Bob_uk; Mar 3rd, 2012 at 02:34 PM.
-
Mar 3rd, 2012, 02:39 PM
#5
Re: A little help with timer issue
This is going to cause trouble:
If lngTimer <= Pause Then Exit Sub ' Pause is the variable for pause time in seconds
lngTimer = 0 'reset so that the full delay will happen again
Those two lines look ok, but they aren't. If the lngTimer is below Pause, nothing happens to it. If it = Pause, or is larger, then it will be set back to 0. The result is the same, frankly, unless you somewhere compare lngTimer to 0, but you don't. This will keep looping forever with lngTimer being set back to 0 periodically, nothing more. I suspect you want to disable the timer, but I haven't looked thoroughly.
My usual boring signature: Nothing
-
Mar 3rd, 2012, 05:08 PM
#6
Re: A little help with timer issue
vb Code:
Select Case True Case timPause.Enabled timPause.Enabled = False Case timPulse.Enabled timPulse.Enabled = False End Select
Not a great use of Select Case.
You only need to use
and take out the Select Case in your btnStart_Click()
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
|