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

TMR.Interval = 20000 '(20 seconds)
TMG.Interval = 16000 '(16 seconds)
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.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TLR As System.Windows.Forms.PictureBox
    Friend WithEvents TLY As System.Windows.Forms.PictureBox
    Friend WithEvents TLG As System.Windows.Forms.PictureBox
    Friend WithEvents TL As System.Windows.Forms.PictureBox
    Friend WithEvents btnStart As System.Windows.Forms.Button
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
    Friend WithEvents mnuExit As System.Windows.Forms.MenuItem
    Friend WithEvents tmr As System.Windows.Forms.Timer
    Friend WithEvents tmg As System.Windows.Forms.Timer
    Friend WithEvents tmy As System.Windows.Forms.Timer
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents Timer2 As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.TLR = New System.Windows.Forms.PictureBox
        Me.TLY = New System.Windows.Forms.PictureBox
        Me.TLG = New System.Windows.Forms.PictureBox
        Me.TL = New System.Windows.Forms.PictureBox
        Me.btnStart = New System.Windows.Forms.Button
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.mnuFile = New System.Windows.Forms.MenuItem
        Me.mnuExit = New System.Windows.Forms.MenuItem
        Me.tmr = New System.Windows.Forms.Timer(Me.components)
        Me.tmg = New System.Windows.Forms.Timer(Me.components)
        Me.tmy = New System.Windows.Forms.Timer(Me.components)
        Me.Button1 = New System.Windows.Forms.Button
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.Timer2 = New System.Windows.Forms.Timer(Me.components)
        Me.SuspendLayout()
        '
        'TLR
        '
        Me.TLR.BackgroundImage = CType(resources.GetObject("TLR.BackgroundImage"), System.Drawing.Image)
        Me.TLR.Location = New System.Drawing.Point(48, 16)
        Me.TLR.Name = "TLR"
        Me.TLR.Size = New System.Drawing.Size(72, 160)
        Me.TLR.TabIndex = 0
        Me.TLR.TabStop = False
        '
        'TLY
        '
        Me.TLY.BackgroundImage = CType(resources.GetObject("TLY.BackgroundImage"), System.Drawing.Image)
        Me.TLY.Location = New System.Drawing.Point(48, 16)
        Me.TLY.Name = "TLY"
        Me.TLY.Size = New System.Drawing.Size(72, 160)
        Me.TLY.TabIndex = 1
        Me.TLY.TabStop = False
        '
        'TLG
        '
        Me.TLG.BackgroundImage = CType(resources.GetObject("TLG.BackgroundImage"), System.Drawing.Image)
        Me.TLG.Location = New System.Drawing.Point(48, 16)
        Me.TLG.Name = "TLG"
        Me.TLG.Size = New System.Drawing.Size(72, 160)
        Me.TLG.TabIndex = 2
        Me.TLG.TabStop = False
        '
        'TL
        '
        Me.TL.BackgroundImage = CType(resources.GetObject("TL.BackgroundImage"), System.Drawing.Image)
        Me.TL.Location = New System.Drawing.Point(48, 16)
        Me.TL.Name = "TL"
        Me.TL.Size = New System.Drawing.Size(72, 160)
        Me.TL.TabIndex = 3
        Me.TL.TabStop = False
        '
        'btnStart
        '
        Me.btnStart.Location = New System.Drawing.Point(464, 40)
        Me.btnStart.Name = "btnStart"
        Me.btnStart.TabIndex = 4
        Me.btnStart.Text = "Start"
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile})
        '
        'mnuFile
        '
        Me.mnuFile.Index = 0
        Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuExit})
        Me.mnuFile.Text = "File"
        '
        'mnuExit
        '
        Me.mnuExit.Index = 0
        Me.mnuExit.Text = "Exit"
        '
        'tmr
        '
        Me.tmr.Interval = 20000
        '
        'tmg
        '
        Me.tmg.Interval = 16000
        '
        'tmy
        '
        Me.tmy.Interval = 4000
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(512, 136)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 5
        Me.Button1.Text = "Button1"
        '
        'Timer1
        '
        Me.Timer1.Interval = 2000
        '
        'Timer2
        '
        Me.Timer2.Interval = 2000
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.White
        Me.ClientSize = New System.Drawing.Size(632, 278)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.btnStart)
        Me.Controls.Add(Me.TL)
        Me.Controls.Add(Me.TLG)
        Me.Controls.Add(Me.TLY)
        Me.Controls.Add(Me.TLR)
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Traffic Light"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        'hides TL and displayed TR
        TL.Visible = False
        TLR.Visible = True
        'starts the TMG timer
        tmg.Enabled = True
    End Sub
    Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        'closes the program
        Dim splash As New splash
        Me.Close()
        splash.Close()
    End Sub

    Private Sub tmr_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr.Tick
        'displays TLR and hides everything else
        TLY.Visible = False
        TLG.Visible = False
        TLR.Visible = True
        'disables this timer and starts TMG
        tmr.Enabled = False
        tmg.Enabled = True
    End Sub

    Private Sub tmg_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmg.Tick
        'displays TLG and hides everything else
        TLY.Visible = False
        TLR.Visible = False
        TLG.Visible = True
        'disables this timer and starts TMY
        tmg.Enabled = False
        tmy.Enabled = True
    End Sub

    Private Sub tmy_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmy.Tick
        'displays TLY and hides everything else
        TLR.Visible = False
        TLG.Visible = False
        TLY.Visible = True
        'disables this timer and starts TMR
        tmy.Enabled = False
        tmr.Enabled = True
    End Sub

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'when the form loads, all images except TL are hidden
        TLR.Visible = False
        TLG.Visible = False
        TLY.Visible = False
    End Sub
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

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.

