-
Notifications
You must be signed in to change notification settings - Fork 14
/
kitt.py
executable file
·86 lines (77 loc) · 2.19 KB
/
kitt.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# K.I.T.T. Bike Light
#
# Use Button A to change color
# Use Button B to change style
# Use Slide Switch to power on/off
# Ride safe and don't hassle the Hoff
#
# Created by Eric Gross. Sublime Text on MacBook Pro (5/27/2019)
# Codecademy: Learn Hardware Programming with CircuitPython - Bike Light
from adafruit_circuitplayground.express import cpx
import time
colorIndex = 0
functionIndex = 0
rainbow = ((255, 0, 0), (255, 85, 0), (255, 255, 0), (0, 255, 0), (34, 139, 34), (0, 255, 255), (0, 0, 255), (0, 0, 139), (255, 0, 255), (75, 0, 130), (0,0,0))
###### Function to Change Color ######
def checkPress(colorIndex):
if cpx.button_a:
colorIndex += 1
if colorIndex > 10:
return 0
return colorIndex
###### Function to Change Function ######
def functionPress(functionIndex):
if cpx.button_b:
functionIndex += 1
if functionIndex > 2:
return 0
return functionIndex
###### Color and Blinking ######
def blinkLed(colorIndex, d = 0.5, ledIndex = 10, turnOff = True):
if ledIndex == 10:
cpx.pixels.fill((rainbow[colorIndex][0], rainbow[colorIndex][1], rainbow[colorIndex][2]))
time.sleep(d)
else:
cpx.pixels[ledIndex] = (rainbow[colorIndex][0], rainbow[colorIndex][1], rainbow[colorIndex][2])
time.sleep(d)
if turnOff == True:
cpx.pixels.fill((0, 0, 0))
time.sleep(d)
return
def kitt(d = 0.1):
global colorIndex
colorIndex = checkPress(colorIndex)
for x in range(0, 10):
blinkLed(10, d, x, False)
blinkLed(colorIndex, 0, x, False)
time.sleep(d)
colorIndex = checkPress(colorIndex)
for x in range(9, -1, -1):
blinkLed(10, d, x, False)
blinkLed(colorIndex, 0, x, False)
time.sleep(d)
return
def rainbowSpin(d = 0.1):
global colorIndex
for x in range(0, 10):
blinkLed(colorIndex, d, x, False)
colorIndex += 1
if colorIndex > 10:
colorIndex = 0
return
##########################
###### Main Program ######
##########################
while True:
if cpx.switch:
functionIndex = functionPress(functionIndex)
colorIndex = checkPress(colorIndex)
if functionIndex == 0:
blinkLed(colorIndex)
elif functionIndex == 1:
kitt()
else:
rainbowSpin()
else:
# shut it
cpx.pixels.fill((0, 0, 0))