When last I wrote about the Feather RP2040 and the Featherwing OLED 128×32 ( https://arcanesciencelab.wordpress.com/2022/02/05/tinkering-with-the-black-adafruit-feather-rp2040/ ), I’d mentioned I’d like to enable the three switches on the edge of the Featherwing labeled ‘A’, ‘B’, and ‘C’. The code posted herewith has that enabled. I now have both the Featherwing and the NeoPixel ring attached to the Feather RP2040. I had to make a slight change to what the NeoPixel ring’s DIN (data in) line was attached to because it turns out that the ‘A’ switch on the Featherwing wants to use that same pin as an input. You can see the code changes and additions in the highlighted sections below.
When the code is executing, pressing any of the buttons will print “Button … pressed” only once on the REPL. When that button is released then “Button … released” is printed. This is simple early code without anything substantial hooked into those button checks, but the framework presented here should inspire something more interesting.
The next stage will be to add code back in to manipulate the OLED display. I hope that code will be a bit more streamlined than my earlier example.
import time import board import neopixel from digitalio import DigitalInOut, Direction, Pull # Define all three buttons, A, B, and C. # a_pressed = b_pressed = c_pressed = False a_button = DigitalInOut(board.D9) a_button.direction = Direction.INPUT a_button.pull = Pull.UP b_button = DigitalInOut(board.D6) b_button.direction = Direction.INPUT b_button.pull = Pull.UP c_button = DigitalInOut(board.D5) c_button.direction = Direction.INPUT c_button.pull = Pull.UP # For the single neopixel on the Feather itself. # s_neopixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2, auto_write=False, pixel_order=neopixel.GRB ) # For the 12 neopixel ring attached to the Feather. # neopixel_ring = neopixel.NeoPixel( board.D10, 12, brightness=0.2, auto_write=True, pixel_order=neopixel.GRB ) def cycle_color(color): s_neopixel.fill(color) s_neopixel.show() for i in range(len(neopixel_ring)): neopixel_ring[i-1] = (0,0,0,0) # turn LED off neopixel_ring[i] = color neopixel_ring.show() time.sleep(.05) while True: if (a_button.value is not True): if (not a_pressed): print('Button A pressed') a_pressed = True else: if (a_pressed): print('Button A released') a_pressed = False if (b_button.value is not True): if (not b_pressed): print('Button B pressed') b_pressed = True else: if (b_pressed): print('Button B released') b_pressed = False if (c_button.value is not True): if (not c_pressed): print('Button C pressed') c_pressed = True else: if (c_pressed): print('Button C released') c_pressed = False cycle_color((64,0,0,0)) # red cycle_color((0,64,0,0)) # green cycle_color((0,0,64,0)) # blue cycle_color((64,16,0,0)) # orange cycle_color((0,32,32,0)) # cyan cycle_color((0,0,0,0)) # black (LED off)