We'll be using CircuitPython as an alternative language for this project. Are you new to using CircuitPython? No worries, there is a full getting started guide here.

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython. You can learn about Mu and its installation in this tutorial.

When the code starts it turns off the Neopixels and puts up the shades. Then it continually checks the light level and turns on the Neopixels if it's dark enough, or puts down the shades if it's bright enough. If neither is the case, it turns off the lights and puts up the shades.

# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Circuit Playground Express auto-sunglasses/flashlight

Adafruit invests time and resources providing this open source code.
Please support Adafruit and open source hardware by purchasing
products from Adafruit!

Written by Dave Astels for Adafruit Industries
Copyright (c) 2018 Adafruit Industries
Licensed under the MIT license.

All text above must be included in any redistribution.
"""

import time
import board
import pwmio
from adafruit_circuitplayground.express import cpx
from adafruit_motor import servo

pwm = pwmio.PWMOut(board.A1, duty_cycle=2 ** 15, frequency=50)
my_servo = servo.Servo(pwm)

cpx.pixels.fill((0, 0, 0))
my_servo.angle = 90

while True:
    light_level = cpx.light

    if light_level < 10:
        cpx.pixels.fill((200, 200, 200))
    else:
        cpx.pixels.fill((0, 0, 0))
        if light_level < 200:
            my_servo.angle = 90
        else:
            my_servo.angle = 0

    time.sleep(0.25)

This guide was first published on Aug 15, 2018. It was last updated on Aug 15, 2018.

This page (Code with CircuitPython) was last updated on Sep 30, 2023.

Text editor powered by tinymce.