🎄⛄❄️🎁 It's beginning to look a lot like ADABOX-mas! The next ADABOX from Adafruit ships soon! Get yours now, or give as a gift! 🎄⛄❄️🎁
0

a problem about The Scream: Interactive Screaming Painting
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

a problem about The Scream: Interactive Screaming Painting

by kiku on Sun Nov 03, 2019 10:20 pm

Hi! Sorry! I have a problem again.
I see a interesting project The Scream: Interactive Screaming Painting
so follow the steps of tutorial.
R0017418.JPG
R0017418.JPG (515.36 KiB) Viewed 340 times

I installled CircuitPython 4.1.0(adafruit-circuitpython-circuitplayground_express_crickit-en_US-4.1.0) on Circuit Playground Express and upload .wav and code.py to it.
2019-11-04.jpg
2019-11-04.jpg (72.36 KiB) Viewed 340 times

But it can't work
Where is wrong?
THANKS!

kiku
 
Posts: 7
Joined: Thu Oct 31, 2019 9:25 pm

Re: a problem about The Scream: Interactive Screaming Painti

by V2man on Sun Nov 03, 2019 11:56 pm

What's in your CIRCUITPY/lib directory?

V2man
 
Posts: 326
Joined: Mon Dec 03, 2018 12:38 am

Re: a problem about The Scream: Interactive Screaming Painti

by kiku on Mon Nov 04, 2019 2:43 am

It is empty
2019-11-04-1.jpg
2019-11-04-1.jpg (13.5 KiB) Viewed 321 times

kiku
 
Posts: 7
Joined: Thu Oct 31, 2019 9:25 pm

Re: a problem about The Scream: Interactive Screaming Painti

by V2man on Mon Nov 04, 2019 9:55 pm

What's in your CIRCUITPY/boot_out.txt file?

V2man
 
Posts: 326
Joined: Mon Dec 03, 2018 12:38 am

Re: a problem about The Scream: Interactive Screaming Painti

by kiku on Mon Nov 04, 2019 10:59 pm

Thanks you for reply
my boot_out.txt:
Adafruit CircuitPython 4.1.0 on 2019-08-02; Adafruit CircuitPlayground Express with Crickit libraries with samd21g18
Did I missing some important steps?
Thanks!

kiku
 
Posts: 7
Joined: Thu Oct 31, 2019 9:25 pm

Re: a problem about The Scream: Interactive Screaming Painti

by V2man on Tue Nov 05, 2019 1:00 am

With CIRCUITPY/boot_out.txt
Adafruit CircuitPython 4.1.0 on 2019-08-02; Adafruit CircuitPlayground Express with Crickit libraries with samd21g18

If you have not done so already from https://circuitpython.org/libraries download Bundle Version 4.x.x
Unzip adafruit-circuitpython-bundle-4.x-mpy-20191103.zip
The lib directory contains the libraries you will need to run your code.py.
Copy adafruit_crickit.mpy to CIRCUITPY/lib

Your code.py based on the Scream example has these statements:
import time
import math
import array
import audiobusio
import audioio
import board
from adafruit_crickit import crickit


All the libraries of the import statements above are built into the CircuitPython 4.1.0 you have on your CPX board. They came with the CircuitPython uf2 file you copied to the CPLAYBOOT drive. The file, adafruit_crickit.mpy satisfies the from statement.

If you want to know more about libraries read this post https://forums.adafruit.com/viewtopic.php?f=60&t=158205
If you do not want to worry about which libraries you need and are not concerned about storage space just copy over the entire contents of the lib directory from the bundle to the CIRCUITPY/lib drive. That's how I started out.

V2man
 
Posts: 326
Joined: Mon Dec 03, 2018 12:38 am

Re: a problem about The Scream: Interactive Screaming Painti

by V2man on Tue Nov 05, 2019 2:12 am

I also copy over the README.txt and VERSIONS.txt to the CIRCUITPY drive just to keep track of the date of the libraries.

V2man
 
Posts: 326
Joined: Mon Dec 03, 2018 12:38 am

Re: a problem about The Scream: Interactive Screaming Painti

by kiku on Wed Nov 06, 2019 12:07 am

Thank you for helping me
I follow your advice to copy adafruit_crickit.mpy to CIRCUITPY/lib
But.....
I don't know Which part of the problem ?
I am very frustrated.

kiku
 
Posts: 7
Joined: Thu Oct 31, 2019 9:25 pm

Re: a problem about The Scream: Interactive Screaming Painti

by V2man on Wed Nov 06, 2019 3:02 am

With power supplied to the Crickit board:
Do you have sound?
Is the servo motor moving?

