Install CircuitPython

The first thing to do is install CircuitPython on your Funhouse. Follow this guide to get it set up with the latest release version.

Text Editor

Adafruit recommends using the Mu editor for using your CircuitPython code with the Funhouse. You can get more info in this guide.

Shhhh... Secrets

In order for the Funhouse to connect to the internet, you'll need to include a secrets.py file on the board that contains your WiFi access point ssid and password, as well as your AIO key.

Follow this guide page to get your secrets.py file set up.

Download the Project Bundle

Your project will use a specific set of CircuitPython libraries and the code.py file. In order to get the libraries you need, click on the Download Project Bundle link below, and uncompress the .zip file.

# SPDX-FileCopyrightText: Copyright (c) 2021 John Park for Adafruit
#
# SPDX-License-Identifier: MIT
# FunHouse Parking Assistant

import time
import board
import adafruit_hcsr04
import neopixel
from adafruit_funhouse import FunHouse

SLOW_DISTANCE = 30  # distance (in centimeters) when you should slow
STOP_DISTANCE = 8  # distance when you should hit those brakes

GREEN = 0x00FF00
AMBER = 0xF0D000
RED = 0xFF0000


funhouse = FunHouse(default_bg=None, scale=3)
funhouse.peripherals.dotstars.brightness = 0.05
funhouse.peripherals.dotstars.fill(GREEN)

pixel_pin = board.A2
num_pixels = 30
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)

sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.A0, echo_pin=board.A1)

while True:
    try:
        print((sonar.distance,))

        if sonar.distance > SLOW_DISTANCE:
            funhouse.peripherals.dotstars.fill(GREEN)
            pixels.fill(GREEN)
            pixels.show()
        elif sonar.distance < SLOW_DISTANCE and sonar.distance > STOP_DISTANCE:
            funhouse.peripherals.dotstars.fill(AMBER)
            pixels.fill(AMBER)
            pixels.show()
            funhouse.peripherals.play_tone(1000, 0.3)
        elif sonar.distance < STOP_DISTANCE:
            funhouse.peripherals.dotstars.fill(RED)
            pixels.fill(RED)
            pixels.show()
            funhouse.peripherals.play_tone(2600, 0.3)

    except RuntimeError:
        print("Retrying!")
    time.sleep(0.01)

Next, drag the contents of the uncompressed bundle directory onto you microcontroller board CIRCUITPY drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.

How it Works

First, you'll load the necessary libraries, including  adafruit_hcsr04 for using the ultrasonic distance sensor.

import time
import board
import adafruit_hcsr04
import neopixel
from adafruit_funhouse import FunHouse

Variables

Next, you'll set variables for the distance thresholds, in centimeters. You can adjust these as necessary for your particular garage/parking space and vehicle.

SLOW_DISTANCE = 30  # distance (in centimeters) when you should slow
STOP_DISTANCE = 8  # distance when you should hit those brakes

FunHouse and NeoPixels

Now, you'll set up the FunHouse and the NeoPixels.

GREEN = 0x00FF00
AMBER = 0xF0D000
RED = 0xFF0000


funhouse = FunHouse(default_bg=None, scale=3)
funhouse.peripherals.dotstars.brightness = 0.05
funhouse.peripherals.dotstars.fill(GREEN)

pixel_pin = board.A2
num_pixels = 30
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)

Sonar

The ultrasonic distance sensor is set up on the FunHouse A0 pin for triggering a ping, and A1 for receiving the echo.

sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.A0, echo_pin=board.A1)

Main Loop

Here's what happens in the main loop of the program. Inside of a try-except loop, the sonar.distance value is checked.

If it is greater than the SLOW_DISTANCE the NeoPixels are set to green.

If it is less than the SLOW DISTANCE and greater than the STOP DISTANCE, the NeoPixels are set to yellow and the FunHouse speaker plays a medium tone.

If the distance is less than the STOP_DISTANCE, the NeoPixels are set to red and the tone plays at a higher pitch.

while True:
    try:
        print((sonar.distance,))

        if sonar.distance > SLOW_DISTANCE:
            funhouse.peripherals.dotstars.fill(GREEN)
            pixels.fill(GREEN)
            pixels.show()
        elif sonar.distance < SLOW_DISTANCE and sonar.distance > STOP_DISTANCE:
            funhouse.peripherals.dotstars.fill(AMBER)
            pixels.fill(AMBER)
            pixels.show()
            funhouse.peripherals.play_tone(1000, 0.3)
        elif sonar.distance < STOP_DISTANCE:
            funhouse.peripherals.dotstars.fill(RED)
            pixels.fill(RED)
            pixels.show()
            funhouse.peripherals.play_tone(2600, 0.3)

    except RuntimeError:
        print("Retrying!")
    time.sleep(0.01)

This guide was first published on Jun 02, 2021. It was last updated on 2021-06-02 09:57:42 -0400.

This page (Code the FunHouse Parking Assistant) was last updated on May 15, 2022.

Text editor powered by tinymce.