208 views|1 replies

183

Posts

12

Resources
The OP
 

【Follow me Season 2 Episode 1】Creative Task 2: Squidward [Copy link]

 This post was last edited by nemon on 2024-8-31 19:14

The task of creative task two is to make an octopus, whose tentacles expand or contract according to the size of the surrounding sound.

In order to make the expansion and contraction of the antennae the same, I did not use a 360-degree servo, but instead used a 28BYJ48 stepper motor driven by ULN2003.

In order to make the operation easier, a file Stepper28BYJ48_ULN2003.py was written first, which defines the class for operating the stepper motor.

During initialization, pass in the control pin, and then each time you take a step, you only need to pass the direction and speed.

import board,digitalio
import time
'''
EXAMPLE:
import Stepper28BYJ48_ULN2003

stepper = Stepper28BYJ48_ULN2003( board.A3 , board.A2 , board.A1 , board.A0 )
for i in range(4076):
    stepper.steper_one_step(1,100000)
'''
class Stepper28BYJ48_ULN2003:
    _DELAY_T = 0.15
    _POS_ARRY =(
        (False , True  ,True  ,True ),# A
        (False , False ,True  ,True ),# AB
        (True  , False ,True  ,True ),# B
        (True  , False ,False ,True ),# BC
        (True  , True  ,False ,True ),# C
        (True  , True  ,False ,False),# CD
        (True  , True  ,True  ,False),# D
        (False , True  ,True  ,False),# DA
    )
    # init
    def __init__(self,pin_a,pin_b,pin_c,pin_d):
        #self.speed = 1000
        #self.direction = 1
        self.pos = -1
        self.pins = [ digitalio.DigitalInOut(p) for p in (pin_a,pin_b,pin_c,pin_d) ]
        for p in self.pins:
            p.switch_to_output()
    # step
    def steper_one_step(self,d=1,speed=1000):
        self.pos += d #self.direction
        self.pos %= len(self._POS_ARRY)
        print(self.pos)
        #ctr_a.value , ctr_b.value , ctr_c.value , ctr_d.value = self._POS_ARRY[pos]
        for i in range(len(self.pins)):
            self.pins[i].value = self._POS_ARRY[self.pos][i]
        #time.sleep(self._DELAY_T/self.speed)
        time.sleep(self._DELAY_T/abs(speed))

To detect the volume of the sound, you can use the audiobusio library, feed it a buffer, and it will return a series of analog values.

By simplifying the official website code, you can detect the sound size:

import audiobusio,board,array,math,time

mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16)
samples = array.array("H", [0] * 320)

while True:
    mic.record(samples, len(samples))
    minbuf = int( sum(samples) / len(samples) )
    samples_sum = sum([ float(sample - minbuf) * (sample - minbuf) for sample in samples] )
    print( sum(samples) , minbuf , math.sqrt(samples_sum / len(samples)) )
    time.sleep(0.5)

After the octopus tentacles are retracted, press the switch and they will extend again. Because a stepper motor is used, as long as there is no loss of step, it will return to its original position after the same number of steps in the opposite direction.

The final complete code is this:

import board,time
from Stepper28BYJ48_ULN2003 import Stepper28BYJ48_ULN2003 as st
import audiobusio,board,array,math
import digitalio
from adafruit_circuitplayground import cp

N = 1/4
stepper = st( board.A3 , board.A2 , board.A1 , board.A6 )

mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16)
samples = array.array("H", [0] * 320)

button = digitalio.DigitalInOut(board.A5)
button.switch_to_input(pull=digitalio.Pull.DOWN)

while True:
    mic.record(samples, len(samples))
    minbuf = int( sum(samples) / len(samples) )
    samples_sum = sum([ float(sample - minbuf) * (sample - minbuf) for sample in samples] )
    val = math.sqrt(samples_sum / len(samples))
    print( sum(samples) , minbuf , val )
    if val > 300 :
        print('danger!')
        cp.play_file('danger.wav')
        for i in range(4076 * N):
            if i%100==0:
                print(i,end=' :-> ')
            stepper.steper_one_step(-1,100000)
        time.sleep(2)
        while button.value :
            time.sleep(0.05)
        print('safe.')
        cp.play_file('shoping.wav')
        for i in range(4076 * N):
            if i%100==0:
                print(i,end=' :-> ')
            stepper.steper_one_step( 1,100000)
        time.sleep(2)
    time.sleep(0.1)

The body of the octopus is made from a cup. Cut the cup wall into 8 strips, fold and punch holes. Then fix the stepper motor to the bottom of the cup, install a bottle cap on the shaft, punch 8 holes around the bottle cap, pass cotton thread through the holes, then through the holes of the folded cup wall, and wrap around the end of the tentacles, and it's done.

The running effect is as follows:


This post is from DigiKey Technology Zone

Latest reply

good   Details Published on 2024-8-31 21:01
 
 

3

Posts

0

Resources
2
 

good

This post is from DigiKey Technology Zone
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

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