Search code examples
loopsinfinite-looplabview

How to get out of infinite loop?


I want to make in LabView a game similar to this: memory game

but I get stuck in a infinite loop and do not know how to get out. This is what I have so far :Block diagram

This is a piece of code for two of the buttons. In my opinion there are only 3 possible cases (bt1 and bt2 are the identical buttons):

  1. Both bt1 and bt2 are pressed and they disappear (first part of the photo)
  2. Just one of the two buttons was pressed, but also another button was pressed and the buttons go back to the initial color (middle part of the photo)
  3. Just one of the two buttons was pressed and it is the only button pressed, so it must stay pressed until another button is pressed (last part of the photo)

The 'once' variable indicates if another button was pressed before (0-was not pressed before ; 1- was pressed before).

As you can see the infinite loop occurs between the 2nd and the 3rd case. Any suggestion of how I can solve this would be very helpful.

Update: In the middle part I deleted the part where 'once' receives the value 0, to avoid the infinite loop. (after BT1 and BT2 are set false)


Solution

  • The reason it is not working is as follows:

    1. Button pressed, Loop 3 runs and sets once to 1
    2. No action: Loop 2 runs and sets once to zero even though two buttons have not been pressed.

    Depending on the speed of the while loop it may not be possible to notice the change. Highlight execution is useful here.

    The issue with your structure is that the there is no difference between case 2 and case 3, i.e. you could set the cards to have a value, if equal case one if not case 2 and junk case three as two cards not turned.

    Rather than use Boolean, use something with a value, e.g. picture controls.

    The following uses an event listener to monitor button presses:

    enter image description here

    enter image description here

    enter image description here

    There are four controls, 2 skull & 2 apple.
    If more than one control is clicked there is a check for equality and if true the the controls are hidden, if false they revert to grey.

    You can have as many pictures as desired in this scenario,