Timers don't work right

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2006
Posts: 181
Reputation: PcPro12 is an unknown quantity at this point 
Solved Threads: 6
PcPro12 PcPro12 is offline Offline
Junior Poster

Timers don't work right

 
0
  #1
Oct 20th, 2009
So I have a question about timers and need some help...

I'm still learning VB.NET programming, so I'm a beginner here...but so far, with timers and all, I've been able to do them well. My last assignment dealing with timers went very smooth...one of the things was to create timers to change the background color of the form and loop through several colors every 5 seconds, than have a button to change the timer intervals to 2 seconds. I did everything, and it worked.

Than came the next assignment. Traffic Lights. I have to have 3 timers, one for the red light, one for yellow, and one for green. I used pictures are my traffic lights. Here is how the timers have to go, how the lights should come on, and for how long.

Red light has to stay on for 20 seconds.
Than green light comes next, for 16 seconds.
Than yellow light comes after that, for 4 seconds.

Than the entire thing has to loop again, starting from red until the user clicks STOP.

With my earlier timer assignment, and this one, they seem to be closely related and takes about the same thing and looks to be done the same way. But, I was wrong in thinking that OR I'm doing something wrong with the timers.

When I click the start button, the red timer is enabled. Here is the timer settings. I specified these settings in the design view under the timer settings. So I didn't write it in the code area.

TM = Timer
R=Red
G=Green
Y=Yellow

TMR=TimerRed
TMG=TimerGreen
TMY=TimerYellow

  1. TMR.Interval = 20000 '(20 seconds)
  2. TMG.Interval = 16000 '(16 seconds)
  3. TMY.Interval = 4000 '(4 seconds)
I also have 4 images on the form. And here are their names.

TL=Traffic Light
TLR = Traffic Light Red
TLG = Traffic Light Green
TLY = Traffic Light Yellow

TL is on top, and everything is blank, than TLR is the same TL image, except with a red circle for the red light, and so on.

So TMR HAS to display TLR for 20 seconds, than go on to TMG and display TLG for 16 seconds, than go on to TMY and display TLY for 4 seconds, than start again at TMR and display TLR for 20 seconds, etc.....

But it's not working like that. I coded my start button to display TLR AND start the TMR timer, which again, is set to display TLR again. The reason I told my button to display TLR is because when I start the TMR timer, it waits 16-20 seconds before it starts displaying TLR...but I wanted TLR to be displayed once the button was clicked, so it doesn't throw the user off thinking it doesn't work.

So, here is what the program is actually doing. When I click start, TMR starts, BUT, TLR is displayed for 16 seconds, TMG is displayed for 4 seconds, and TLY is displayed for 20 seconds. The timer interval from Red went to yellow, from yellow it went to green, and from green it went to red. So basically, the timer intervals went one up. I can't figure out why it did that.

I than tried using a label and colors and doing the same thing, instead of using images. And yet again, same problem. BUT, in my earlier program, everything worked fine, the color changed in the right order.

