Using if Statements

If statements can be used to have the program do different things under different circumstances.
Note on the 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

if statement

Make the first 10 lights (red) except for the 5th light, which should be green.

While there are a number of ways to do this, we'll use an if statement to make the change. We code the logical statement:


              import board
              import neopixel

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

              for i in range(10):
                if (i == 4):
                  pixels[i] = (0, 100, 0) # green
                else:
                  pixels[i] = (100, 0, 0) # red
            

Every turn through the loop, the if statement checks to see if i is equal to 4. If it is, it makes that LED green, else, it make the LED red.

You will notice that the if statement uses a double equal sign ("==").


              if (i == 4):
            

== is a comparison operator that tests to see if the things on either side are the same: "equal to" each other.

The other comparison operators are:

Operator Test
==Equal To
!=Not Equal To
< Less Than
> Greater Than
<= Less Than Or Equal To
>= Greater Than Or Equal To

Since the operators have opposites of each other, and they all test to see if the statement is true or false, you can be flexible and creative.

For example, the same program, to have 10 red lights and the fifth one green could be written as:


              import board
              import neopixel

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

              for i in range(10):
                if (i != 4):
                  pixels[i] = (100, 0, 0) # green
                else:
                  pixels[i] = (0, 100, 0) # red
            

Assignment 1

Use a single for loop and an if statatement to make the first 8 lights blue and the rest red. Code the logic:
  • LOOP through all of the pixels.
    • IF the pixel number is less than 9 make the pixel blue.
    • Else make it red.
Code ▼

Assignment 2

Redo Assignment 1 using the greater than (>) operator.
Code ▼

Assignment 3

Redo Assignment 2 using the greater than or equal to (>=) operator.
Code ▼

Assignment 4

Make all the LED's green except for the second one (red) using the not equal to (!=) operator.
Code ▼

Using the Modulus Operator: %

The modulus is the remainder if you divide two numbers. So:

11 % 4 = 3

You can use this to check if a number is exactly divisible by another number, because if it is the remainder is zero.

So, if I want to make every third light green I could do:


            import board
            import neopixel

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

            for i in range(20):
              if (i % 3) == 0:
                pixels[i] = (0,40,0)
            

Assignment 5

Use an if statement to make every LED that is not divisible by 3 blue.
Code ▼

Assignment 6

Light up the pixels one at a time (green with a ~0.1 second delay) except for every 5th pixel which should full 1 second delay.
Code ▼

Logical Operators: and, or, nor, and not

If you have two conditions to be met you can nest if statements or you can use the logical operators to chain the conditions together.

For example, write a program that makes the pixels that are divisible by both 2 and 3 green:


            import board
            import neopixel

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

            for i in range(20):
              if (((i % 2) == 0) and ((i % 3) == 0)):
                pixels[i] = (0,10,0)
            

not negates the condition it's applied to. So to do the opposite of the program above: Write a program that makes the pixels that are not divisible by both 2 and 3 green:


            import board
            import neopixel

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

            for i in range(20):
              if not(((i % 2) == 0) and ((i % 3) == 0)):
                pixels[i] = (0,10,0)
            

Assignment 7

Write a program to make every pixel that is greater than 10 and even green, while making the others red.
Code ▼

Assignment 8

Write a program to make every pixel that is divisible by either 3 or 4 green, while making the others red.
Code ▼

Assignment 9

Write a program to make every pixel that is NOT divisible by either 3 or 4 green, while making the others red.
Code ▼