State Machine TETRIX® Mastery with LabVIEW™ State Machine Exercise Solution Introduction: In this guide, the steps to complete the State Machine exercise will be outlined. Concepts discussed in the lesson will be applied in order to build the solution VI. Solution: 1. Open a blank VI. 2. Create a While Loop with a shift register. Initialize the shift register with a string constant set to Idle. This is the state the robot must be in when the program starts. 3. Create a Case structure within the While Loop and wire the output of the shift register to the Case selector. Name the current case “Idle”. Code required for the Idle state will be placed in this case. 4. In the Idle case, place a Read Ultrasonic function with a constant set to Port 2 wired into the Port terminal. Use a Less? function to compare it to a value of 10. 5. Create a nested Case structure inside the Idle case and wire the output of the Less? function into the Case selector. 6. When the robot is in the Idle state, and it senses an object less than 10 cm away, it should continue to the Forward state. For this reason, place a constant set to “Forward” in the True case, and wire it to the input of the shift register. 7. When the robot is in the Idle state and does not sense an object less than 10 cm away, it should remain in the Idle state. For this reason, simply wire straight across the False case to send “Idle” to the input of the shift register. 31 TETRIX® Mastery with LabVIEW™ State Machine State Machine Exercise Solution 8. Notice that for each state, when the touch sensor is pressed, the program should stop. Since this is a similarity for all cases, the code to complete this action can be placed outside of the Case structure. Place a Read Touch function set to Pressed and Port 4 outside the Case structure and wire the Yes/No terminal to the stopping condition of the While Loop. 9. To make the robot perform actions in the Forward state, create a new case in the original Case structure, currently in the Idle case, and call this new case “Forward”. Make sure it is spelled the same way as the string constant sent to the shift register in the previous case. 10. In the Forward case, the robot is required to go forward for 1 second. Add a Move DC Motors followed by a Wait For Time function. Place the motor constant outside of the While Loop so that it can be used multiple times, and set the motor power to 50 for both motors. Set the Wait For Time function to wait for one second. 11. Since the robot needs to go to the Backward state once this action is completes, create a string constant set to “Backward” and wire it to the input of the shift register. 12. The Backward case is very similar to the Forward case, so right-click on the “Forward” label at the top of the Case structure and select Duplicate Case. Name this case “Backward”, making sure to spell it the same way as in the previous step. 13. The motors need to go in the reverse direction in the Backward case, so change the motor power constant to -50. Also, the next state that the robot should go to is the Idle state, so change the string constant to “Idle”. 14. Since the robot will move on to the Idle case next, in which the motors are stopped, add a Stop DC Motors command after the Wait function in order to stop the motors before moving onto the Idle case. 32 State Machine TETRIX® Mastery with LabVIEW™ State Machine Exercise Solution 15. Notice that there is still a case in the Case structure that says “False”, Default. This is not necessary for this program, so it can be deleted by right-clicking it and selecting Delete This Case. However, now there is no default case. Set the Idle case to be the default case by right-clicking and selecting Make This The Default Case. This will ensure that no matter what string constant is sent to the Case selector, the program will always know what to do. 16. After the touch sensor has been pressed, signalling the end of the program, the DC motors should be stopped before the program terminates. Add a Stop DC Motors function outside the While Loop to achieve this. Use the same motor constant that was used for the other DC Motor functions. 17. Download and run the program on the robot using the run button. 33
© Copyright 2026 Paperzz