So here is my source code for the program.

  1. Public Class Form1
  2. Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6. Public Sub New()
  7. MyBase.New()
  8.  
  9. 'This call is required by the Windows Form Designer.
  10. InitializeComponent()
  11.  
  12. 'Add any initialization after the InitializeComponent() call
  13.  
  14. End Sub
  15.  
  16. 'Form overrides dispose to clean up the component list.
  17. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18. If disposing Then
  19. If Not (components Is Nothing) Then
  20. components.Dispose()
  21. End If
  22. End If
  23. MyBase.Dispose(disposing)
  24. End Sub
  25.  
  26. 'Required by the Windows Form Designer
  27. Private components As System.ComponentModel.IContainer
  28.  
  29. 'NOTE: The following procedure is required by the Windows Form Designer
  30. 'It can be modified using the Windows Form Designer.
  31. 'Do not modify it using the code editor.
  32. Friend WithEvents TLR As System.Windows.Forms.PictureBox
  33. Friend WithEvents TLY As System.Windows.Forms.PictureBox
  34. Friend WithEvents TLG As System.Windows.Forms.PictureBox
  35. Friend WithEvents TL As System.Windows.Forms.PictureBox
  36. Friend WithEvents btnStart As System.Windows.Forms.Button
  37. Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
  38. Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
  39. Friend WithEvents mnuExit As System.Windows.Forms.MenuItem
  40. Friend WithEvents tmr As System.Windows.Forms.Timer
  41. Friend WithEvents tmg As System.Windows.Forms.Timer
  42. Friend WithEvents tmy As System.Windows.Forms.Timer
  43. Friend WithEvents Button1 As System.Windows.Forms.Button
  44. Friend WithEvents Timer1 As System.Windows.Forms.Timer
  45. Friend WithEvents Timer2 As System.Windows.Forms.Timer
  46. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  47. Me.components = New System.ComponentModel.Container
  48. Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
  49. Me.TLR = New System.Windows.Forms.PictureBox
  50. Me.TLY = New System.Windows.Forms.PictureBox
  51. Me.TLG = New System.Windows.Forms.PictureBox
  52. Me.TL = New System.Windows.Forms.PictureBox
  53. Me.btnStart = New System.Windows.Forms.Button
  54. Me.MainMenu1 = New System.Windows.Forms.MainMenu
  55. Me.mnuFile = New System.Windows.Forms.MenuItem
  56. Me.mnuExit = New System.Windows.Forms.MenuItem
  57. Me.tmr = New System.Windows.Forms.Timer(Me.components)
  58. Me.tmg = New System.Windows.Forms.Timer(Me.components)
  59. Me.tmy = New System.Windows.Forms.Timer(Me.components)
  60. Me.Button1 = New System.Windows.Forms.Button
  61. Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
  62. Me.Timer2 = New System.Windows.Forms.Timer(Me.components)
  63. Me.SuspendLayout()
  64. '
  65. 'TLR
  66. '
  67. Me.TLR.BackgroundImage = CType(resources.GetObject("TLR.BackgroundImage"), System.Drawing.Image)
  68. Me.TLR.Location = New System.Drawing.Point(48, 16)
  69. Me.TLR.Name = "TLR"
  70. Me.TLR.Size = New System.Drawing.Size(72, 160)
  71. Me.TLR.TabIndex = 0
  72. Me.TLR.TabStop = False
  73. '
  74. 'TLY
  75. '
  76. Me.TLY.BackgroundImage = CType(resources.GetObject("TLY.BackgroundImage"), System.Drawing.Image)
  77. Me.TLY.Location = New System.Drawing.Point(48, 16)
  78. Me.TLY.Name = "TLY"
  79. Me.TLY.Size = New System.Drawing.Size(72, 160)
  80. Me.TLY.TabIndex = 1
  81. Me.TLY.TabStop = False
  82. '
  83. 'TLG
  84. '
  85. Me.TLG.BackgroundImage = CType(resources.GetObject("TLG.BackgroundImage"), System.Drawing.Image)
  86. Me.TLG.Location = New System.Drawing.Point(48, 16)
  87. Me.TLG.Name = "TLG"
  88. Me.TLG.Size = New System.Drawing.Size(72, 160)
  89. Me.TLG.TabIndex = 2
  90. Me.TLG.TabStop = False
  91. '
  92. 'TL
  93. '
  94. Me.TL.BackgroundImage = CType(resources.GetObject("TL.BackgroundImage"), System.Drawing.Image)
  95. Me.TL.Location = New System.Drawing.Point(48, 16)
  96. Me.TL.Name = "TL"
  97. Me.TL.Size = New System.Drawing.Size(72, 160)
  98. Me.TL.TabIndex = 3
  99. Me.TL.TabStop = False
  100. '
  101. 'btnStart
  102. '
  103. Me.btnStart.Location = New System.Drawing.Point(464, 40)
  104. Me.btnStart.Name = "btnStart"
  105. Me.btnStart.TabIndex = 4
  106. Me.btnStart.Text = "Start"
  107. '
  108. 'MainMenu1
  109. '
  110. Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile})
  111. '
  112. 'mnuFile
  113. '
  114. Me.mnuFile.Index = 0
  115. Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuExit})
  116. Me.mnuFile.Text = "File"
  117. '
  118. 'mnuExit
  119. '
  120. Me.mnuExit.Index = 0
  121. Me.mnuExit.Text = "Exit"
  122. '
  123. 'tmr
  124. '
  125. Me.tmr.Interval = 20000
  126. '
  127. 'tmg
  128. '
  129. Me.tmg.Interval = 16000
  130. '
  131. 'tmy
  132. '
  133. Me.tmy.Interval = 4000
  134. '
  135. 'Button1
  136. '
  137. Me.Button1.Location = New System.Drawing.Point(512, 136)
  138. Me.Button1.Name = "Button1"
  139. Me.Button1.TabIndex = 5
  140. Me.Button1.Text = "Button1"
  141. '
  142. 'Timer1
  143. '
  144. Me.Timer1.Interval = 2000
  145. '
  146. 'Timer2
  147. '
  148. Me.Timer2.Interval = 2000
  149. '
  150. 'Form1
  151. '
  152. Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  153. Me.BackColor = System.Drawing.Color.White
  154. Me.ClientSize = New System.Drawing.Size(632, 278)
  155. Me.Controls.Add(Me.Button1)
  156. Me.Controls.Add(Me.btnStart)
  157. Me.Controls.Add(Me.TL)
  158. Me.Controls.Add(Me.TLG)
  159. Me.Controls.Add(Me.TLY)
  160. Me.Controls.Add(Me.TLR)
  161. Me.Menu = Me.MainMenu1
  162. Me.Name = "Form1"
  163. Me.Text = "Traffic Light"
  164. Me.ResumeLayout(False)
  165.  
  166. End Sub
  167.  
  168. #End Region
  169.  
  170. Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
  171. 'hides TL and displayed TR
  172. TL.Visible = False
  173. TLR.Visible = True
  174. 'starts the TMG timer
  175. tmg.Enabled = True
  176. End Sub
  177. Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
  178. 'closes the program
  179. Dim splash As New splash
  180. Me.Close()
  181. splash.Close()
  182. End Sub
  183.  
  184. Private Sub tmr_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr.Tick
  185. 'displays TLR and hides everything else
  186. TLY.Visible = False
  187. TLG.Visible = False
  188. TLR.Visible = True
  189. 'disables this timer and starts TMG
  190. tmr.Enabled = False
  191. tmg.Enabled = True
  192. End Sub
  193.  
  194. Private Sub tmg_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmg.Tick
  195. 'displays TLG and hides everything else
  196. TLY.Visible = False
  197. TLR.Visible = False
  198. TLG.Visible = True
  199. 'disables this timer and starts TMY
  200. tmg.Enabled = False
  201. tmy.Enabled = True
  202. End Sub
  203.  
  204. Private Sub tmy_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmy.Tick
  205. 'displays TLY and hides everything else
  206. TLR.Visible = False
  207. TLG.Visible = False
  208. TLY.Visible = True
  209. 'disables this timer and starts TMR
  210. tmy.Enabled = False
  211. tmr.Enabled = True
  212. End Sub
  213.  
  214. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  215. 'when the form loads, all images except TL are hidden
  216. TLR.Visible = False
  217. TLG.Visible = False
  218. TLY.Visible = False
  219. End Sub
  220. End Class
