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

IMG_1847.jpg

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

IMG_1819.jpg
Makerbot-Sketch-Classroom-3D-printer-e1615455857714.png
hot glue gun.jpg
solder kit.jpg
IMG_1846.jpg

3D Printing

Screenshot 2023-11-26 174522.png
Screenshot 2023-11-27 135141.png
Screenshot 2023-11-27 135150.png
Image 11-29-23 at 3.24 PM (1).jpeg

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

servomotor.jpg
IMG_1818.jpg
IMG_1819.jpg
  1. Hot glue the short end of the connector to the car (do this for both cars).
  2. Hot glue the long end to a side of the servo motor (do this for both connectors).
  3. Hot glue the CPX holder to the centerpiece (on a side without the openings for the wires).
  4. Hot glue the four quarters of the racetrack together to form a circular track.
  5. Insert the servo motor into the top of the centerpiece with its wires going inside the centerpiece.
  6. Run the wires through the centerpiece to where the CPX will be.
  7. Solder the wires connected to the servo motor to the CPX on the GND, VOUT, and A2 inputs.
  8. Code the CPX by connecting it to the computer and running the code previously made.
  9. Insert the battery into the centerpiece and run its wire to the CPX; then connect the wire to the CPX.
  10. Place the racetrack underneath the cars.
  11. Race the cars!