Screamer: Interactive Screaming Painting by John Park
code.py from the-scream-munch-screaming-interactive-scream-painting.pdf
Have you installed a python editor like MU to check code.py for errors?

After using TAB and space your code.py should look like this:
If not copy the code below:

Code: Select all | TOGGLE FULL SIZE
import time
import math
import array
import audiobusio
import audioio
import board
from adafruit_crickit import crickit
# Number of samples to read at once.
NUM_SAMPLES = 160
# Remove DC bias before computing RMS.
def normalized_rms(values):
    minbuf = int(mean(values))
    samples_sum = sum(
        float(sample - minbuf) * (sample - minbuf)
        for sample in values
    )

    return math.sqrt(samples_sum / len(values))

def mean(values):
    return sum(values) / len(values)
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
                       sample_rate=16000, bit_depth=16)

# Record an initial sample to calibrate. Assume it's quiet when we start.
samples = array.array('H', [0] * NUM_SAMPLES)
mic.record(samples, len(samples))

head_servo = crickit.servo_1
head_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)
head_servo.angle = 90  # center the head.

# Set audio out on speaker.
a = audioio.AudioOut(board.A0)

# Start playing the file (in the background).
def play_file(wavfile):
    print("Playing scream!")
    with open(wavfile, "rb") as f:
        wav = audioio.WaveFile(f)
        a.play(wav)
        while a.playing:
            head_servo.angle = 60
            time.sleep(.01)
            head_servo.angle = 120
            time.sleep(.01)

while True:
    mic.record(samples, len(samples))
    magnitude = normalized_rms(samples)
    print(((magnitude),))  # formatting is for the Mu plotter.

if magnitude < 1000:  # it's quiet, do nothing.
    pass
else:
    print("LOUD")
    head_servo.angle = 60
    time.sleep(.05)
    head_servo.angle = 120
    time.sleep(.05)
    head_servo.angle = 90
    time.sleep(.02)
    play_file("scream_low.wav")
    head_servo.angle = 90
    time.sleep(2)

V2man
 
Posts: 326
Joined: Mon Dec 03, 2018 12:38 am

Re: a problem about The Scream: Interactive Screaming Painti

by V2man on Thu Nov 07, 2019 9:07 pm

Did the code.py you were using look like the example I posted? Spaces and Indentations are very important in CircuitPython. If not correct they will keep your code.py from executing.

V2man
 
Posts: 326
Joined: Mon Dec 03, 2018 12:38 am

Re: a problem about The Scream: Interactive Screaming Painti

by kiku on Fri Nov 08, 2019 2:49 am

I tried many ways,but it still.....
It's too difficult for me
I decide to give up temporarily
Sorry for waste your times
Thanks you so much.

kiku
 
Posts: 7
Joined: Thu Oct 31, 2019 9:25 pm

Re: a problem about The Scream: Interactive Screaming Painti

by V2man on Fri Nov 08, 2019 10:12 am

Don't get discouraged! To climb a mountain you must take one step at a time. Sometimes the steps are not always up. We learn much more when we face obstacles than when the path is clear and easy.

Can you still see the CIRCUITPY or CPLAYBOOT drive on your computer?

V2man
 
Posts: 326
Joined: Mon Dec 03, 2018 12:38 am

Re: a problem about The Scream: Interactive Screaming Painti

by V2man on Sun Nov 10, 2019 10:12 pm

Here's an updated screamer_painting file. The file I provided earlier does not work due to if magnitude < 1000 and below not being indented by a tab. This works with CircuitPython 5.0.0-alpha.5 uf2 and Bundle Version 5.x libraries. I have not test with version 4.x. My external speaker connected to my Crickit is not working right now. So I don't know if an external speaker will work. Mine works and sometimes it does not. I have not figured out why.
You should hear the sound from speaker on the CPX board and the servo move when you make a loud sound. You will hear scream_low.wav.

Code: Select all | TOGGLE FULL SIZE
# screaming_painting.py
# Needs scream_low.wav file
import time
import math
import array
import audiobusio
import digitalio
import audioio
import board
from adafruit_crickit import crickit

# Enable the speaker
spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = digitalio.Direction.OUTPUT
spkrenable.value = True

# Number of samples to read at once.
NUM_SAMPLES = 160
# Remove DC bias before computing RMS.
def normalized_rms(values):
    minbuf = int(mean(values))
    samples_sum = sum(
        float(sample - minbuf) * (sample - minbuf)
        for sample in values
    )

    return math.sqrt(samples_sum / len(values))

def mean(values):
    return sum(values) / len(values)
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
                       sample_rate=16000, bit_depth=16)

