【Follow me第二季第1期】使用thonny完成入门任务三+进阶任务
[复制链接]
基础任务三(必做):接近检测——设定安全距离并通过板载LED展示,检测到入侵时,发起声音报警
搭配器件: Adafruit Circuit Playground Express
先来一段音乐:
from adafruit_circuitplayground.express import cpx
cpx.play_tone(220, 0.5) //220HZ 持续0.5S
两只老虎简谱:
最终程序:
from adafruit_circuitplayground.express import cpx
while True:
cpx.play_tone(1046, 0.5)
cpx.play_tone(1175, 0.5)
cpx.play_tone(1318, 0.5)
cpx.play_tone(1046, 0.5)
cpx.play_tone(1046, 0.5)
cpx.play_tone(1175, 0.5)
cpx.play_tone(1318, 0.5)
cpx.play_tone(1046, 0.5)
cpx.play_tone(1318, 0.5)
cpx.play_tone(1397, 0.5)
cpx.play_tone(1568, 1)
cpx.play_tone(1318, 0.5)
cpx.play_tone(1397, 0.5)
cpx.play_tone(1568, 1)
cpx.play_tone(1568, 0.25)
cpx.play_tone(1760, 0.25)
cpx.play_tone(1568, 0.25)
cpx.play_tone(1397, 0.25)
cpx.play_tone(1318, 0.5)
cpx.play_tone(1046, 0.5)
cpx.play_tone(1568, 0.25)
cpx.play_tone(1760, 0.25)
cpx.play_tone(1568, 0.25)
cpx.play_tone(1397, 0.25)
cpx.play_tone(1318, 0.5)
cpx.play_tone(1046, 0.5)
cpx.play_tone(1046, 0.5)
cpx.play_tone(784, 0.5)
cpx.play_tone(1046, 1)
cpx.play_tone(1046, 0.5)
cpx.play_tone(784, 0.5)
cpx.play_tone(1046, 1)
WIN_20240808_23_16_59_Pro
接近检测,参考了坛友的思路,利用光照传感器的感应,不过跟坛友的思路相反:
把全部白光LED灯打开,照度一般在4000+,用手接近的话,形成反射效果,照度瞬间上升至30000~40000+
只需要加个判断即可。
import board
import time
import analogio
import neopixel
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10)
for i in range(10):
pixels[i] = (255, 255, 255)
pixels.show()
light = analogio.AnalogIn(board.LIGHT)
while True:
time.sleep(1)
print(light.value)
if light.value >10000:
print("Hurry UP")
执行效果如图:(不知道为什么,加上音乐,程序总是不能正常跑,就只好没加了)
进阶任务(必做):制作不倒翁——展示不倒翁运动过程中的不同灯光效果
搭配器件: Adafruit Circuit Playground Express、不倒翁(自选搭配,例如扭蛋)
此处主要用到三轴加速度传感器,根据三轴加速度值赋予LED不同的颜色。
import time
import board
from adafruit_circuitplayground import cp
cp.pixels.auto_write = False
cp.pixels.brightness = 0.3
while True:
cp.pixels.fill((0, 0, 0))
if not cp.shake():
x, y, z = cp.acceleration
r = int(abs(x) / 10 * 255)
g = int(abs(y) / 10 * 255)
b = int(abs(z) / 10 * 255)
cp.pixels.fill((r, g, b))
cp.pixels.show()
time.sleep(0.1)
由于家里没有不倒翁,只好找来一只鳄鱼代劳下了,晃动鳄鱼,LED变换不同的色彩。
今天的分享就到这里,晚安~
|