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:
-
LOOP through the first 10 pixels (Line 6).
- IF it's the 5th pixel make it green (Lines 7,8).
- ELSE make the pixel red (Line 9,10).
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:
-
LOOP through the first 10 pixels.
- IF it's NOT the 5th pixel make it red.
- ELSE make it green.
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 singlefor
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.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
for i in range(20):
if i < 8:
pixels[i] = (0,0,50)
else:
pixels[i] = (50, 0, 0)
Assignment 2
Redo Assignment 1 using the greater than (>) operator.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
for i in range(20):
if i > 7:
pixels[i] = (50,0,0)
else:
pixels[i] = (0, 0, 50)
Assignment 3
Redo Assignment 2 using the greater than or equal to (>=) operator.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
for i in range(20):
if i >= 8:
pixels[i] = (50,0,0)
else:
pixels[i] = (0, 0, 50)
Assignment 4
Make all the LED's green except for the second one (red) using the not equal to (!=) operator.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
for i in range(20):
if i != 1:
pixels[i] = (0,40,0)
else:
pixels[i] = (40, 0, 0)
Using the Modulus Operator: %
The modulus is the remainder if you divide two numbers. So:
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 anif
statement to make every LED that is not divisible by 3 blue.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
for i in range(20):
if (i % 3) != 0:
pixels[i] = (0,0,30)
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.
import board
import neopixel
import time
pixels = neopixel.NeoPixel(board.GP0, 20)
for i in range(20):
# use an if statement to set the time delay
if (i%5 == 0):
dt = 1
else:
dt = 0.1
# make pixel green
pixels[i] = (0,10,0)
# pause for the set time (dt)
time.sleep(dt)
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.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
for i in range(20):
if ( ((i % 2) == 0) and (i > 10) ):
pixels[i] = (0,10,0)
else:
pixels[i] = (10,0,0)
Assignment 8
Write a program to make every pixel that is divisible by either 3 or 4 green, while making the others red.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
for i in range(20):
if ( ((i % 3) == 0) or ((i % 4) == 0) ):
pixels[i] = (0,10,0)
else:
pixels[i] = (10,0,0)
Assignment 9
Write a program to make every pixel that is NOT divisible by either 3 or 4 green, while making the others red.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
for i in range(20):
if ( ((i % 3) == 0) or ((i % 4) == 0) ):
pixels[i] = (0,10,0)
else:
pixels[i] = (10,0,0)