Using for Loops

Using for loops to light groups of lights and to animate sequences.
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

for ... in ... loops

Light up the first 10 lights.

While you could type out all 10 lines of code, whenever you have to do the same thing over and over again, it's easier to make a loop.


              for i in range(10):
                pixels[i] = (0,0,100)
            

this makes a loop that repeats 10 times, but every time i takes a different value. i goes from 0 to 9: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Notice that in going from 0 to 9 there are 10 values.

The range function has more flexibility than just this if you pass it more values. The general form is:


              range(start, stop, step):
            

  1. start: starting value
  2. stop: stopping value (actually, it stops at one less than this value)
  3. step: how much to skip every time you go through the loop.

If you leave out the step value (e.g. range(3, 10)), it assumes a step of 1.

So the full code to turn first every other light you can use a step of 2:


            import board
            import neopixel

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

            for i in range(0, 10, 2):
              pixels[i] = (0,0,100)
            

Assignment 1

Make the first 16 lights green.
Code ▼

Assignment 2

Make the first 5 lights red, and the rest blue.
Hint ▼
Code ▼

Assignment 3

From light 4 to light 16 make every other light red.
Code ▼

Assignment 4

Alternate the colors blue and red for the entire strip.

Use loops to do this. However, you could also use a single loop and a conditional statement (if).

Code ▼
Code (if) ▼

Light Sequences: Change over time

Make all of the lights light up one at a time (with a 0.25 second delay between each lighting up.)

Here you just need to wait a moment (0.25 seconds to be exact) before lighting up the next light. We can do this by using the time module and the sleep function.

First import time:


              import time
            

Then use the sleep function to pause the program for the specified number of seconds, like so:


            time.sleep(0.25)
          

The sleep function needs to be inside the loop so that the loop pauses during each iteration. The full program would look like:


                import board
                import neopixel
                import time

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

                for i in range(20):
                  pixels[i] = (10,0,0)
                  time.sleep(0.25)
              

Assignment 5

Light every other light, but pause 0.5 seconds before each new one lights up.
Code ▼

Assignment 6

Make all of the lights green, keep them green for 0.5 seconds, and then change them to red going backwards from the end. Use a 0.1 second delay before each switch.
Code ▼

Assignment 7

Over 2.5 seconds have a single light of the 20 light up and march across the strip, with all the other lights being black.
Code ▼

Assignment 8

Have the last LED slowly light up to full red over 2 seconds.
Notes ▼

Repeating sequences: Nesting Loops

Light up every light (0.25 second delay) and repeat this sequence three times. Wait 0.5 seconds in between each sequence.

You can place loops within loops to get this done.


                import board
                import neopixel
                import time

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

                for n in range(3):
                  for i in range(20):
                    pixels[i] = (10,0,0)
                    time.sleep(0.25)
                  time.sleep(0.5)
              

In python, loops are defined by the indentation. So, notice how the inner loop (for i in range(20) is fully indented relative to the outer loop (for n in range(3)).

Also notice how the pause between the sequences (line 11) is dedented so it's not in the sequence loop, but still in the n loop.

Assignment 9

Make a single light go back and forth 5 times.
Code ▼

Assignment 10

Make the last led pulse. Have it light up to full red over 2 seconds, then fade to black over the next two seconds. Repeat this pulse 5 times.
Notes ▼

Assignment 11

Create an animation sequence of your own.