In this tutorial we learn something fun. I will guide you to design a game named as “Guess the number”. The user will enter an input number within the range of 0 to 10 as a guess; if the number guessed by the user is correct the LED will blink otherwise it will remain turned off. At the end of the tutorial you are provided with an exercise to do it by yourself, and in the next tutorials I will assume that you have done those exercises and I will not explain the concept regarding them.
Guess the number game in labview
- Now lets’ design a VI performing the operation described above. First of all, create a VI as we have done in tutorial 1 and save it for future use as we have been doing in the entire previous tutorial.
- The VI we are going to design will generate random numbers within the range of 0 to 10 and it will compare the input value of the user with the randomly generated number, if they are same the indicator LED will turn ON otherwise it will remain OFF.
- From the function palette on the block diagram, select numeric and then select Random Number as shown in the figure below,
Figure 1: Random generator
- The problem here is, the built in random number generator generates random number within the range of 0-1 and it generates floating point random numbers, where as we want to compare it with integers. Hence, first we have to manipulate the range of random number generator somehow to 0-10.
- We can do this simply by multiply the output of the random number generator by 10. From the function palette on the block diagram, select numeric and then select multiply as shown in the figure below,
Figure 2: Multiply block
- Connect one input side of this block with the output of random generator and at the other input side create a constant as shown in the figure below, and give it a value of 10,
Figure 3: Constant placement
- After doing so the block diagram thus formed will generate random numbers within the range of 0-10 but the number will be of type float as shown in the figure below,
Figure 4: Floating random number
- Now if we want to convert this random number from type floating point to type integer, we will use a built in block of LabView. From the function palette on the block diagram, select numeric and then select conversion and then select to long integer as shown in the figure below,
Figure 5: Using to integer block
- This block will apply the ceil function and change the floating point number to its next integer. At the output of this block create an indicator to keep the record of the number you wants to be guessed, as shown in the figure below,
Figure 6: Indicator creation
- Now from the control palette on the front panel select numeric and then select control, as shown in the figure below,
Figure 7: Creating control
- This control will take the input value from the user, compare it with the randomly generated value and display the correction of you guess in form of an LED indicator.
- Don’t forget to name or label every control and indicator according to their work in the VI. Now for the comparison portion of the VI from the function palette select comparison and then select Equal? As shown in the figure below,
Figure 8: Comparison block placement
- At one input of this block connect the output of the ‘to integer’ block that was generating random number integer, and at the other input connect the numeric control whose values is taken input from the user as a guess.
- This block will compare both the values; if they are equal the output of the block will be returned as true and false otherwise. The comparison block diagram is sown in the figure below,
Figure 9: Comparison block
- At the output node of this block click right and from the drop down menu select create and then select indicator as shown in the figure below,
Figure 10: LED indicator for correction
- This will create an LED indicator to show the correctness of your answer. The complete block diagram of your VI is shown in the figure below,
Figure 11: Block diagram
- Now enter a value in the control you created and run the VI, if the guess matches the number generated the LED will blink as shown in the figure below,
Figure 12: Correct guess
- And if the number generated is different from the number guessed LED will not blink as shown in the figure below,
Figure 13: Guess is incorect
- Now in order to keep the randomly generated number a secret, delete the numeric indicator from the block diagram and now run the VI. The correct and in correct conditions are shown in the figure below,
Figure 15: Correct guess
- Create a guess number game that will allow you to guess numbers between the range 2 -8 instead of 0 to 10.