LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

HELP WITH A TRAFFIC LIGHT THAT RUNS FOR 2 MINS

Solved!
Go to solution

Hey guys, I'm new to Labview. I need a hand to work on this code and make it run exactly for 2 mins(up counting from 0-120secs) and it should run FOREVER unless I press a stop button. This is the timing: From 0-55 (green on), 56-59 (yellow on), and 60-120 (red on). And if possible, I want to show the timer on front panel to the user.

 

For now, I know that I should use a while loop and knows how to insert a stop button to the while loop. But how do I make the loops to run for a certain period of time? Please show me the simplest method possible as I really trying to hard to understand this. 

 

Shown below is my previous code where the user needs to input any number to light up the traffic. This one is without any timer but only to light up the traffic light based on input from the user. Sorry for my bad english

Download All
0 Kudos
Message 1 of 12
(5,199 Views)
Solution
Accepted by topic author LabviewBeginner97

The simplest...

 

Use a milisecond timer to get a time reference.

Divide it by 1000 to get seconds.

Use a quotient and remainder with x the time, 120 as y.

Use the remainder as an input for your logic.

 

Put a wait ms in the loop, or it will drain one CPU thread.

 

An alternative is to:

use a shift register with a counter.

Increment it each cycle.

Give the loop a 1 sec. wait, use the Q&R with 120 and  use the R for the logic

Put R in the right shift register.

 

This is all OK-ish for a test, but not how you make a LabVIEW program. Just saying, if you're going to add more (and more), you need a strategy...

 

Think "what if"... What if you have 10 colors? What if you have 10 RGB lights? What if the timing is different for each of them?

 

Message 2 of 12
(5,181 Views)

Omg you're a genius! Thank you so much, I've been trying for hours but nothing works. Now I understand better.

0 Kudos
Message 3 of 12
(5,151 Views)

Hello, do you successfully get the solutions? I am still trying to put the timer inside the loop but its not working.

0 Kudos
Message 4 of 12
(4,326 Views)

@nr_syfn wrote:

Hello, do you successfully get the solutions? I am still trying to put the timer inside the loop but its not working.


Then show us what you are doing and describe your definition of "not working".(timing wrong? Broken VI? Computer bursts into flames? etc.). Be specific!

0 Kudos
Message 5 of 12
(4,310 Views)

Here's the edited program from previous owner, I couldn't get the traffic light running continuously.

nr_syfn_0-1640580063720.png

 

 

0 Kudos
Message 6 of 12
(4,290 Views)

It does not run forever because the loop terminates around iteration 120. So try to find a math function that starts over from zero instead, without stopping the loop.

 

Also:

  • The sequence structure serves no purpose. Remove it!
  • There is no need for any local variable if you place the terminal correctly. Also make sure to pick the correct mechanical action.
  • Your timer is not in seconds and has the wrong representation.
  • Your wait constant has the wrong representation.
  • Most of the code is overly complicated and could be simplified dramatically.
  • ...
0 Kudos
Message 7 of 12
(4,285 Views)

@altenbach wrote:

It does not run forever because the loop terminates around iteration 120. So try to find a math function that starts over from zero instead, without stopping the loop.


Alternatively, put a 2nd loop around everything.

 

The inner while loop then turns out to be a for loop, with 122 wired to N. The abort Boolean can still terminate the for loop inner, using the Conditional Terminal.

 

Be careful with using the loop iterator. With a wait of 100 ms, your code will stop working after 6 years and 9 months. If you lower the wait to 10, it will be 248 days... On a single cycled timed loop on an FPGA, which is where you want to control traffic lights, it won't make a minute.

0 Kudos
Message 8 of 12
(4,262 Views)

wiebe@CARYA wrote:

@altenbach wrote:

It does not run forever because the loop terminates around iteration 120. So try to find a math function that starts over from zero instead, without stopping the loop.


Alternatively, put a 2nd loop around everything.

 


I don't like loop stacks containing the bulk of the code (see also) 😄

 

All you needs is a fully scalable and maintainable state machine! Use diagram constants to define the colors and times for each color, so they can be tuned without having to touch the main code (Or read them form a configuration file at program start). Now you just have three states (and maybe an "out of service" state, etc.) where each state knows how long to last (in real milliseconds) until it should go to the next state. All state code is the same, differing only in color and duration, allowing you to reuse the bulk of the code. You can even expand the code later to e.g. switch the cycle lengths according to time of day and traffic volume.

 

Why don't you search the forum for better examples? I am sure there are many!

0 Kudos
Message 9 of 12
(4,252 Views)

@altenbach wrote:
Why don't you search the forum for better examples? I am sure there are many!

Have a look here for some basic ideas.

 

altenbach_1-1588792751016.png

 

0 Kudos
Message 10 of 12
(4,249 Views)