So far, I've been doing great in programming, in my class, I'm the fastest programmer and easily solve other people's problems, but this problem has really cut me back So can anyone help me? If you need me to clarify my problem more, or provide the images to the application, or anything else. Let me know and I'll be happy to provide anything

Thanks for reading and helping
-PcPro12
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 301
Reputation: TomW is on a distinguished road 
Solved Threads: 43
TomW TomW is offline Offline
Posting Whiz
 
0
  #2
Oct 20th, 2009
I think your confusing yourself with so many different timers. I would suggest only using one timer (interval to one second) and through coding keep track of the elapsed time and which light should be displayed. Also take a look at "StopWatch" in the help file it shows how to get, format & display elapsed time.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 301
Reputation: TomW is on a distinguished road 
Solved Threads: 43
TomW TomW is offline Offline
Posting Whiz
 
1
  #3
Oct 20th, 2009
  1. Private Sub btnStart_Click(...) Handles btnStart.Click
  2.  
  3. If btnStart.Text = "Start" Then
  4. btnStart.Text = "Stop"
  5. lblTrafficLight.Text = "Red"
  6. timRed.Enabled = True
  7. Else
  8. btnStart.Text = "Start"
  9.  
  10. Select Case True
  11. Case timRed.Enabled
  12. timRed.Enabled = False
  13. Case timGreen.Enabled
  14. timGreen.Enabled = False
  15. Case timYellow.Enabled
  16. timYellow.Enabled = False
  17. End Select
  18. End If
  19.  
  20. End Sub
  21.  
  22. Private Sub timRed_Tick(...) Handles timRed.Tick
  23. lblTrafficLight.Text = "Green"
  24. timGreen.Enabled = True
  25. timRed.Enabled = False
  26. End Sub
  27.  
  28. Private Sub timGreen_Tick(...) Handles timGreen.Tick
  29. lblTrafficLight.Text = "Yellow"
  30. timYellow.Enabled = True
  31. timGreen.Enabled = False
  32. End Sub
  33.  
  34. Private Sub timYellow_Tick(...) Handles timYellow.Tick
  35. lblTrafficLight.Text = "Red"
  36. timRed.Enabled = True
  37. timYellow.Enabled = False
  38. End Sub
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: girlwayne is an unknown quantity at this point 
Solved Threads: 1
girlwayne girlwayne is offline Offline
Newbie Poster
 
