活动链接https://www.eeworld.com.cn/huodong/digikey_follow_me_2024/
物料介绍:
这次活动使用的板子是adfruit家的Circuit Playground Expresses,主要特性如下:
• 10颗Mini NeoPixel LED,每个LED均可显示任何彩虹颜色
• 1个运动传感器(LIS3DH 3轴XYZ加速度计),带有轻击和自由落体检测功能
• 1个温度传感器 (MMBT2222)
• 1个光传感器 (ALSPT1931),也可以用作颜色或脉冲传感器
• 1个声音传感器(MEMS麦克风)
• 红外发射器和接收器 (DSOP38338),可发射和接收远程控制代码,在Circuit Playground Expresses之间发送消息,或用作接近传感器
• 1个带D类放大器的迷你扬声器(7.5mm磁性扬声器/蜂鸣器)
• 2个按钮,标记为A和B
• 1个滑动开关
• 8个易于使用的鳄鱼夹输入/输出引脚(含I2C、UART、8个可进行模拟输入的引脚/多PWM 输出)
• 7个焊盘可用作电容式触摸输入, 剩余 焊盘是一个真模拟输出
• 绿色“ON”LED
• 红色“#13”LED,用于基本闪烁
• 复位按钮
外观指示图
入门任务(必做):开发环境搭建,板载LED点亮
官方主页https://learn.adafruit.com/adafruit-circuit-playground-express/overview介绍了几种开发环境,这里选择熟悉的CircuitPython,将uf2固件下载后,按下RESET按钮,出现虚拟磁盘,见uf2固件复制到磁盘即可。
基础任务一(必做):控制板载炫彩LED,跑马灯点亮和颜色变换
主要物料:Circuit Playground Expresses板载RGB LED
官方提供了跑马灯例程https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-neopixel#
# Circuit Playground NeoPixel
import time
import board
from rainbowio import colorwheel
import neopixel
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.2, auto_write=False)
# choose which demos to play
# 1 means play, 0 means don't!
color_chase_demo = 1
flash_demo = 1
rainbow_demo = 1
rainbow_cycle_demo = 1
def color_chase(color, wait):
for i in range(10):
pixels[i] = color
time.sleep(wait)
pixels.show()
time.sleep(0.5)
def rainbow_cycle(wait):
for j in range(255):
for i in range(10):
rc_index = (i * 256 // 10) + j * 5
pixels[i] = colorwheel(rc_index & 255)
pixels.show()
time.sleep(wait)
def rainbow(wait):
for j in range(255):
for i in range(len(pixels)):
idx = int(i + j)
pixels[i] = colorwheel(idx & 255)
pixels.show()
time.sleep(wait)
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
OFF = (0, 0, 0)
while True:
if color_chase_demo:
color_chase(RED, 0.1) # Increase the number to slow down the color chase
color_chase(YELLOW, 0.1)
color_chase(GREEN, 0.1)
color_chase(CYAN, 0.1)
color_chase(BLUE, 0.1)
color_chase(PURPLE, 0.1)
color_chase(OFF, 0.1)
if flash_demo:
pixels.fill(RED)
pixels.show()
# Increase or decrease to change the speed of the solid color change.
time.sleep(1)
pixels.fill(GREEN)
pixels.show()
time.sleep(1)
pixels.fill(BLUE)
pixels.show()
time.sleep(1)
pixels.fill(WHITE)
pixels.show()
time.sleep(1)
if rainbow_cycle_demo:
rainbow_cycle(0.05) # Increase the number to slow down the rainbow.
if rainbow_demo:
rainbow(0.05) # Increase the number to slow down the rainbow.
基础任务二(必做):监测环境温度和光线,通过板载LED展示舒适程度
主要物料:Circuit Playground Expresses板载RGB LED和温度传感器
思路是通过读取温度和光线传感器数据,不同阈值显示不同颜色。
import time
from adafruit_circuitplayground import cp
cp.detect_taps = 1
cp.pixels.auto_write = False
cp.pixels.brightness = 0.3
while True:
print(cp.temperature)
print(cp.light)
if cp.light>15:
cp.pixels[0] = (255, 0, 0)
else:
cp.pixels[0] = (0, 0, 0)
if cp.temperature>27:
cp.pixels[1] = (255, 0, 0)
else:
cp.pixels[1] = (0, 0, 0)
cp.pixels.show()
time.sleep(0.1)
基础任务三(必做):接近检测——设定安全距离并通过板载LED展示,检测到入侵时,发起声音报警
主要物料:Circuit Playground Expresses板载RGB LED和蜂鸣器、红外发射器
思路是根据板子上的红外发射传感器实现。
from adafruit_circuitplayground import cp
from adafruit_circuitplayground.express import cpx
from digitalio import DigitalInOut, Direction,Pull
from analogio import AnalogIn
import time
import board
def wheel(pos):
#input a value 0 to 255 to get a color value.
#The colors are a transition r - g - b - back to r.
if(pos<0) or (pos)>255:
return (0,0,0)
if pos<85:
return(int(255-pos*3),int(pos*3),0)
elif pos<170:
pos -=85
return (0,int(255-pos*3),int(pos*3))
else:
pos -=170
return(int(pos*3),0,int(255-pos*3))
cp.pixels.brightness = 0.2
ir_tx = DigitalInOut(board.IR_TX)
ir_tx.direction = Direction.OUTPUT
proximity = AnalogIn(board.IR_PROXIMITY)
while True:
ir_tx.value=True
time.sleep(0.1)
ir_tx.value = False
proximity_value = proximity.value
print("proximity=%d" % proximity_value)
max_value=42500
min_value=31500
interval_value = (max_value - min_value)/11
proximity_index = (proximity_value - min_value)//interval_value
for p in range(10):
if p<= proximity_index:
cpx.pixels[p] = tuple(int(c*((10-p%10))/10.0) for c in wheel(25*(p%10)))
else:
cpx.pixels[p] = (0,0,0)
if proximity_index >5:
cp.play_tone(500,0.5)
进阶任务(必做):制作不倒翁——展示不倒翁运动过程中的不同灯光效果
主要物料:Circuit Playground Expresses板载RGB LED和加速度传感器
思路是读取加速度传感器的数据,只要有运动就将运动映射到点亮不同的LED。
import time
import math
import board
import busio
import neopixel
import adafruit_lis3dh
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
num_pixels = 10
pixels = neopixel.NeoPixel(board.NEOPIXEL, num_pixels, brightness=.05, auto_write=False)
def get_tilt_index(x, y):
angle = math.atan2(y, x) * 180 / math.pi
if angle < 0:
angle += 360
index = int((angle + 15) // 30) % num_pixels
return index
while True:
x, y, z = lis3dh.acceleration
index = get_tilt_index(x, y)
pixels.fill((0, 0, 0))
pixels[index] = (255, 0, 0)
pixels.show()
time.sleep(0.2)
代码包分享:
活动体验总结:
很高兴又一次参加了论坛的活动,通过这次任务熟悉了CircuitPython的玩法,adfruit家板子一如既往的好用,digkey买板子一如既往的快,感谢赞助方。
大学堂视频:
https://training.eeworld.com.cn/course/68737
本帖最后由 dql2016 于 2024-10-27 10:45 编辑