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

How to do it...

Let's perform the following steps for this recipe:

  1. Run the following lines of code in the REPL. You should hear a high-pitched beeping sound for 0.5 seconds:
>>> from adafruit_circuitplayground.express import cpx
>>> 
>>> BEEP_HIGH = 960
>>> BEEP_LOW = 800
>>> 
>>> cpx.play_tone(BEEP_HIGH, 0.5)
  1. Use the following code to play a low-pitched beeping sound for 0.5 seconds:
>>> cpx.play_tone(BEEP_LOW, 0.5)
  1. Use the following code to play a siren that goes from a high to a low pitch through three cycles, playing for a total of three seconds:
>>> for i in range(3):
...     cpx.play_tone(BEEP_HIGH, 0.5)
...     cpx.play_tone(BEEP_LOW, 0.5)
... 
>>> 
  1. The code that follows combines all the code shown in this recipe to make one complete program. Add this to the main.py file and it will play the siren alarm for three seconds every time you reload the code:
from adafruit_circuitplayground.express import cpx

BEEP_HIGH = 960
BEEP_LOW = 800

for i in range(3):
    cpx.play_tone(BEEP_HIGH, 0.5)
    cpx.play_tone(BEEP_LOW, 0.5)