Private Sub btnStart_Click(...) Handles btnStart.Click

        If btnStart.Text = "Start" Then
            btnStart.Text = "Stop"
            lblTrafficLight.Text = "Red"
            timRed.Enabled = True
        Else
            btnStart.Text = "Start"

            Select Case True
                Case timRed.Enabled
                    timRed.Enabled = False
                Case timGreen.Enabled
                    timGreen.Enabled = False
                Case timYellow.Enabled
                    timYellow.Enabled = False
            End Select
        End If

    End Sub

    Private Sub timRed_Tick(...) Handles timRed.Tick
        lblTrafficLight.Text = "Green"
        timGreen.Enabled = True
        timRed.Enabled = False
    End Sub

    Private Sub timGreen_Tick(...) Handles timGreen.Tick
        lblTrafficLight.Text = "Yellow"
        timYellow.Enabled = True
        timGreen.Enabled = False
    End Sub

    Private Sub timYellow_Tick(...) Handles timYellow.Tick
        lblTrafficLight.Text = "Red"
        timRed.Enabled = True
        timYellow.Enabled = False
    End Sub
commented: helped me solve my problem completely, THANKS!!! :D +4

Please dont confuse yourself with different timers as Tom says....keep it simple things wil go right.

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.

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 :icon_redface:

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 :)

Private Sub btnStart_Click(...) Handles btnStart.Click

        If btnStart.Text = "Start" Then
            btnStart.Text = "Stop"
            lblTrafficLight.Text = "Red"
            timRed.Enabled = True
        Else
            btnStart.Text = "Start"

            Select Case True
                Case timRed.Enabled
                    timRed.Enabled = False
                Case timGreen.Enabled
                    timGreen.Enabled = False
                Case timYellow.Enabled
                    timYellow.Enabled = False
            End Select
        End If

    End Sub

    Private Sub timRed_Tick(...) Handles timRed.Tick
        lblTrafficLight.Text = "Green"
        timGreen.Enabled = True
        timRed.Enabled = False
    End Sub

    Private Sub timGreen_Tick(...) Handles timGreen.Tick
        lblTrafficLight.Text = "Yellow"
        timYellow.Enabled = True
        timGreen.Enabled = False
    End Sub

    Private Sub timYellow_Tick(...) Handles timYellow.Tick
        lblTrafficLight.Text = "Red"
        timRed.Enabled = True
        timYellow.Enabled = False
    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

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.

Public Class Form1

    Dim m_intCounter As Integer = 0

    Private Sub btnStart_Click(...) Handles btnStart.Click

        If btnStart.Text = "Start" Then
            btnStart.Text = "Stop"
            m_intCounter = 0
            Timer1.Enabled = True
        Else
            btnStart.Text = "Start"
            lblTrafficLight.Text = ""
            lblTrafficLight.BackColor = Me.BackColor
            Timer1.Enabled = False
        End If

    End Sub

    Private Sub Timer1_Tick(...) Handles Timer1.Tick

        If m_intCounter >= 40 Then
            m_intCounter = 1
        Else
            m_intCounter += 1
        End If

        Select Case m_intCounter
            Case 1 To 20
                lblTrafficLight.Text = "Red"
                lblTrafficLight.BackColor = Color.Red
            Case 21 To 36
                lblTrafficLight.Text = "Green"
                lblTrafficLight.BackColor = Color.Green
            Case 37 To 40
                lblTrafficLight.Text = "Yellow"
                lblTrafficLight.BackColor = Color.Yellow
            Case Else
                'Do nothing
        End Select

    End Sub

End Class

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

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.

Ahh, I see...alright...

Alright cool...will do that...thanks again for you help :)

-PcPro12

If all is working please mark the thread as solved.

Yes, all is working...and I was at school when I did my last reply, and I forgot to mark it as solved...now I got on at home and marked it...

Thanks once again :)
-PcPro12

to TomW: did you try coding in vb6 using parallel port interfacing?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.