Last active
January 29, 2019 16:16
-
-
Save pdbartsch/279083a2a1f4db694b05d7666b275e50 to your computer and use it in GitHub Desktop.
The code running lights and sounds on my outlaw class pinewood derby police car 2019 using Adafruit's Hallowing and CircuitPython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import array | |
import math | |
import board | |
import pulseio | |
import digitalio | |
import touchio | |
import audioio | |
import busio | |
import adafruit_lis3dh | |
import neopixel | |
import random | |
red = (255, 0, 0) | |
blue = (0, 0, 255) | |
# Setup LED pins | |
RED_PIN = board.D10 | |
BLUE_PIN = board.D11 | |
TAIL_PIN = board.D12 # brake lights | |
HEAD1_PIN = board.D5 # left headlight | |
HEAD2_PIN = board.D9 # right headlight | |
# Setup PIR pins | |
PIR_PIN = board.SENSE # Pin port connected to PIR sensor output wire. | |
# Setup digital input for PIR sensor: | |
pir = digitalio.DigitalInOut(PIR_PIN) | |
pir.direction = digitalio.Direction.INPUT | |
# internal neopixel set to 0, the only way I could find to turn it off | |
inrgb = neopixel.NeoPixel(board.NEOPIXEL, 1) | |
inrgb.brightness = 0 | |
# Setup digital output for LED pins: | |
ledr = digitalio.DigitalInOut(RED_PIN) | |
ledr.direction = digitalio.Direction.OUTPUT | |
ledb = digitalio.DigitalInOut(BLUE_PIN) | |
ledb.direction = digitalio.Direction.OUTPUT | |
ledbrake = digitalio.DigitalInOut(TAIL_PIN) | |
ledbrake.direction = digitalio.Direction.OUTPUT | |
ledhead1 = digitalio.DigitalInOut(HEAD1_PIN) | |
ledhead1.direction = digitalio.Direction.OUTPUT | |
ledhead2 = digitalio.DigitalInOut(HEAD2_PIN) | |
ledhead2.direction = digitalio.Direction.OUTPUT | |
# Setup cap touch buttons | |
toucha2 = touchio.TouchIn(board.TOUCH4) | |
toucha5 = touchio.TouchIn(board.TOUCH1) | |
# Set up accelerometer on I2C bus, 4G range: | |
i2c = busio.I2C(board.SCL, board.SDA) | |
ACCEL = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x18) | |
ACCEL.range = adafruit_lis3dh.RANGE_4_G | |
def step_away(): | |
sound = ['Siren2.wav', 'IndustrialAlarm.wav', 'carhorn.wav', 'BeepCensor.wav', 'woopwoop.wav', 'dogwoof.wav'] | |
s = random.choice(sound) | |
wave_file = open(s, "rb") # open a wav file | |
wave = audioio.WaveFile(wave_file) | |
audio.play(wave) # play the wave file | |
# led.value = True | |
while audio.playing: # turn on LED, turn servo | |
alarm_lights(2, 0.2) | |
wave_file.close() # close the wav file | |
def race_ready(): | |
wave_file = open('siren.wav', "rb") # open a wav file | |
wave = audioio.WaveFile(wave_file) | |
audio.play(wave) # play the wave file | |
# led.value = True | |
while audio.playing: # turn on LED, turn servo | |
police_lights(1, 0.08) | |
wave_file.close() # close the wav file | |
def racing(): | |
wave_file = open('OpenPipeSymphony.wav', "rb") # open a wav file | |
wave = audioio.WaveFile(wave_file) | |
audio.play(wave) # play the wave file | |
# led.value = True | |
while audio.playing: # turn on LED, turn servo | |
police_lights(1, 0.08) | |
wave_file.close() # close the wav file | |
ledbrake.value = True | |
time.sleep(4) | |
ledbrake.value = False | |
time.sleep(.1) | |
ledbrake.value = True | |
time.sleep(.1) | |
ledbrake.value = False | |
time.sleep(.1) | |
ledbrake.value = True | |
time.sleep(.1) | |
ledbrake.value = False | |
# Setup audio out pin | |
audio = audioio.AudioOut(board.A0) | |
# function for police lights external | |
def police_lights(count, speed): | |
for _ in range(count): | |
ledr.value = True | |
ledhead1.value = True | |
ledhead2.value = False | |
time.sleep(speed) | |
ledr.value = False | |
ledhead1.value = False | |
ledhead2.value = True | |
time.sleep(speed) | |
ledb.value = True | |
ledhead1.value = True | |
ledhead2.value = False | |
time.sleep(speed) | |
ledb.value = False | |
ledhead1.value = False | |
ledhead2.value = True | |
time.sleep(speed) | |
ledhead2.value = False | |
def alarm_lights(count, speed): | |
for _ in range(count): | |
ledr.value = True | |
ledhead1.value = True | |
ledhead2.value = False | |
ledbrake.value = True | |
time.sleep(speed) | |
ledr.value = False | |
ledhead1.value = False | |
ledhead2.value = True | |
ledbrake.value = False | |
time.sleep(speed) | |
ledb.value = True | |
ledhead1.value = True | |
ledhead2.value = False | |
ledbrake.value = True | |
time.sleep(speed) | |
ledb.value = False | |
ledhead1.value = False | |
ledhead2.value = True | |
ledbrake.value = False | |
time.sleep(speed) | |
ledhead2.value = False | |
while True: | |
# feel for single taps | |
ACCEL.set_tap(1, 60) | |
if ACCEL.tapped: | |
print("Tapped!") | |
time.sleep(0.01) | |
# feel for shakes | |
if ACCEL.shake(shake_threshold=10): | |
print("Shaken!") | |
step_away() | |
# feel for downward track angle | |
try: | |
# ACCEL_X = ACCEL.acceleration[0] | |
# ACCEL_Y = ACCEL.acceleration[1] | |
ACCEL_Z = ACCEL.acceleration[2] # Read Z axis acceleration | |
except OSError: | |
pass | |
# print(ACCEL_Z) # uncomment to see the accelerometer z reading | |
# print(ACCEL_X, ACCEL_Y, ACCEL_Z) | |
if -8.55 <= ACCEL_Z <= -8.35: | |
# print(ACCEL_X, ACCEL_Y, ACCEL_Z) | |
# print('Ready to race!') | |
race_ready() | |
# # feel for downward track angle | |
# try: | |
# ACCEL_X = ACCEL.acceleration[0] | |
# ACCEL_Y = ACCEL.acceleration[1] | |
# ACCEL_Z = ACCEL.acceleration[2] # Read Z axis acceleration | |
# except OSError: | |
# pass | |
# print(ACCEL_X, ACCEL_Y, ACCEL_Z) | |
# time.sleep(0.5) | |
# feel for touches | |
if toucha2.value: | |
# print("touched 2!") | |
racing() | |
if toucha5.value: | |
# print("touched 5!") | |
racing() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment