我有一个学校的项目,在这个项目中,我必须模拟一个四路交叉口的一组交通灯,所以两个信号灯必须相互配合,在切换到下一个信号灯之前有一个小的延迟。我已经完成了~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端级
发布于 2015-08-10 17:09:23
假设一个非常简单的光系统,其中每个状态与所有其他状态相互排斥,然后考虑使用枚举来表示每个光的当前方向和状态,并使用一个单个计时器,如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")。你也可以用这种方法处理一个奇怪的3路交叉口。
如果光的状态重叠,就像左转灯有不同的长度和/或开始时间比直线走,那么完全不同的方法解决这个问题可能会更好。
https://stackoverflow.com/questions/31923923
复制相似问题