交通灯模拟故障

时间:2015-08-10 15:48:59

标签: vb.net

我有一个学校项目,我必须在四路交叉口模拟一组交通灯,所以两个灯必须相互配合工作,并在切换到下一组灯之前有一个小延迟。我已经完成了~98%,但似乎无法让我的红灯亮起来。就目前而言,我的绿灯和黄灯按预期运行,除了灯亮黄灯后,它会变回绿灯...... 我不确定我在哪里出错,特别是考虑到黄灯工作。任何指针都将非常感谢,谢谢!

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

结束班

1 个答案:

答案 0 :(得分:1)

假设一个非常简单的灯光系统,其中每个状态与所有其他状态互斥,则考虑使用Enum来表示每个灯光的当前方向和状态,并且<如Plutonix所建议的强>单计时器。

这是一个简单的例子:

Public Class Form1

    Private Enum IntersectionDirectionState
        NorthSouth
        EastWest
    End Enum

    Private Enum TrafficLightState
        Green
        Yellow
        Red
    End Enum

    Private dir As IntersectionDirectionState = IntersectionDirectionState.NorthSouth
    Private NS As TrafficLightState = TrafficLightState.Green
    Private EW As TrafficLightState = TrafficLightState.Red

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        OutputState()
        Timer1.Interval = 2000
        Timer1.Start()
    End Sub

    Private Sub OutputState()
        Console.WriteLine(String.Format("dir: {0}, NS: {1}, EW: {2}", dir, NS, EW))
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Select Case dir
            Case IntersectionDirectionState.NorthSouth
                Select Case NS
                    Case TrafficLightState.Green
                        NS = TrafficLightState.Yellow

                    Case TrafficLightState.Yellow
                        NS = TrafficLightState.Red

                    Case TrafficLightState.Red
                        dir = IntersectionDirectionState.EastWest
                        EW = TrafficLightState.Green

                End Select

            Case IntersectionDirectionState.EastWest
                Select Case EW
                    Case TrafficLightState.Green
                        EW = TrafficLightState.Yellow

                    Case TrafficLightState.Yellow
                        EW = TrafficLightState.Red

                    Case TrafficLightState.Red
                        dir = IntersectionDirectionState.NorthSouth
                        NS = TrafficLightState.Green

                End Select

        End Select
        OutputState()
    End Sub

End Class

输出:

dir: NorthSouth, NS: Green, EW: Red
dir: NorthSouth, NS: Yellow, EW: Red
dir: NorthSouth, NS: Red, EW: Red
dir: EastWest, NS: Red, EW: Green
dir: EastWest, NS: Red, EW: Yellow
dir: EastWest, NS: Red, EW: Red
dir: NorthSouth, NS: Green, EW: Red
dir: NorthSouth, NS: Yellow, EW: Red
dir: NorthSouth, NS: Red, EW: Red
dir: EastWest, NS: Red, EW: Green

我将让您更新用户界面以显示正确的状态,并更改计时器的Interval以在周期的每个阶段产生不同的延迟。

通过这种方法,您可以添加方向枚举以处理受保护的左转弯(例如,您需要另一个变量来跟踪该状态,就像“NS”和“EW”一样)。你也可以用这种方法处理一个奇怪的三向交叉点。

如果灯光状态重叠,就像左转灯的长度和/或开始时间不同,那么采用完全不同的方法可能会更好。