Racing Cars: Circling Cars on Race Track
by mhaffner in Workshop > 3D Printing
94 Views, 0 Favorites, 0 Comments
Racing Cars: Circling Cars on Race Track
This device is activated by a button on the CPX. Once the button is pressed, the lights on the CPX will flash with the sounds it makes as it counts down to the cars starting their “race”. The cars will then start moving at a certain speed. When certain buttons are clicked, the cars can speed up or change direction. As the cars are “racing”, lights flash on the CPX. The cars and lights will stop when a loud noise is made.
Supplies
- Adafruit Circuit Playground Express Basekit
- Continuous Rotation Micro Servo
- 3D Printer
- Hot Glue Gun
- Battery Pack
- Wires
- Solder Kit
3D Printing
Download the following links as STL files. Then, import them into MakerBot (MakerBot website). Adjust the base so that only the car and connector have a base. Preview the print to then export it to your files as a MakerBot file. Put this file onto a flash drive and use it to print on a 3D printer for each piece.
STL file links:
Car and Connector (print twice on Sketch printer)
CPX Holder (Sketch printer)
Centerpiece (Sketch printer)
Track (print four times on Sketch printer)
Coding
Copy and paste the following code into the Mu editor:
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries
# SPDX-License-Identifier: MIT
from adafruit_circuitplayground.express import cpx
import array
import math
import audiobusio
import board
import time
import pwmio
from adafruit_motor import servo
# servo
pwm = pwmio.PWMOut(board.A1, frequency=50)
motor_direction = 0
my_servo = servo.ContinuousServo(pwm, min_pulse=400, max_pulse=2500)
# Sound Sensor
def mean(values):
return sum(values) / len(values)
def normalized_rms(values):
minbuf = int(mean(values))
sum_of_samples = sum(
float(sample - minbuf) * (sample - minbuf)
for sample in values
)
return math.sqrt(sum_of_samples / len(values))
mic = audiobusio.PDMIn(
board.MICROPHONE_CLOCK,
board.MICROPHONE_DATA,
sample_rate=16000,
bit_depth=16
)
samples = array.array('H', [0] * 160)
mic.record(samples, len(samples))
# Countdown
def flash_leds_and_play_tone():
for _ in range(3):
cpx.pixels.fill((50, 0, 0))
cpx.play_tone(261.63, 0.5)
cpx.pixels.fill((0, 0, 0))
time.sleep(0.5)
# LED control
def alternate_led_colors():
num_leds = 10
for i in range(num_leds):
if i % 2 == 0:
cpx.pixels[i] = (0, 20, 0)
else:
cpx.pixels[i] = (20, 0, 0)
cpx.pixels.show()
time.sleep(1)
for i in range(num_leds):
if i % 2 == 0:
cpx.pixels[i] = (20, 0, 0)
else:
cpx.pixels[i] = (0, 20, 0)
cpx.pixels.show()
time.sleep(1)
while True:
my_servo.throttle = motor_direction
if cpx.switch:
cpx.play_file("speed.wav")
flash_leds_and_play_tone()
cpx.pixels.fill((0, 50, 0))
cpx.play_tone(440.00, 1)
cpx.pixels.fill((0, 0, 0))
motor_direction = 0.15
if cpx.button_b:
motor_direction = -1
cpx.play_file("dip.wav")
alternate_led_colors()
if cpx.button_a:
motor_direction = 1
cpx.play_file("rise.wav")
alternate_led_colors()
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
time.sleep(.1)
if magnitude > 400:
motor_direction = 0
cpx.pixels.fill((0, 0, 0))
Assembly
- Hot glue the short end of the connector to the car (do this for both cars).
- Hot glue the long end to a side of the servo motor (do this for both connectors).
- Hot glue the CPX holder to the centerpiece (on a side without the openings for the wires).
- Hot glue the four quarters of the racetrack together to form a circular track.
- Insert the servo motor into the top of the centerpiece with its wires going inside the centerpiece.
- Run the wires through the centerpiece to where the CPX will be.
- Solder the wires connected to the servo motor to the CPX on the GND, VOUT, and A2 inputs.
- Code the CPX by connecting it to the computer and running the code previously made.
- Insert the battery into the centerpiece and run its wire to the CPX; then connect the wire to the CPX.
- Place the racetrack underneath the cars.
- Race the cars!