Use LEGO MindStorms Inventor to Build a Pet Dog – Code it with Both Blockly Language and Python – Part Two

In the previous post, I introduced how to use blockly language to control LEGO MindStorms Inventor, and write code for the pet dog. As I have already mentioned, we could accomplish the similar functionalities by using Python. In today’s post, we will use the Python editor of LEGO MindStorms Inventor App to implement the same functionality.

The final effect is like this. The peg dog will get bored or feel “hungry”. As the pet owner, you need to play with it and feed it with the colorful blocks in time, otherwise, it will stop responding to you.

Python Code and Implementation

From line 7 to line 21, the program defines a function “tapping”. The program sets both left and right motors to the position 0 to ensure that the legs are put at “sitting” position. When user taps the smart hub, one motor runs forward and the other motor runs backward. Since the two motors need to run at the same time, the program uses motor pair to run them simutaneously. The third and fourth parameter of “move_tank” method controls the spinning direction and speed of the motor pair. When speed is (50, 50), the two motors could make the front legs move forward. When speed is (-50, -50), both of the front legs move backward.

At the end of the function, the program restores the front legs to the initial “sitting” position by calling “run_to_position(0)”.

Besides the “tapping” function, the program defines “running” function. Similar to “tapping”, “running” function also requires the two motors restore to “sitting” position at the beginning, and then spin simutaneously. Different from tapping, running contains several repeating actions. Therefore, the program repeats 5 times from line 34 to line 36 to simulate the continuous running behaviour. After that, the two front legs will restore to the initial “sitting” position.

As the convension of Python is to put function definition at the front of the file, so starting from line 46, the program defines those global variables and begins running code. Please note that the port connection to sensors and motors might be different on your model, so just check the port connection on your model and adjust the letters accordingly.

From line 64 to line 66, the program defines three variables. “game_on” indicates if the program runs the main loop. “boring_time” and “hungry_time” variables indicate the boring and hungry index, respectively.

At beginning, reset timer to 0 and then enter the main loop. At line 72, the program checks if the user double taps the smart hub. You might ask why the program uses “doubletapped”, instead of using “tapped” parameter, which the blockly language uses. That is because blockly language has not “doubletapped” option. “tapped” action is a bit sensive, so even when the user does not tap the smart hub, the motion sensor will sometimes trigger “tapped” event if the smart hub moves or shakes a bit. Using “doubletapped” event generates much less false positive signals.

When the ultrasonic sensor identifies any object closer to 8 cm, call “running” function. Since both “tapping” and “running” functions take time to execute, the program sets “boring_time” to 0 before and after running the functions.

From line 86 to line 95, the program checks the identified color of color sensor. If the user is “feeding” the pet dog, the program reduces the value of variable “hungry_time” until the “hungry_time” smaller than 5. It indicates the “Full” status.

From line 98 to line 115, the program checks the value of “hungry_time” and “boring_time” and makes different sound and light matrix pattern. Please note that the length of sound track might be different. In order to track the value of “hungry_time” and “boring_time” accurately, the program uses “” function to get the elasped time after the last call of “timer.reset()”. In this way, for example, if sound track “Damage” takes 3 seonds to finish playing, the boring_time will increase by 3, instead of increasing a fixed seconds.

When “hungry_time” value exceeds a certain threshold value (here it is 35), the program will begin to “shut down” and set game_on value to False. Once this variable “game_on” changes to False, the program exits the main loop and begins running from line 117. The pet dog is going to be “shut down” because it is too starved.

Through calling the two functions “tapping” and “running” and simulating feeding behaviour, the value of “hungry_time” and “boring_time” will fluctuate dynamically depending on how you interact with the pet dog. There are only one hundred plus code, so try to type it and see if your pet dog runs correctly. Enjoy the coding and have fun!

Note: All the analysis articles are copyright products of Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.