0
  #4
Oct 20th, 2009
Please dont confuse yourself with different timers as Tom says....keep it simple things wil go right.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 301
Reputation: TomW is on a distinguished road 
Solved Threads: 43
TomW TomW is offline Offline
Posting Whiz
 
0
  #5
Oct 20th, 2009
Again I wouldnt use 3 different timers but if it is required by your class specifications, I added an example with using 3 different timers.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 181
Reputation: PcPro12 is an unknown quantity at this point 
Solved Threads: 6
PcPro12 PcPro12 is offline Offline
Junior Poster
 
0
  #6
Oct 20th, 2009
ohh...sorry...I'm still a beginner...and my previous assignment, I spent like 2 days to find out how to use one timer to loop 6 colors in the form background, and failed, thinking it wasn't possible..so I used 6 timers

but I'm glad I can use one timer, and I would be glad to use just one. My assignment is just an explanation of what the program should do and look like. How I do it and what I use is up to me.

@girlwayne: I thought that WAS the simple way I guess I was wrong...

Thanks for the code Tom, I'll try it, and it looks like I got WAY more to learn. I tend to write longer code for simple stuff. Even when simpler code exists.

Thanks Tom and girlwayne for the reply
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 181
Reputation: PcPro12 is an unknown quantity at this point 
Solved Threads: 6
PcPro12 PcPro12 is offline Offline
Junior Poster
 
0
  #7
Oct 20th, 2009
Originally Posted by TomW View Post
  1. Private Sub btnStart_Click(...) Handles btnStart.Click
  2.  
  3. If btnStart.Text = "Start" Then
  4. btnStart.Text = "Stop"
  5. lblTrafficLight.Text = "Red"
  6. timRed.Enabled = True
  7. Else
  8. btnStart.Text = "Start"
  9.  
  10. Select Case True
  11. Case timRed.Enabled
  12. timRed.Enabled = False
  13. Case timGreen.Enabled
  14. timGreen.Enabled = False
  15. Case timYellow.Enabled
  16. timYellow.Enabled = False
  17. End Select
  18. End If
  19.  
  20. End Sub
  21.  
  22. Private Sub timRed_Tick(...) Handles timRed.Tick
  23. lblTrafficLight.Text = "Green"
  24. timGreen.Enabled = True
  25. timRed.Enabled = False
  26. End Sub
  27.  
  28. Private Sub timGreen_Tick(...) Handles timGreen.Tick
  29. lblTrafficLight.Text = "Yellow"
  30. timYellow.Enabled = True
  31. timGreen.Enabled = False
  32. End Sub
  33.  
  34. Private Sub timYellow_Tick(...) Handles timYellow.Tick
  35. lblTrafficLight.Text = "Red"
  36. timRed.Enabled = True
  37. timYellow.Enabled = False
  38. End Sub
