In this tutorial I will help you to design a VI that will take a number as input from the user and at the output it will return the sum of natural numbers up to that number. For example, the user enters 3 as an input value to the VI then the output of the VI will return 1+2+3 = 6 in the indicator. We will be using a ‘while’ loop to do so. 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.
Explanation using a VI:
- 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. In order to place the numeric control click right on the front panel from the control palette selects numeric and then selects control as shown in the figure below,
Figure 1: Numeric control placement
- On the block diagram window, from the function palette select structures and then select while loop as shown in the figure below,
Figure 2: While loop placement
- If we are willing to add all previous number of a numeric integer we will need the previous sum value of the loop. But how can we access the previous value of the sum??. We can do so by using shift registers as we have used in previous tutorial. Click right on the boundary of the while loop and from the drop down menu select ad shift register as shown in the figure below,
Figure 3: Adding shift registers
- As we have already discussed in previous tutorial we must have to initialize the shift register. From the function palette select numeric and then select constant as shown in the figure below,
Figure 4: Creating numeric constant
Figure 5: Shift register constant
- For the addition task place an addition block, from the function palette select numeric and then select add as shown in the figure below,
Figure 6: Add block placement
- At one of the two inputs of the add block connect the right shift register, and the output of this block connect the right shift register.
- The purpose of this will be, the right shift register will store the value of sum from the previous iteration and will pass it on to the left shift register in the next iteration, as we have discussed in detail in the tutorial related to shift registers.
- Now on the other input of the add block we have to connect the incremented iteration because the number of iterations start from 0 but we want sum from 1 onwards. From the function palette select numeric and then select increment as shown in the figure below, Â
Figure 7: Increment block placement
- At the input of this increment block connect the iterative index of the while loop, and connect the output of this increment block to the remaining input of the add block. The resulting add block diagram will look like the one shown in the figure below,
Figure 8: Adding condition
- Now, we have to enter the termination condition of the while loop, the loop will exit when the iterative index is equal to the numbr we have given as input. In our case the control number is the input. From the function palette select comparison and then select greater than or equal block as shown in the figure below,
Figure 9: Greater than or equal block
- At the upper input of this block connect the increment iterative index and at the lower input connect the numeric control that will take user’s input value. At the output f greater than or equal block connect the loop condition button as shown in the figure below,
Figure 10: Termination condition
- This will continuously test the value of incremented iterative index in every iteration whether it is equal to or greater than the number entered by the user. As soon as the incremented iterative index become equal to the number entered by the user, the output will become true and the loop will stop. At the right shift register click right and from the drop down select create and then select indicator as shown in the figure below,
Figure 11: Output indicator
Figure 12: Complete block diagram
- Run the VI by entering a number 3 in the input number constant as an example as shown in the figure below,
Figure 13: Output 1
- Now test the system for any other number lets’ say 5 the sum will now be, 1+2+3+4+5=15 as shown in the figure below,
Figure 14: Output 2
Exercise:
- Design a VI that will take two input integers and calculate the sum of natural numbers between those two integers
<<Previous tutorial                    Next tutorial>>