How Tethered Terminal Control Works
Since this project involves cyber:bot motion, it uses the cyberbot extension. The first serial write line block (after the recommended 1 second startup delay) just reminds the person typing in the terminal that valid speeds are from -100 to 100. The serial new line block adds a line after the “Speeds are -100 to 100” message.
The main loop starts with three input statements getting text from the user that represents values. The input statement returns the character representations of the characters. The first one is set (text) to (serial read until(carriage return)) which reads the terminal until you press enter in which it will save the things you typed as a string.
In addition to working in calculations, number variables are required by the (Pin…) servo speed (vL or vR) method as well as the pause (ms) blockl. When you typed 25 in response to the Enter left speed: prompt, the “25“ string was stored in text. That won’t work for servo speed or pause. That’s why set (vL) to (parse to number (text)) stores the number version of the “25“ string in the vL variable. The same applies to vR, and to ms for the pause call.
Once vL, vR, and ms are all values stored in number variables, the project uses vL to set the left (P18) servo speed, and vR to set the right (P19) servo speed. Did you notice the negative sign in (Pin19) servo speed (0 – vR)? That makes it so that you can type positive values for left and right servo speeds and the result is forward motion. No more remembering that left is counterclockwise and right is clockwise for forward motion!
The ms number variable is used in the pause block’s argument so that the servos continue running for the ms time you typed into the terminal. After that, the servo stop block stops the servos from turning and completes the maneuver.
After this, the forever loop repeats, and you can type in another maneuver for the cyber:bot to execute.
Did You Know?
Input to parse to number will always be a two step process, but it doesn’t have to take up two blocks. For example,
… can be condensed to:
Expressions and statements contained by parentheses are evaluated from the inside toward the outside. So, in this case the result text string result of the digits you typed is returned by serial read until(carriage return). Then, that result (like the string “25”) is converted into a number with the outer parse to number(…), and then stored in vL.
If you accidentally type a character like ‘q’ instead of the number 1, it will cause an exception. That’s because the parse to number(…) block needs something with digits to convert to a number type. It can convert the string “25” to the value 25, but it cannot convert the string “q5” to any value.