How can I add the status of lights “red-yellow” in Verilog

OP #5901072
Rate this post
• ▲ useful
▼ not useful
I wrote a simple school project traffic light and I want to add a new 
state of "red-yellow" lights

!https://ibb.co/vDw4DTN This picture shows how it should look like

I do not know what the "if-else" statement looks like.

Thank you in advance for your help
1
`timescale 1ns / 1ps
2
module Traffic
3
(
4
  input       reset,
5
  input       clk,
6
  input       NS_VEHICLE_DETECT,
7
  input       EW_VEHICLE_DETECT,
8
  output wire NS_RED,
9
  output wire NS_YELLOW,
10
  output wire NS_GREEN,
11
  output wire EW_RED,
12
  output wire EW_YELLOW,
13
  output wire EW_GREEN
14
);
15

16
reg [5:0] lights; //  {NS_RED, NS_YELLOW, NS_GREEN, EW_RED, EW_YELLOW, EW_GREEN}
17
reg [4:0] nsCounter;
18
reg [3:0] ewCounter;
19
reg [1:0] yellowCounter;
20

21

22
assign {NS_RED, NS_YELLOW, NS_GREEN, EW_RED, EW_YELLOW, EW_GREEN} = lights;
23

24

25
always @(posedge clk)
26
   if (reset)
27
      lights <= 6'b001100;
28
   else
29
     if (nsCounter == 31 & EW_VEHICLE_DETECT & NS_GREEN)
30
       lights <= 6'b010100;
31
     else
32
       if (ewCounter == 15 & EW_GREEN) 
33
         lights <= 6'b100010;
34
       else
35
         if (yellowCounter == 3 & NS_YELLOW)
36
           lights <= 6'b100001;
37
         else
38
           if (yellowCounter == 3 & EW_YELLOW) 
39
             lights <= 6'b001100;
40
//------------------------------------
41

42
always @(negedge clk)
43
  if (reset)
44
    begin
45
      nsCounter     <= 5'h0;
46
      ewCounter     <= 4'h0;
47
      yellowCounter <= 2'h0;
48
    end
49
  else
50
    begin
51
      nsCounter     <= nsCounter     + 1'b1;
52
      ewCounter     <= ewCounter     + 1'b1; 
53
      yellowCounter <= yellowCounter + 1'b1;
54
    end
55
//------------------------------------
56

57
endmodule
58
//====================================
Moderator (Company: Titel) #5901289
Rate this post
• ▲ useful
▼ not useful
Use a FSM with states for the possible light patterns. Cycle through 
those states and assign the according lights value afterwards.

BTW1:
one counter for the delay is eoungh when using a FSM for the states.

BTW2:
A question arises when seeing this:
input       NS_VEHICLE_DETECT,
input       EW_VEHICLE_DETECT,
What is the idle state of those traffic lights?
NS and EW red?
Or is the main street NS green and the lower traffic street EW red?
If the second (which would be more comfortable, but it would not force 
main street drivers to reduce speed..) then you also could ignore the NS 
vehicle detection.

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.