Help with timers.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cassandra.flowers

    Help with timers.

    Hi,
    I'm using Visual Basic 6 and I'm trying to make a program to change the
    colours of traffic lights.

    I am using 3 oval shapes, for each light. I want the traffic lights to
    change colour through the standard traffic light sequence by changing the
    'backcolor' of each oval: Red, Red & Amber, Green, Amber, Red.

    There are 2 ways I am trying to do this:

    With 1 command button. Each time it is clicked it changes the backcolor
    property of the circles. The problem with this, is that I don't know how to
    make something change each time the command button is clicked:

    e.g. Red light
    CLICK
    Red & Amber light
    CLICK
    Green Light
    CLICK, etc.

    I tried doing it, but it changes all the lights at once, on the first click,
    and I want each light to change each time the button is clicked. I know I
    could do this using a few buttons, but don't know how to do it with just 1?

    The second way I am trying to do this is using a Timer Control.

    The problem is, I have no idea how to do this. All my reference books tell
    me how to set it up to perform the 'TIME' function. Nothing else. I want
    the lights to change after each second (1000 interval) but, I don't know how
    to do this.

    Could anyone shed some light on the subject please? I would be very
    grateful for any help anyone can give.

    Thanks

    Cassandra


  • CMiYC

    #2
    Re: Help with timers.

    On Mon, 10 Nov 2003 18:23:59 +0000, cassandra.flowe rs wrote:[color=blue]
    > With 1 command button. Each time it is clicked it changes the backcolor
    > property of the circles. The problem with this, is that I don't know how to
    > make something change each time the command button is clicked:
    >
    > e.g. Red light
    > CLICK
    > Red & Amber light[/color]


    You need to setup a state machine. Then each time the
    button is clicked you check the current "state", change the appropriate
    colors, change state, and exit.

    You'll need something like this:

    onFormLoad()
    state = 0 '0 = Red Light (initial state)


    onButtonClick()
    if state = 0 then
    redOval.color = black
    greenOval.color = green
    end if

    if state = 1 .... ' 1 = green
    if state = 2 .... ' 2 = yellow

    state = state + 1
    if state = 3 then state = 0 ' start over

    (obviously that's not the exact code but it should get you going)

    Setting up a timer would replace onButtonClick() with onTimerTick or
    whatever the Timer uses.

    -James

    Comment

    • Roy Riddex

      #3
      Re: Help with timers.


      "CMiYC" <cmiyc@nowhere. com> wrote in message
      news:pan.2003.1 1.10.19.11.25.9 20162@nowhere.c om...[color=blue]
      > On Mon, 10 Nov 2003 18:23:59 +0000, cassandra.flowe rs wrote:[color=green]
      > > With 1 command button. Each time it is clicked it changes the backcolor
      > > property of the circles. The problem with this, is that I don't know[/color][/color]
      how to[color=blue][color=green]
      > > make something change each time the command button is clicked:
      > >
      > > e.g. Red light
      > > CLICK
      > > Red & Amber light[/color][/color]

      I have recently tackled the exact same 2 programs from a book called
      "Computing Projects in Visual Basic" by D. Christopher. I'm in a good mood
      so I'll post my two programs for you.
      This one is for the Click event program:

      Option Explicit
      Dim counter As Integer

      Private Sub Command1_Click( )
      counter = counter + 1
      If counter = 5 Then
      counter = counter - 4
      End If
      If counter = 1 Then
      shpRed.BackColo r = RGB(255, 0, 0)
      shpAmber.BackCo lor = RGB(255, 255, 255)
      shpGreen.BackCo lor = RGB(255, 255, 255)
      ElseIf counter = 2 Then
      shpAmber.BackCo lor = RGB(210, 100, 45)
      ElseIf counter = 3 Then
      shpGreen.BackCo lor = RGB(0, 140, 0)
      shpRed.BackColo r = RGB(255, 255, 255)
      shpAmber.BackCo lor = RGB(255, 255, 255)
      ElseIf counter = 4 Then
      shpGreen.BackCo lor = RGB(255, 255, 255)
      shpAmber.BackCo lor = RGB(210, 100, 45)
      End If
      End Sub

      Private Sub Form_Load()
      counter = 1
      shpRed.BackColo r = RGB(255, 0, 0)
      shpAmber.BackCo lor = RGB(255, 255, 255)
      shpGreen.BackCo lor = RGB(255, 255, 255)
      End Sub

      and this one is for the timer controlled program:

      Option Explicit
      Dim counter As Integer

      Private Sub Command1_Click( )
      Timer1.Enabled = True
      End Sub

      Private Sub Form_Load()
      counter = 1
      shpRed.BackColo r = RGB(255, 0, 0)
      shpAmber.BackCo lor = RGB(255, 255, 255)
      shpGreen.BackCo lor = RGB(255, 255, 255)
      End Sub

      Private Sub Timer1_Timer()
      counter = counter + 1
      If counter = 5 Then
      counter = counter - 4
      End If
      If counter = 1 Then
      shpRed.BackColo r = RGB(255, 0, 0)
      shpAmber.BackCo lor = RGB(255, 255, 255)
      shpGreen.BackCo lor = RGB(255, 255, 255)
      ElseIf counter = 2 Then
      shpAmber.BackCo lor = RGB(210, 100, 45)
      ElseIf counter = 3 Then
      shpGreen.BackCo lor = RGB(0, 140, 0)
      shpRed.BackColo r = RGB(255, 255, 255)
      shpAmber.BackCo lor = RGB(255, 255, 255)
      ElseIf counter = 4 Then
      shpGreen.BackCo lor = RGB(255, 255, 255)
      shpAmber.BackCo lor = RGB(210, 100, 45)
      End If
      End Sub

      To set the timer to an interval of 1000 open your form, select the timer
      (which I assume you have placed on the form) then at the right of the screen
      you can set the interval to 1000. Let me know how you get on.
      PS. if you're gonna use my coding then remember to use the same names for
      your lights i.e. shpGreen, shpRed, shpAmber


      Comment

      • Roy Riddex

        #4
        Re: Help with timers.

        Oh and for the timed version, remember to have a command button to start
        things off (you only need it if you 'borrow' my code from above).


        Comment

        Working...