From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Countdown timer with starting value changing every iteration

Hello everyone. 

I am making a program in which a counter starts from 5 and goes to 0 and then a LED light gives an indication by turning on. After that a random event occurs whose value is added to or subtracted from original counter value of 5 and the counter runs again to 0 and then LED turns on. This process repeats itself unless its stopped. Now, the value of the counter due to random event can be in decimals like 4.36, 5.12, 4.89 etc. I want this counter to decrement in real time with counter values to be considered seconds/milliseconds. Currently I am using time delay but it will be applicable only for the initial counter value of 5 and not the later updated values in decimals. I have tried using Elapse time express VI but have not been able to get it run properly. Please help me with this issue. I have attached the VI and image of block diagram. Your help and guidance will be highly appreciated.

Best regards,

Fahad Bin Zahid

Download All
0 Kudos
Message 1 of 9
(3,423 Views)

Hello everyone. 

I am making a program in which a counter starts from 5 and goes to 0 and then a LED light gives an indication by turning on. After that a random event occurs whose value is added to or subtracted from original counter value of 5 and the counter runs again to 0 and then LED turns on. This process repeats itself unless its stopped. Now, the value of the counter due to random event can be in decimals like 4.36, 5.12, 4.89 etc. I want this counter to decrement in real time with counter values to be considered seconds. Currently I am using time delay but it will be applicable only for the initial counter value of 5 and not the later updated values in decimals. I have tried using Elapse time express VI but have not been able to get it run properly. Please help me with this issue. I have attached the VI and image of block diagram. Your help and guidance will be highly appreciated.

Best regards,

Fahad Bin Zahid

Download All
0 Kudos
Message 2 of 9
(3,405 Views)

There are several goals here that appear (to me) to be mixed up.  Let me see if I can reformulate your task slightly, and see if this helps at all --

  • You want a "Timer" that will take a time (in seconds, including fractions of a second) and will wait until the time remaining is in "whole seconds".  That is, if you call it with "5", it will wait 1 second and return "4", while if you call it with "2.35", it will wait 0.35 seconds and return "2".  Note that it is perfectly "legal" to call this with 0 or negative numbers (0 means "wait 1 second and return -1", while -0.4 mean "wait 0.6 seconds and return -1").
  • Do you know how a While Loop and a Shift Register work together?  The While loop lets you do things over and over (until a Stop condition is realized), while the Shift Register allows you to let the output from one While iteration become the input for the next iteration.  Do you know how to specify the input for the first iteration using the same Shift Register?
  • Do you know how to create a sub-VI?  I'd make the Timer in the first point a sub-VI, then call it in the While Loop of the second VI, feeding it values from the Shift Register (and conditionally "feeding" the Shift Register until it's time to get a new value).  This basically "solves" your problem.

Bob Schor

 

P.S. -- there are, of course, many ways to "solve" this problem.  I'm suggesting some steps that, if you try to do them, should teach you some fundamental principles of LabVIEW that you'll need to understand in order to master it.  "Doing it yourself" is the best way to learn.

0 Kudos
Message 3 of 9
(3,374 Views)

Hi,  thank you for your kind response.

1. There is no condition for the timer to change value after 1 second. It can run in milliseconds for whole or decimal numbers.

2. Yes, I know about the working of shift register and while loop infact I am already using them together for the counter.

3. Yes, I have the idea of creating sub VI but the problem is I am not getting how to implement timer on this counter or do this whole task in some other way. I have been struck on this problem. I have tried different things but nothing worked.

Please see the attached VI. Any help would be highly appreciated

0 Kudos
Message 4 of 9
(3,371 Views)

Hi Fahad,

 

I moved your message from the Hardware board to this LabVIEW board: it's a pure LabVIEW software question.

It will get more attention in here!

 

And please don't ask by PM for (generic) help…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 9
(3,374 Views)

I'm assuming you are trying to learn LabVIEW -- the best way to do that is to learn the basic concept and then apply them to problems (such as the assignment you have been given).  I'll suggest some approaches that you can consider.

  • Learn about sub-VIs.  A useful sub-VI for you would be a "Timer" that takes two inputs ("Time In", a Dbl that holds a "Time In (s)" and "Error In", the LabVIEW Error Line) and has two outputs ("Time Out (s)", a Dbl that holds a smaller Time and "Error Out", the output LabVIEW Error Line).
    • Might as well learn some good LabVIEW Style -- almost all sub-VIs that you develop should have the following characteristics:
    • Use the default 4-2-2-4 Connector Pattern, with inputs on the left edge, outputs on the right edge, and Error In and Error Out on the lower corners.
    • Create an Icon, such as a Square Outline and a 2-3 line Text name ("Timer") to identify the sub-VI on your Block Diagram.
    • The Timer inputs and outputs are in seconds (that's what the "(s)" in the name is supposed to suggest).  The Timer should wait enough milliseconds for "Time Out (s)" to be the next lower whole second time.  So if Time In was 4.35, you would wait 350 milliseconds and Time Out would be 4.00, while if Time In was 6.00, you would wait 1000 milliseconds and Time Out would be 5.00.
  • Learn about While Loops and how they use Shift Registers.  With your Timer sub-VI, you will want to call it repetetively, watching "Time Out" count down seconds, which "sounds like" a While Loop surrounding your Timer with the Shift Register connected to Time In and Time Out.  Think about how you want to start this While Loop, and what you want to do when Time Out reaches 0.

The nice thing about developing a sub-VI (or "breaking your Big Task into a number of smaller, simpler tasks") is that you can test the smaller parts by trying to run them with various inputs to convince yourself that they work. 

 

The above should be all that you need to start solving your problem by yourself.  Try it, you will learn a lot, and it will "stick" with you since you figured it out by yourself.

 

Bob Schor 

 

0 Kudos
Message 6 of 9
(3,361 Views)

Oops, I see I've answered the same question twice (because you asked it twice, and the second one was moved to the LabVIEW Forum from Hardware).

 

Go read my other response, and then come back with your version of Timer.vi, the routine that does a wait of up to 1 sec so that "Time Out" is a "whole number of seconds to continue waiting".  Once you so that, the problem essentially is solved ...

 

Bob Schor

0 Kudos
Message 7 of 9
(3,362 Views)

Thank you very much for your kind response.

I am not doing some assignment, I am working on an industrial project for which I have to use LabVIEW and this countdown timer is a small part of it. So while doing it, yes, I am learning LabVIEW as I am not an expert and have never used timers along with many other things.

Thank you for your advice on using sub VI. I know about sub VI and shift registers and I am already using them. The problem at this stage is not the organization of the program, but the implementation of timer on this variable countdown, be it in a sub VI or in the same program. Also, I don't require it to elapse time in milliseconds for decimal part and in seconds for whole number part, the whole figure can elapse in milliseconds. There has to be a random value for the countdown every time and every time when the count down goes to zero LED must give indication. I have tried using elapsed time express VI and some other things but not been able to get what I want.

Again, thank you for your time.

Best regards,

Fahad Bin Zahid

0 Kudos
Message 8 of 9
(3,345 Views)

Here's the question I was trying (unsuccessfully, I'm sorry to say) to get you to consider:

 

Suppose I want a "random wait" of from 0 to 10 seconds.  

  • First question -- how would I generate it?  LabVIEW's Random Number generator gives U(0, 1) numbers, meaning "Uniformly distributed between 0 and 1".  So I multiply by 10 and convert this to U(0, 10).

Suppose I get 4.5678.  The Wait (ms) requires a millisecond count, so I "round to the nearest millisecond" (can you figure out how to do this?) and get 4.568 seconds, or 4568 milliseconds.  What do I want the first "wait" to be?

  • Suppose I choose 1000 msec.  My timer will go 4.568, 3.568, 2.568, 1,568, 0,568, 0.
  • Suppose I choose 568 msec.  My timer will go 4.568, 4.000, 3.000, 2.000, 1.000, 0.
  • Which one of these is "correct"?  The answer is "It's up to you, you can choose either, or something else".  My personal preference, particularly if I'm displaying "Time remaining", is the second ...

Go ahead and try your hand at creating such a VI that gets Timer In, waits (up to) 1 second, and returns "Timer Out".  Once you have it working, see if you can embed it in a While Loop and solve your entire problem.  If you have trouble, post your VI and I'll provide more help.  You cannot learn LabVIEW (or any other Programming Languages) without writing some code and getting feedback on whether your code works.  Note that some of the best feedback is to simply run your code yourself and see if it "does the right thing" (it should always do what you tell it to do -- the trick, and skill, is getting it to do what you want it to do -- I hope you see the subtle distinction).

 

Bob Schor

0 Kudos
Message 9 of 9
(3,310 Views)