05-01-2020 03:19 AM
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
Solved! Go to Solution.
05-01-2020 03:48 AM
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?
05-01-2020 07:14 AM
Omg you're a genius! Thank you so much, I've been trying for hours but nothing works. Now I understand better.
12-26-2021 07:45 AM
Hello, do you successfully get the solutions? I am still trying to put the timer inside the loop but its not working.
12-26-2021 11:44 AM
@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!
12-26-2021 10:39 PM - edited 12-26-2021 10:41 PM
Here's the edited program from previous owner, I couldn't get the traffic light running continuously.
12-26-2021 11:08 PM - edited 12-26-2021 11:20 PM
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:
12-27-2021 04:11 AM - edited 12-27-2021 04:18 AM
@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.
12-27-2021 10:35 AM
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!
12-27-2021 10:45 AM - edited 12-27-2021 10:45 AM
@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.