-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12_button.py
52 lines (36 loc) · 1.39 KB
/
day12_button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Imports
import time
from machine import Pin
from neopixel import NeoPixel
# Define our button pin
button = Pin(15, Pin.IN, Pin.PULL_DOWN)
# Define the strip pin number (28) and number of LEDs (15)
strip = NeoPixel(Pin(28), 15)
# Colour variables
red = 255,0,0
green = 0,255,0
blue= 0,0,255
# Define colour list
colours = [red, green, blue]
# Create index variable starting at 0
myindex = 0
# Variable with the number of items in our list (3)
# We -1 as the index starts at 0, and we want to use this for the colour list index number (0, 1 or 2)
# This is useful as it means we don't have to count the colours if we add more
indexlength = len(colours) -1
while True: # Run forever
time.sleep(0.4) # Delay
if button() == 1: # If button pressed
# If the index variable is less than or equal to the lengh of the index
if myindex < indexlength:
# Add +1 to the index variable
myindex = myindex + 1
# If the index variable is over the index length
else:
# Set index variable back to 0 (the first item in our list)
myindex = 0
## Now this code runs AFTER the if statements...
# Fill the strip with the current list index colour
strip.fill((colours[myindex]))
# Write the data to the LED strip
strip.write()