LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

What do you guys think about my traffic lights code (see attached vi)?

I want constructive criticism.  I know I have a lot of case structures, but I know someone in this forum can get this done with a single while loop and couple of logics. His first name starts with C and last name starts with A. Those who know, knows who I am talking about.

 

But what do you think about what I have accomplished? I am so happy that I've learned a lot. Soon I will no longer be a baby programmer. Thanks God!

 

Feel free to share how you would do yours. Let's do code demonstration of simple stuff. 

0 Kudos
Message 1 of 19
(1,558 Views)
  • Try to do it without any local variables. None are needed!
  • No need to get timestamps because you are only interested in differences. Replace it with "high resolution relative seconds"
  • Your case structure after the loop can never be false, so it is just a glorified sequence frame.
  • traffic lights don't need a 10ms resolution.
  • Your "obj" control should be a diagram constant, not a hidden control. Give it a better name because the term "object" has a special meaning in programming.
  • Since Exactly one LED is true at any given time, use a radiobutton control for it (see here for example), greatly simplifying things.
  • etc.
Message 2 of 19
(1,523 Views)
  • In a typical traffic light, the red is on top and the green on the bottom, so please rearrange that.
  • You can eliminate all that duplicate code. If you need to really keeping your 9 states, all you need is three cases. (In fact, you only need three states, see my linked code above)
  • No need for locals!

 

See attached for a quick literal rewrite. Make sure you understand it fully.

Message 3 of 19
(1,517 Views)

In the US, the middle light is usually referred to as 'yellow'.  Also, the green is more of a blue-green.  The actual chromaticity ranges are specified by law (but I couldn't find them); however I've adjusted the colors to make them a bit closer.

 

I've also attached an old traffic light control that I made years ago.

"If you weren't supposed to push it, it wouldn't be a button."
Download All
Message 4 of 19
(1,486 Views)

Thanks Mr. Altenbach! These are very good advice. Also, I like how simple you made it look, using only 3 cases. I knew it would be a piece of cake for you. lol.

I have a question about what you did here: https://forums.ni.com/t5/LabVIEW/PUT-TIMER-AT-TRAFFIC-LIGHT/m-p/4046577#M1160741

How are you able to insert booleans (G, Y, R) in enum? How did you do that?

0 Kudos
Message 5 of 19
(1,464 Views)

Thank you, Mr. Paul_cardinale, for your input. Good to know!

0 Kudos
Message 6 of 19
(1,463 Views)

I wish every LabVIEW developer were as enthusiastic as you.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 7 of 19
(1,433 Views)

@GRCK5000 wrote:

Thanks Mr. Altenbach! These are very good advice. Also, I like how simple you made it look, using only 3 cases. I knew it would be a piece of cake for you. lol.

I have a question about what you did here: https://forums.ni.com/t5/LabVIEW/PUT-TIMER-AT-TRAFFIC-LIGHT/m-p/4046577#M1160741

How are you able to insert booleans (G, Y, R) in enum? How did you do that?


That's a radio buttons control (q.v.)

"If you weren't supposed to push it, it wouldn't be a button."
Message 8 of 19
(1,419 Views)

@GRCK5000 wrote:

How are you able to insert booleans (G, Y, R) in enum? How did you do that?


 

A radiobutton container (boolean palette) is similar to a cluster, but it can only contain booleans. Any style and number of booleans!!!!

 

Only exactly one (or also none of so configured) can be TRUE at any given time, As soon as another one turns true, all others turn false automatically. This is very useful and often dramatically simplifies your code.

 

In your case, you would place a radiobutton control from the palette, then drop your three LEDs in it. Now you can remove the two booleans that were originally in there. You can change the cluster order if needed. Of course you can make it a control (typical) or an indicator (as in this case).

 

The type is an enum with values for all element names in cluster order. The current value of the enum is the label of the element that is currently true, so make sure all elements have unique and clear labels, even if they don't show on the front panel.

 

( Also have a look at my description here and vote for it if you like. 😄 )

Message 9 of 19
(1,407 Views)

🤣 I just love what I do. Accomplishment brings even more happiness. Thanks God!

0 Kudos
Message 10 of 19
(1,345 Views)