MicroPython Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

Let's perform the following steps:

  1. Ensure that the slide switch is flipped to the left-hand side. Run the following lines of code in the REPL:
>>> from adafruit_circuitplayground.express import cpx
>>> cpx.switch
True
  1. Change the slide switch to be flipped to the right-hand side. Run the following code block:
>>> cpx.switch
False
  1. Add the following code to the main.py file and it will repeatedly print the state of the slide switch on execution. Turn the slide switch to the left and right to observe the change in output:
from adafruit_circuitplayground.express import cpx
import time

while True:
    print(cpx.switch)
    time.sleep(0.05)