Using the Circuit Playground Express (CPX) Library
The adafruit.circuitplayground
library is designed to get you working quickly with easy to use code. It takes up a bit more memory than lower-level calls (also available in the next section). If you are using Crickit with the Circuit Playground Express and are having memory issues, try the code below, otherwise use the following code to get started.
Here is code to read Buttons A, B, and the slide switch.
from adafruit_circuitplayground.express import cpx if cpx.button_a: # do something pass if cpx.button_b: # do something else pass if cpx.switch: # do more like silence a sound, etc. pass
Here is an example which lights certain NeoPixels when the switches are triggered:
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT from adafruit_circuitplayground.express import cpx while True: if cpx.button_a: cpx.pixels[0] = (0, 30, 0) else: cpx.pixels[0] = (0, 0, 0) if cpx.button_b: cpx.pixels[9] = (0, 30, 0) else: cpx.pixels[9] = (0, 0, 0) if cpx.switch: cpx.pixels[4] = (0, 0, 30) cpx.pixels[5] = (0, 0, 0) else: cpx.pixels[4] = (0, 0, 0) cpx.pixels[5] = (0, 0, 30)
Press Button A and NeoPixel 0 lights up, press B and NeoPixel 9 lights up. If the slide switch is left, NeoPixel 4 is blue, if the switch is to the right, NeoPixel 5 is blue.
Using Lower-Level Library Calls to Save Memory
The following code is very lightweight for reading the switches. It is perfect for when you want to add additional code such as Crickit or other library modules which may require a lot of memory.
Reading keys uses the following libraries:
-
digitalio
- we useDigitalInOut, Pull, Direction
-
board
- we use board definitions for the switches
The bare code to set up all three switches is as follows. You can comment out or delete any button that you don't plan to use.
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT from digitalio import DigitalInOut, Pull, Direction import board button_a = DigitalInOut(board.BUTTON_A) button_a.direction = Direction.INPUT button_a.pull = Pull.DOWN button_b = DigitalInOut(board.BUTTON_B) button_b.direction = Direction.INPUT button_b.pull = Pull.DOWN switch = DigitalInOut(board.SLIDE_SWITCH) switch.direction = Direction.INPUT switch.pull = Pull.UP # Get the values of the switches now last_buttona = button_a.value last_buttonb = button_b.value last_switch = switch.value
Here is sample code using the buttons to light the same LEDs as in the MakeCode button example. This requires also adding the neopixel
library.
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT from digitalio import DigitalInOut, Pull, Direction import board import neopixel # NeoPixel setup and blank out pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1) pixels.fill((0, 0, 0)) # button setup: A, B, and slide switch button_a = DigitalInOut(board.BUTTON_A) button_a.direction = Direction.INPUT button_a.pull = Pull.DOWN button_b = DigitalInOut(board.BUTTON_B) button_b.direction = Direction.INPUT button_b.pull = Pull.DOWN switch = DigitalInOut(board.SLIDE_SWITCH) switch.direction = Direction.INPUT switch.pull = Pull.UP # main program - light NeoPixels depending on switches while True: if button_a.value: pixels[0] = (0, 30, 0) else: pixels[0] = (0, 0, 0) if button_b.value: pixels[9] = (0, 30, 0) else: pixels[9] = (0, 0, 0) if switch.value: pixels[4] = (0, 0, 30) pixels[5] = (0, 0, 0) else: pixels[4] = (0, 0, 0) pixels[5] = (0, 0, 30)
Page last edited January 22, 2025
Text editor powered by tinymce.