title: Traffic Light Simulation Trouble
body: <p>I have a project for school in which I must simulate a set of traffic lights at a four-way intersection, so two lights must work in conjunction with each other and have a small delay before switching to next set of lights. I have this ~98% done but can't seem to get my red lights to function. As it stands, my green lights and yellow lights function as expected, with the exception that after the light is yellow, it returns to green...
  I'm not sure where I went wrong here, especially considering the yellow lights work. Any pointers would be very appreciated, Thank you!</p>
  
  <pre><code>Public Class frmTraffic
  
  Dim NS As Boolean = False
  Dim EW As Boolean = False
  
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
  End Sub
  
  Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
  
      'Set North/South boolean to true and call the Sub for it
      NS = True
      NSLights()
  
  End Sub
  
  Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
  
      'Add each light and timer object to a collection for ease of use
      Dim lights() = {L1Green, L1Yellow, L1Red, L2Green, L2Yellow, L2Red, L3Green, L3Yellow, L3Red, L4Green, _
                      L4Yellow, L4Red}
  
      Dim timers() = {tmrDelay, tmrGreen, tmrRed, tmrYellow}
  
      'Disable all timers
      For Each tmr In timers
          tmr.Enabled = False
      Next
      'Return all lights to default system color
      For Each light In lights
          light.BackColor = SystemColors.Window
      Next
  
  End Sub
  
  Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles tmrYellow.Tick
  
      tmrGreen.Enabled = False
      NSLights()
      EWLights()
      tmrRed.Enabled = True
  
  End Sub
  
  Private Sub tmrDelay_Tick(sender As Object, e As EventArgs) Handles tmrDelay.Tick
  
      tmrRed.Enabled = False
      NSLights()
      EWLights()
      'Add each light and timer object to a collection for ease of use
      Dim lights() = {L1Green, L1Yellow, L1Red, L2Green, L2Yellow, L2Red, L3Green, L3Yellow, L3Red, L4Green, _
                      L4Yellow, L4Red}
      'Return all lights to default system color
      For Each light In lights
          light.BackColor = SystemColors.Window
      Next
      Swap()
  
  End Sub
  
  Private Sub tmrGreen_Tick(sender As Object, e As EventArgs) Handles tmrGreen.Tick
  
      NSLights()
      EWLights()
      tmrYellow.Enabled = True
  
  End Sub
  
  Private Sub tmrRed_Tick(sender As Object, e As EventArgs) Handles tmrRed.Tick
  
      tmrYellow.Enabled = False
      NSLights()
      EWLights()
      tmrDelay.Enabled = True
  
  End Sub
  
  Sub NSLights()
  
      'Begin the Traffic light pattern
      If NS = True Then
          tmrDelay.Enabled = False
          tmrGreen.Enabled = True
          L1Yellow.BackColor = SystemColors.Window
          L1Red.BackColor = SystemColors.Window
          L2Yellow.BackColor = SystemColors.Window
          L2Red.BackColor = SystemColors.Window
          L1Green.BackColor = Color.Green
          L2Green.BackColor = Color.Green
          If tmrYellow.Enabled = True Then
              L1Green.BackColor = SystemColors.Window
              L2Green.BackColor = SystemColors.Window
              L1Yellow.BackColor = Color.Yellow
              L2Yellow.BackColor = Color.Yellow
              If tmrRed.Enabled = True Then
                  L1Yellow.BackColor = SystemColors.Window
                  L2Yellow.BackColor = SystemColors.Window
                  L1Red.BackColor = Color.Red
                  L2Red.BackColor = Color.Red
              End If
          End If
      End If
  
  End Sub
  
  Sub EWLights()
  
      'Begin the Traffic light pattern
      If EW = True Then
          tmrDelay.Enabled = False
          tmrGreen.Enabled = True
          L3Yellow.BackColor = SystemColors.Window
          L3Red.BackColor = SystemColors.Window
          L4Yellow.BackColor = SystemColors.Window
          L4Red.BackColor = SystemColors.Window
          L3Green.BackColor = Color.Green
          L4Green.BackColor = Color.Green
          If tmrYellow.Enabled = True Then
              L3Green.BackColor = SystemColors.Window
              L4Green.BackColor = SystemColors.Window
              L3Yellow.BackColor = Color.Yellow
              L4Yellow.BackColor = Color.Yellow
              If tmrRed.Enabled = True Then
                  L3Yellow.BackColor = SystemColors.Window
                  L4Yellow.BackColor = SystemColors.Window
                  L3Red.BackColor = Color.Red
                  L4Red.BackColor = Color.Red
              End If
          End If
      End If
  
  End Sub
  
  Function Swap()
  
      Dim cond0 = Nothing
      Dim cond1 = NS = True
      Dim cond2 = NS = False
      Dim cond3 = EW = True
      Dim cond4 = EW = False
      Dim x = cond0
  
      If NS = cond1 Then
          NS = cond2
          x = cond3
          EWLights()
      ElseIf EW = cond3 Then
          EW = cond4
          x = cond1
          NSLights()
      End If
  
      Return x
  
  End Function
  </code></pre>
  
  <p>End Class</p>