Buttons

Now let’s use the buttons on the device to turn individual LEDs on and off. We use the cp.button_a and cp.button_b properties to determine if the given button is being pressed down:

from adafruit_circuitplayground import cp

while True:
    if cp.button_a:
        cp.pixels[0] = (255, 255, 255)
    elif cp.button_b:
        cp.pixels[1] = (255, 255, 255)

Challenge Problem 1

Modify the code above so that only one pixel is on at a time. In other words, if multiple buttons are pressed in a sequence, only the most recent pixel stays on.

Hint: consider using cp.pixels.fill

Challenge Problem 2

Now modify the code so that each pixel is on only as long as the user holds down the button.

References

Thanks! We hope you found what you are looking for. Please feel free to contribute via Github.