Code with CircuitPython

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.

"""
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 simpleio
from adafruit_circuitplayground.express import cpx

servo = simpleio.Servo(board.A1)

cpx.pixels.fill((0, 0, 0))
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:
            servo.angle = 90
        else:
            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 Oct 14, 2019.