If you accidentally run into a problem of not being able to break a code execution: old code keeps running and you cannot send new code to the robot, this can be because your previous code is having an infinite loop running at max speed.
An infinite loop is a loop that does not have specific conditions to terminate itself. This code below is an example of an infinite loop:
while True:
print("hello world")
This code will run endlessly, until you turn off the robot. It is normal to run an infinite loop in your robot, but this will put the robot is a busy state of only focusing on running your loop, and not listening to the Break signal sent from the Portal.
When your code is still in development, it is a good practice to add a small time delay at the end of the loop, so that it is possible to terminate the loop, fix the code, and run test again. To do this, you can use the time
library with time.sleep()
function:
import time
while True:
print("hello world")
time.sleep(0.001)
time.sleep()
will temporarily pause the code execution for a small duration of time (in seconds) specified by the value passed in. In this example, we are pausing the code execution at the end of each loop by 0.001 second, or 1 ms. This is long enough for the robot to listen to the Break signal and break the code execution when you want to.