|
| 1 | +# K.I.T.T. BikeLight |
| 2 | +# Use Button A to change color |
| 3 | +# Use Button B to change style |
| 4 | +# Ride safe and don't Hassle the Hoff |
| 5 | +# |
| 6 | +# Created by Eric Gross. Sublime Text on MacBookPro on 5/27/19. |
| 7 | +# CodeCademy 2019: Hardware Programming with CircuitPython: BikeLight |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +from adafruit_circuitplayground.express import cpx |
| 12 | +import time |
| 13 | + |
| 14 | +colorIndex = 0 |
| 15 | +functionIndex = 0 |
| 16 | +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)) |
| 17 | + |
| 18 | + |
| 19 | +###### Function to Change Color ###### |
| 20 | +def checkPress(colorIndex): |
| 21 | + if cpx.button_a: |
| 22 | + colorIndex += 1 |
| 23 | + if colorIndex > 10: |
| 24 | + return 0 |
| 25 | + return colorIndex |
| 26 | + |
| 27 | +###### Function to Change Function ###### |
| 28 | +def functionPress(functionIndex): |
| 29 | + if cpx.button_b: |
| 30 | + functionIndex += 1 |
| 31 | + if functionIndex > 2: |
| 32 | + return 0 |
| 33 | + return functionIndex |
| 34 | + |
| 35 | + |
| 36 | +###### Color and Blinking ##### |
| 37 | +def blinkLed(colorIndex, d = 0.5, ledIndex = 10, turnOff = True): |
| 38 | + if ledIndex == 10: |
| 39 | + cpx.pixels.fill((rainbow[colorIndex][0],rainbow[colorIndex][1],rainbow[colorIndex][2])) |
| 40 | + time.sleep(d) |
| 41 | + else: |
| 42 | + cpx.pixels[ledIndex] = (rainbow[colorIndex][0],rainbow[colorIndex][1],rainbow[colorIndex][2]) |
| 43 | + time.sleep(d) |
| 44 | + if turnOff == True: |
| 45 | + cpx.pixels.fill((0,0,0)) |
| 46 | + time.sleep(d) |
| 47 | + return |
| 48 | + |
| 49 | + |
| 50 | +def kitt(d = 0.1): |
| 51 | + global colorIndex |
| 52 | + colorIndex = checkPress(colorIndex) |
| 53 | + for x in range(0,10): |
| 54 | + blinkLed(10, d, x, False) |
| 55 | + blinkLed(colorIndex, 0, x, False) |
| 56 | + time.sleep(d) |
| 57 | + colorIndex = checkPress(colorIndex) |
| 58 | + for x in range(9,-1,-1): |
| 59 | + blinkLed(10, d, x, False) |
| 60 | + blinkLed(colorIndex, 0, x, False) |
| 61 | + time.sleep(d) |
| 62 | + return |
| 63 | + |
| 64 | +def rainbowSpin(d = 0.1): |
| 65 | + global colorIndex |
| 66 | + for x in range(0,10): |
| 67 | + blinkLed(colorIndex, d, x, False) |
| 68 | + colorIndex += 1 |
| 69 | + if colorIndex > 10: |
| 70 | + colorIndex = 0 |
| 71 | + return |
| 72 | + |
| 73 | + |
| 74 | +########################## |
| 75 | +###### Main Program ###### |
| 76 | +########################## |
| 77 | +while True: |
| 78 | + if cpx.switch: |
| 79 | + functionIndex = functionPress(functionIndex) |
| 80 | + colorIndex = checkPress(colorIndex) |
| 81 | + if functionIndex == 0: |
| 82 | + blinkLed(colorIndex) |
| 83 | + elif functionIndex == 1: |
| 84 | + kitt() |
| 85 | + else: |
| 86 | + rainbowSpin() |
| 87 | + else: |
| 88 | + # shut it |
| 89 | + cpx.pixels.fill((0, 0, 0)) |
0 commit comments