I just tried this and it works like a charm

Thank you very much. You solved my problem completely. I just changed the label to my images and everything worked fine.

I noticed you set the GREEN color to the red timer, yellow color to the green timer, and red color to the yellow timer. I think that was where I was confused. The timer runs the time before executes its code, as I've noticed. So than what I did was display my code twice and put the colors in the wrong timers.

I also now understand and see why using too many timers is and would be very confusing and time consuming also.

I learned a lot from your replies and your code. Thanks again.
-PcPro12
Last edited by PcPro12; Oct 20th, 2009 at 5:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 301
Reputation: TomW is on a distinguished road 
Solved Threads: 43
TomW TomW is offline Offline
Posting Whiz
 
0
  #8
Oct 20th, 2009
You sound like your trying to understand all that is going on with the code; so that is good. I didnt add comments to this to allow ya to think about it but if you need an explaination just ask.

Timer interval is set to 1,000

Personally I would do this with a loop, the StopWatch class and the TimeSpan object but I dont wanna get to far ahead of where your at in school.

  1. Public Class Form1
  2.  
  3. Dim m_intCounter As Integer = 0
  4.  
  5. Private Sub btnStart_Click(...) Handles btnStart.Click
  6.  
  7. If btnStart.Text = "Start" Then
  8. btnStart.Text = "Stop"
  9. m_intCounter = 0
  10. Timer1.Enabled = True
  11. Else
  12. btnStart.Text = "Start"
  13. lblTrafficLight.Text = ""
  14. lblTrafficLight.BackColor = Me.BackColor
  15. Timer1.Enabled = False
  16. End If
  17.  
  18. End Sub
  19.  
  20. Private Sub Timer1_Tick(...) Handles Timer1.Tick
  21.  
  22. If m_intCounter >= 40 Then
  23. m_intCounter = 1
  24. Else
  25. m_intCounter += 1
  26. End If
  27.  
  28. Select Case m_intCounter
  29. Case 1 To 20
  30. lblTrafficLight.Text = "Red"
  31. lblTrafficLight.BackColor = Color.Red
  32. Case 21 To 36
  33. lblTrafficLight.Text = "Green"
  34. lblTrafficLight.BackColor = Color.Green
  35. Case 37 To 40
  36. lblTrafficLight.Text = "Yellow"
  37. lblTrafficLight.BackColor = Color.Yellow
  38. Case Else
  39. 'Do nothing
  40. End Select
  41.  
  42. End Sub
  43.  
  44. End Class
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 181
Reputation: PcPro12 is an unknown quantity at this point 
Solved Threads: 6
PcPro12 PcPro12 is offline Offline
Junior Poster
 
0
  #9
Oct 20th, 2009
ya, that's the first thing I do when I see a new piece of code. I read it and try to see what everything does. And so far, I understood all your code.

Ya, I wanted to do a loop too, but wasn't sure how to do it. I tried playing around with "DO WHILE LOOP", "IF ELSE", "SELECT CASE" statements. But I guess I was doing everything wrong. Cause nothing worked. By by looking at your code above, everything makes sense and I think it would just be easier to do the above instead of putting in 3 timers.

For my class, that would probably be too far. But not for me, I'm ahead of my class in programming and assignments. I learned very basic loop, case, if else statements. But they were basic only. Not much more advanced than that.

Once again, thanks for the help, I learned yet again some more new stuff from your code

-PcPro12
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 301
Reputation: TomW is on a distinguished road 
Solved Threads: 43
TomW TomW is offline Offline
Posting Whiz
 
0
  #10
Oct 20th, 2009
Glad to help.

Well if you use a loop instead of a timer, you would have to keep track of the start time and with each itteration check the current time to see how much time has passed. The loop is easy, working with datetime functions and the timespan object get a bit more involved though.

If you type in "StopWatch" into the help file, it has a nice downloadable example of a stopwatch type of program. It shows how you can compare and format elapsed time for display.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC