LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Formula node, in an infinite while loop, keeps reinitialising my variables. Need workaround.

I am trying to make a real-time 12-hour clock that allows the user to set the minutes and the hours as in a real world 12-hour clock. I implemented an infinite while loop by setting the while condition to “continue if true” and connecting it to a Boolean true. A wait of 1000ms is applied. The formula node contains the code that initializes the minutes and hours according to the user input. The code runs incrementing the seconds with each iteration; when the seconds are equal to 60, the seconds are initialized back to 0 and the minutes are incremented by 1. Similarly, when the minutes become equal to 60, the minutes are initialized back to 0 and the hours are incremented by 1; after the 12th hour, we have a condition where the hours are initialized to 1 and the cycle continues.

 

The seconds are being implemented perfectly, however after 60 seconds or 60 minutes have passed, even though the minutes or hours, respectively, are incremented for a second, they go back to the value of minutes and hours that the user set initially.

 

Please help me figure a workaround that would allow me to initialise the minutes and hours only once and after that hold the value of the variables instead reinitialising with each iteration.

 

0 Kudos
Message 1 of 6
(3,357 Views)

Hi medodo,

 

THINK DATAFLOW!

 

When you set the inputs to your formula node to fixed values you should not wonder why they didn't change!

 

This is all you need:

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 6
(3,339 Views)

The "workaround" for code like this is to take advantage of free material offered by NI and learn how to code correctly.  🙂

 

Unit 1 - Getting Started

Introduction to LabVIEW - particularly the Dataflow section.

LabVIEW Introduction Course - Three Hours

LabVIEW Introduction Course - Six Hours

 

I think the first and second links will be most helpful to you.  The other ones are a bit dry.

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.
0 Kudos
Message 3 of 6
(3,315 Views)

Think of a While Loop as an "inside and outside" structure.  Things you only want to happen once should be outside of the loop, either as inputs or outputs.  You only want the user to set the time once.  So, why is it in the loop? That means this code will run in each iteration.  Your loop is basically:

 

While(1) {

Minutes = User Defined Minutes

Hours = User Defined Hours

Seconds = 0

 

Update time

Output Time

}

 

Looking at that loop, why do you think it keeps getting set back?

 

You should also consider these loops are impacted by Windows.  You aren't going to be reading it at exactly a second.  Over time, you'll drift from your time.  You might want to look at an implmenetation that uses the "Get Date/Time" function instead and saving the original value as an offset to work with the user's input.

 

You're also using an infinite loop.  Why?  I get the idea that a clock goes on forever.  But, this is a digital clock.  At some point you want to shut it down.  Using that stop sign isn't really a good idea. Why not add a control to stop the loop when you're done with the clock?

 

Why don't you have anything in your code to verify the user's input?  Do you really want to allow the user to choose 14 hours and 62 minutes?  Your logic looks specifically for a single value to cycle over.  If the user inputs a value higher, you'll never act like a clock.

 

You don't need a workaround.  Everything is operating as expected.

0 Kudos
Message 4 of 6
(3,297 Views)

Thank you so much for your help, natasftw.

 

So I took what you said. I put the inputs outside the while loop and a control to start and stop the timer, however, the problem is still persisting. When the hours or minutes at the output get incremented after 60 minutes and 60 seconds, respectively, they display this incremented value only once after which they go back to the user defined value. What could be causing this?

0 Kudos
Message 5 of 6
(3,085 Views)

You didn't implement any of the shift registers on the while loop.

0 Kudos
Message 6 of 6
(3,078 Views)