Robotics-II-Circuit-Python

Circuit Python tutorials in Robotics II

View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python

Table of Contents

For Loops with Ranges in Python

In this tutorial, we’ll learn about:

Single Line Program

  1. Connect the M4 to your computer, and load code.py
  2. On line 1, write:
print("this is a non-looped program")
  1. Open the serial console.
  2. Save this program and observe the serial output. You’ll notice you should see a code done running line in your Serial console.

For Loop

  1. Delete all existing code.
  2. Write in this code:
for i in range(5):
    print("this is in a loop!")
  1. Open the serial console.
  2. Save this program and observe the serial output.

Examining a For Loop:

You’ve likely noticed by now that the statement is printed five times. That’s what the for i in range(5): statement does. Let’s examine this:

Try running the following code to see the counter i value throughout loop interations:

for i in range(3):
    print("Loop iteration number:", i)

Using Delays in For Loops:

We can also use time delays in for loops just like in while loops to see the iteration more clearly. This is generally advised for debugging purposes, but not super helpful for when we’re actually running code, unless the time.sleep() value in minimal.

  1. Import the time module and use time.sleep() for delays.
  2. Write and run the following code:
import time
for i in range(5):
    print("This is in a loop! We're on interation number:", i)
    time.sleep(1)
print("The loop has finished")

For Loops and While True Loops

A for loop lets us run a block of code a specific number of times. Unlike a while True: loop, which runs forever, a for loop ends after a predetermined number of iterations. This can be useful when you need to repeat actions a set number of times rather than indefinitely. We can even nest in for loops inside of our while True: loop.

  1. Delete all existing code.
  2. Write and run the following code:
import time

print("starting main loop")
while True:
    for i in range(5):
        print("This is in a loop! We're on interation number:", i)
        time.sleep(1)
    print("The loop has finished. Notice how 'i' will now reset back to '0'")
    time.sleep(1)
  1. Observe the serial output.

For Loops: 2 Arguments (Start & Stop)

a for loop can also take more than a single arguement. Up to this point, we’ve been playing with a single arguement:

for i in range(10):
    print(i)
    time.sleep(0.5)

Let’s try something different:

for i in range(0, 10):
    print(i)
    time.sleep(0.5)

We have 2 arguments now, rather than always starting from 0, we can say what number to start from, and what number to end at. Observe:

for i in range(5, 10):
    print(i)
    time.sleep(0.5)

For Loops: 3 Arguments (Start, Stop, Iterater)

Lets try 3 arguments:

for i in range(0, 10, 2):
    print(i)
    time.sleep(0.5)

Lets look at a few more examples:

Example 1: Count by 5’s

for i in range(0, 50, 5):
    print(i)
    time.sleep(0.5)

Example 2: Reverse counting down

for i in range(10, 0, -1):
    print(i)
    time.sleep(0.5)