211 views|0 replies

650

Posts

8

Resources
The OP
 

【Follow me Season 2 Episode 1】Squidward——Voice Squidward [Copy link]

 

# 【Follow me Season 2 Episode 1】Squidward——Voice Squidward

## 1. Task Introduction

The task of this task is to control the rotation of the servo according to the ambient sound. Here, the microphone sensor on the Adafruit Circuit Playground Express development board is used to collect sound, and the servo module FS90R is added to simulate the tentacles of an octopus. At the same time, the LED on the development board is used to display the status.

## 2. Software Flowchart

## 3. Task code

Here we mainly use microphones, servos and board LED light modules; the servo module mainly controls rotation through PWM signals. Here I use A2 pin control, and two APIs operate forward and reverse respectively; the second is the microphone module. We use an array to cache microphone data and collect microphone data every 0.2s. After collecting the data, the power value is calculated. If the power is greater than 200 for 5 consecutive times, the servo is started to rotate forward to simulate the contraction of octopus tentacles, and the LED displays red at the same time; if it is less than 200 for 5 consecutive times, the mission is safe, the servo is started to reverse, the octopus tentacles are simulated to recover, and the LED displays green at the same time.

import time
import board
import digitalio
import audiobusio
import array
import math
from adafruit_circuitplayground import cp
# 创建一个数字输出引脚
pin = digitalio.DigitalInOut(board.A2)

# 设置引脚为输出模式
pin.switch_to_output()
cp.pixels.brightness = 0.3
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16)
array_data = array.array("H", [0] * 320)

def monitor_up():
    duty_cycle = 0.07
    frequency = 0.05  # 频率 20 Hz
    for i in range(10):
        pin.value = True
        time.sleep(frequency * duty_cycle)  # 打开时间
        pin.value = False
        time.sleep(frequency * (1 - duty_cycle))  # 关闭时间
    time.sleep(1)
def monitor_down():
    duty_cycle = 0.01 
    frequency = 0.05  # 频率 20 Hz
    for i in range(10):
        pin.value = True
        time.sleep(frequency * duty_cycle)  # 打开时间
        pin.value = False
        time.sleep(frequency * (1 - duty_cycle))  # 关闭时间
    time.sleep(1)

flag = False
l_cnt = 0
h_cnt = 0
while True:
    mic.record(array_data, len(array_data))

    avr= int( sum(array_data) / len(array_data) )
    power = sum([ float(wav_data - avr) * (wav_data - avr) for wav_data in array_data] )
    sound = math.sqrt(power / len(array_data))
    print(sound)
    if sound > 200:
        h_cnt = h_cnt +1
        l_cnt = 0
        if h_cnt >=5:
            cp.pixels.fill((255, 0, 0))
            if flag == False: 
                monitor_up()
                flag = True
    else:
        l_cnt = l_cnt +1
        h_cnt = 0
        if l_cnt >=5:
            cp.pixels.fill((0, 255, 0))
            if flag == True:
                monitor_down()
                flag = False
    time.sleep(0.2)

    

## 4. Summary

Finally, this development board has rich resources and is easy to program, especially MakeCode programming, which can be used with basically no basic knowledge. It is very suitable for children to program in the future, so I plan to use this board with children to complete the above tasks in the future, and then share it with you. Thanks to EE and DigiKey

##5. Demonstration Video

3ee0af168c981408b6206aeb61f44b07

code.rar

2.61 KB, downloads: 0

This post is from DigiKey Technology Zone
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list