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: 

Can't get multiple LEDs to turn on

Hey!

 

I am currently trying to program a dice using a random number generator. I have programmed it to light up 

 

1 LED when showing 0

2 LEDs when showing 1

3 LEDs when showing 2

4 LEDs when showing 3

5 LEDs when showing 4 

6 LEDs when showing 5

 

The number generating part of the program works good. The problem happens when it is supposed to light up the LEDs. Instead of lighting up multiple LEDs it only lights up one.

 

For example: The number generator generates the number 4, in this case it is supposed to light up 5 LEDs. But instead it only lights up one LED. Sometimes it doesn't even light up at all and sometimes it lights up different LEDs that show the wrong number. 

 

This program used to work perfectly before but i did some small changes, after that it stopped working. I have also tried resetting the changes but I still can't get it working as I don't know what broke it. 

 

All help is apprecieted!

 

I have attached a screenshot of the program. 

 

Anteckning 2020-02-10 201106.png

0 Kudos
Message 1 of 4
(2,131 Views)

Hi GG,

 

the problem is called race condition - due to heavy overuse of local variables!

 

Hints:

Use an integer datatype for your numeric value.

Use one (1) case structure to define your boolean values.

Even better: use an array constant to define your booleans and a simple IndexArray function based on your numeric value!

 

When you need more help just search this forum, your problem is a typical exercise for LabVIEW beginners…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 4
(2,100 Views)

Attach your actual code instead of a truncated picture.

 

  • As has been mentioned, you code sufffers heavily from race conditions. You are writing to several LEDs multiple times in parallel, and whatever writes last wins and there is no guaranteed that it has anything to do with what you want.
  • Equal comparisons on orange wires are extremely dangerous, so that could be another source of errors, depending on how you generate the random numbers.
  • While you say that generating the numbers "works good", have you verified that the dice is "fair", i.e. that all number have equal probability? we often seem mistakes here.
  • Why is there a 4000ms wait??? When's the last time you used a program that takes up to 4 seconds to react to user inputs?

@GG00 wrote:

This program used to work perfectly before but i did some small changes, after that it stopped working. I have also tried resetting the changes but I still can't get it working as I don't know what broke it. 


The problem with race condition is that they are unpredictable and it is possible that it works correctly by chance but every edit (or undo) will cause a recompile and change.

 

I agree that an array of booleans is probably a better choice, but what you can try now to get it working quickly are two tiny changes:

 

  • delete all local variables
  • replace the comparisons with "greater or equal", "less or equal", or similar on the original random integer. Just play around until things fall into place. You probably can eliminate all the case structures at the same time!

See how far you get...

 

0 Kudos
Message 3 of 4
(2,076 Views)

Here is something to get you started. It won't be easy for a beginner to put it together from a picture and to fully understand the reason for each code items (Yes, they all have a purpose!), so this is a good learning experience. Make sure to understand the code fully! Also note that your teacher reads this forum, so don't cheat and copy it blindly. It would be trivial to change it to output 6 scalar LEDs at the end. Try it!

 

 

altenbach_0-1581367430985.png

 

 

It also teaches you that a simple problems only need simple code. If you can describe the problem in a few short sentences but it takes you a screen-full of LabVIEW code, you should immediately suspect that you are doing something wrong. 😄

 

 

 

0 Kudos
Message 4 of 4
(2,064 Views)