- MicroPython Cookbook
- Marwan Alsabbagh
- 203字
- 2021-06-24 14:28:21
How to do it...
Let's perform the following steps:
- Run the following lines of code in the REPL:
>>> from adafruit_circuitplayground.express import cpx >>> import time >>> >>> BLACK = 0x000000 >>> BLUE = 0x0000FF >>> >>> cpx.pixels.brightness = 0.10 >>> i = 0 >>> direction = 1 >>> >>>
- Run the following code and press the push buttons to see the effect this has on the pixels:
>>> while True: ... if cpx.button_a: ... direction = 1 ... if cpx.button_b: ... direction = -1 ... i += direction ... i = i % 10 ... cpx.pixels.fill(BLACK) ... cpx.pixels[i] = BLUE ... time.sleep(0.05) ...
- The code that follows combines all the code shown in this recipe to make one complete program. Add this block of code to the main.py file and it will change the direction of the lighted pixels from clockwise to counterclockwise each time push button A and push button B are pressed:
from adafruit_circuitplayground.express import cpx import time BLACK = 0x000000 BLUE = 0x0000FF cpx.pixels.brightness = 0.10 i = 0 direction = 1 while True: if cpx.button_a: direction = 1 if cpx.button_b: direction = -1 i += direction i = i % 10 cpx.pixels.fill(BLACK) cpx.pixels[i] = BLUE time.sleep(0.05)