Using while Loops

Using while loops.
Note on Pico vs. Zero:
  1. For the Pico use board.GP0 instead of board.D18 because they are set up to use different input pins by default.

References:

  1. QuickRef: Loops

while loops

Light up the first 10 lights.

We can do this with a for i in range(10): loop, but we can also use a while loop.

The while loop checks to see if a condition is true each time before it goes throught the loop. In this case we'll create a variable, i, give it an initial value (0), and increase it by 1 every time we loop.


              i = 0   # initialize variable
              while (i < 10):   # loop with check
                pixels[i] = (10,0,0)
                i = i + 1   # increase the value of i
            

this makes a loop that repeats 10 times, but every time we have to tell i to increase by 1 (line 4).

The full program looks like:


              import board
              import neopixel

              pixels = neopixel.NeoPixel(board.GP0, 20)

              i = 0   # iniatialize variable
              while (i < 10):   # loop with check
                  pixels[i] = (10,0,0)
                  i = i + 1   # increase the value of i
            

Assignment 1

Use a while loop light up ever other pixel in the strip (green).
Code ▼

Assignment 2

Use a while loop to light up the last 10 lights (green), going backwards with a 0.1 second delay between each one.
Code ▼

Assignment 5

Light up each light that is a Fibonacci Number.

The Fibonacci sequence starts with 0, 1, and then increases by adding the two previous numbers together to get the next number. So the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21 ....

Code (Version 1) ▼

Unpacking (Tuples and Arrays)


                (n1, n2) = (n2, n1 + n2)
            

In Assignment 2, that lights up the Fibonacci sequence, the code example takes three lines (13, 15, and 17) to update the two numbers in the Fibonacci sequence (n1 and n2).


            import board
            import neopixel

            pixels = neopixel.NeoPixel(board.GP0, 20)

            n1 = 0
            n2 = 1

            while (n2 < 20):
                pixels[n2] = (0,10,10)   # set pixel color

                # save the old value of n2
                n2Old = n2
                # update n2 (Fibonacci sequence)
                n2 = n1 + n2
                # make n1 the old value of n2
                n1 = n2Old
            

In python, you can avoid having to save the old value of n2 by updating both numbers at the same time using something called tuple assignment/unpacking tuples.


                # update n2 and n2 (Fibonacci sequence)
                (n1, n2) = (n2, n1+n2)
            

              import board
              import neopixel

              pixels = neopixel.NeoPixel(board.GP0, 20)

              n1 = 0
              n2 = 1

              while (n2 < 20):
                  pixels[n2] = (0,10,10)   # set pixel color

                  # update n2 and n2 (Fibonacci sequence)
                  (n1, n2) = (n2, n1+n2)
              

Infinite Loops

Sometimes you want your program to run forever. Then you can create an infinite loop using a while condition that is always true, such as:


                while True:
            
So, let's take a program that lights up every other light (0.1 second delay between lights),

              import board
              import neopixel
              import time

              pixels = neopixel.NeoPixel(board.GP0, 20)

              i = 0   # initialize variable
              while (i < 20):   # loop
                  pixels[i] = (0,10,0)
                  i = i + 2   # increment i
                  time.sleep(0.1)
            

Now to make it run forever, we stick it into an infinite loop (line 7). And we also need reset the pixels (make them black) in-between each iteration of the infinite loop (lines 14-16):


              import board
              import neopixel
              import time

              pixels = neopixel.NeoPixel(board.GP0, 20)

              while True:
                  i = 0   # initialize variable
                  while (i < 20):   # loop
                      pixels[i] = (0,10,0)
                      i = i + 2   # increment i
                      time.sleep(0.1)

                  # reset leds
                  for n in range(20):
                      pixels[n] = (0,0,0)