Skip to content

Instantly share code, notes, and snippets.

@Sigafoos Sigafoos/pinewood_derby.py
Last active Jan 14, 2018

Embed
What would you like to do?
WIP derby code. Uses a Circuit Playground Express and OLED Featherwing
import time
import adafruit_lis3dh
import adafruit_ssd1306
import board
import busio as io
from digitalio import DigitalInOut, Direction, Pull
import neopixel
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.2)
BLUE = (0, 0, 0x10)
GOLD = (0x10, 0x10, 0)
RED = (0x10, 0, 0)
GREEN = (0, 0x10, 0)
# attempt to open the log for writing. if we can't, it wasn't enabled in boot.py
try:
fp = open("/derby.txt", "a")
except OSError as e:
fp = None
# green if set to off via switch, red if out of space
color = GREEN if e.args[0] != 28 else RED
pixels.fill(color)
pixels.show()
def write(fp, text):
if fp is not None:
fp.write("{}\n".format(text))
fp.flush()
print(text)
write(fp, "\n\n=====")
try:
oled_i2c = io.I2C(board.SCL, board.SDA)
oled = adafruit_ssd1306.SSD1306_I2C(128, 32, oled_i2c)
except ValueError:
print("fatal error: no I2C OLED")
def display(oled, x, y, z):
oled.fill(0)
oled.text(" X Y Z", 0, 0)
oled.text("{: .1f} {: .1f} {: .1f}".format(x, y, z), 0, 10)
oled.text(" {} {} {}".format(*[str(abs(v))[3:6] for v in (x, y, z)]), 0, 20)
acc_i2c = io.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
accelerometer = adafruit_lis3dh.LIS3DH_I2C(acc_i2c, address=0x19)
# for starting a heat
button = DigitalInOut(board.BUTTON_A)
button.direction = Direction.INPUT
button.pull = Pull.DOWN
start = False
pixel_start = 0
while True:
oled.fill(0)
if button.value:
start = not start
if start:
x, y, z = accelerometer.acceleration
print(x, y, z)
write(fp, "{}\t{}\t{}".format(x, y, z))
display(oled, x, y, z)
for j in range(len(pixels)):
pixels[j] = BLUE if int((j + pixel_start) / 5) % 2 == 0 else GOLD
pixel_start = (pixel_start + 1) % len(pixels)
else:
pixels.fill((0,0,0))
oled.text(" derby.Car()", 0, 0)
oled.text("waiting for", 0, 15)
oled.text("button push...", 0, 25)
pixels.show()
oled.show()
time.sleep(0.1)
@Sigafoos

This comment has been minimized.

Copy link
Owner Author

Sigafoos commented Dec 31, 2017

@Sigafoos

This comment has been minimized.

Copy link
Owner Author

Sigafoos commented Jan 14, 2018

c6b2cc16afb5a83e50d276ea39034bf108d4da68 doesn't handle values >9 well (possibly an argument for displaying in Gs rather than m/s^2...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.