# Record an initial sample to calibrate. Assume it's quiet when we start.
samples = array.array('H', [0] * NUM_SAMPLES)
mic.record(samples, len(samples))

head_servo = crickit.servo_1
head_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)
head_servo.angle = 90  # center the head.

try:
    from audiocore import WaveFile
except ImportError:
    from audioio import WaveFile

try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!

# Set audio out on speaker.
a = audioio.AudioOut(board.SPEAKER)

# Start playing the file (in the background).
def play_file(wavfile):
    print("Playing scream!")
    with open(wavfile, "rb") as f:
        wav = audioio.WaveFile(f)
        a.play(wav)
        while a.playing:
            head_servo.angle = 60
            time.sleep(.01)
            head_servo.angle = 120
            time.sleep(.01)

while True:
    mic.record(samples, len(samples))
    magnitude = normalized_rms(samples)
    print(((magnitude),))  # formatting is for the Mu plotter.

    if magnitude < 1000:  # it's quiet, do nothing.
        pass
    else:
        print("LOUD")
        head_servo.angle = 60
        time.sleep(.05)
        head_servo.angle = 120
        time.sleep(.05)
        head_servo.angle = 90
        time.sleep(.02)
        play_file("scream_low.wav")
        head_servo.angle = 90
        time.sleep(0.1)
        head_servo._pwm_out.duty_cycle = 0 # to quiet servo
        time.sleep(2)

V2man
 
Posts: 326
Joined: Mon Dec 03, 2018 12:38 am

Re: a problem about The Scream: Interactive Screaming Painti

by V2man on Mon Nov 11, 2019 5:58 pm

I just checked the last screamer_painting.py I posted. Works with version CircuitPython 4.1.0 uf2 and Bundle Version 4.x also and with only CIRCUITPY/lib/adafruit_crickit.mpy.

V2man
 
Posts: 326
Joined: Mon Dec 03, 2018 12:38 am

Re: a problem about The Scream: Interactive Screaming Painti

by V2man on Wed Nov 13, 2019 4:32 pm

Code: Select all | TOGGLE FULL SIZE
# Based on screaming_painting.py
# Needs scream_low.wav file
# For (CPX or CPB) + Crickit with CircuitPython 5.0.0-alpha.5 uf2
# Needs CIRCUITPY/lib/adafruit_crickit.mpy and CIRCUITPY/scream_low.wav
# Loud sound causes servo to move and wavefile to play scream

import time
import board
import math
import array
import audiobusio
import digitalio
from adafruit_crickit import crickit

try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!

try:
    import audiocore
except ImportError:
    import audioio as audiocore
from audiocore import WaveFile

# Enable the speaker
spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = digitalio.Direction.OUTPUT
spkrenable.value = True
audio = AudioOut(board.SPEAKER)
wavefile = "scream_low.wav"
# Number of samples to read at once.
NUM_SAMPLES = 160
# Remove DC bias before computing RMS.
def normalized_rms(values):
    minbuf = int(mean(values))
    samples_sum = sum(
        float(sample - minbuf) * (sample - minbuf)
        for sample in values
    )

    return math.sqrt(samples_sum / len(values))

def mean(values):
    return sum(values) / len(values)
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
                       sample_rate=16000, bit_depth=16)

# Record an initial sample to calibrate. Assume it's quiet when we start.
samples = array.array('H', [0] * NUM_SAMPLES)
mic.record(samples, len(samples))

head_servo = crickit.servo_1
head_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)
head_servo.angle = 90  # center the head.
time.sleep(0.01)
head_servo._pwm_out.duty_cycle = 0  # to quiet servo

# Start playing the file (in the background).
def play_file(filename):
    print(filename)
    with WaveFile(open(filename, "rb")) as wave:
        audio.play(wave)
        while audio.playing:
            head_servo.angle = 60
            time.sleep(.05)
            head_servo.angle = 120
            time.sleep(.05)

while True:
    mic.record(samples, len(samples))
    magnitude = normalized_rms(samples)
    print(((magnitude),))  # formatting is for the Mu plotter.

    if magnitude < 1000:  # it's quiet, do nothing.
        pass
    else:
        print("LOUD")
        head_servo.angle = 60
        time.sleep(.05)
        head_servo.angle = 120
        time.sleep(.05)
        head_servo.angle = 90
        time.sleep(.05)
        play_file(wavefile)
        head_servo._pwm_out.duty_cycle = 0  # to quiet servo
        time.sleep(2)

V2man
 
Posts: 326
Joined: Mon Dec 03, 2018 12:38 am

Please be positive and constructive with your questions and comments.