Circuit Python tutorials in Robotics II
View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python
In robotics, we always run our programs forever, using iteration, or a loop.
In this first tutorial, we’ll learn:
Let’s try the difference between these 2 programs
import time
while True:
print("this is in a loop!")
time.sleep(1)
You’ve likely noticed by now that rather than printing the statement once, it will print forever, in a loop! That’s what the “while True:” statement does. Let’s examine this:
while True:
this will run because the statement is simply “True”while 1 == 1:
This will also run because 1 is equivalent to 1while 5 == 7:
This will never run, because 5 is not equal to 7.import time
and time.sleep(1)
are special lines from a “module” that allow us to put second pauses into our code.import time
print("this is before the loop")
while True:
print("this is inside the loop")
print("Still inside the loop")
time.sleep(1)
print("this is NOT indented, so it is outside the loop")
print("the statement directly above, and this statement, will NEVER print")
print
statements will never print on our serial console, because they are after our forever, while True: loop.All of our robotics programs will have a single while True: loop inside of them, and this is where all of our code will run. Much like a robot vacuum, we want our robot following instructions forever, until it is powered off.