Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

Simultaneously play sound and blink lights
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Simultaneously play sound and blink lights

by navayii on Thu Apr 16, 2020 7:15 pm

Hi, I'm new to playing with electronics and I'm currently working on a project with circuit playground express using CircuitPython.
I'd like to have both sound and lights go off at the same time. Is there a way to achieve that using CPython?
I have both functions working sequentially but I'd love to run them in parallel. Thanks!

navayii
 
Posts: 8
Joined: Fri Jul 05, 2019 10:33 am

Re: Simultaneously play sound and blink lights

by kevinjwalters on Thu Apr 30, 2020 4:41 pm

Are you using clue.play_file() to play the sound sample? For your case, it doesn't really work as it returns when the sound sample is complete. The source code shows that what you're after is certainly possible, note the while loop which waits until it is finished. If you use the underlying libraries directly then you can do the same thing as that library in your application but put your light flashing code where the while loop is.

kevinjwalters
 
Posts: 627
Joined: Sun Oct 01, 2017 3:15 pm

Re: Simultaneously play sound and blink lights

by navayii on Wed May 06, 2020 10:48 pm

Thank you for your reply. I found the .wav file as a workaround but I was wondering if I could do the same for manually made sine waves. Seems like I cannot use the while loop and have lights go off as long as the sine wave is playing. I was able to do that for the .wav files.

navayii
 
Posts: 8
Joined: Fri Jul 05, 2019 10:33 am

Re: Simultaneously play sound and blink lights

by kevinjwalters on Thu May 07, 2020 9:30 am

For tones, there's already methods in the cp object to help you with this, start_tone() and stop_tone(). These are described in CircuitPython Made Easy on Circuit Playground Express and Bluefruit: Start and Stop Tone.

Here's an example I just ran up on a CPX on REPL:

Code: Select all | TOGGLE FULL SIZE
import time
from adafruit_circuitplayground import cp

c5=523.2511
arp=[c5 * mult for mult in (1,1.25,1.5)]
colours=(0x600000, 0x004000, 0x000060)
black=0x000000

for len in [1.0] * 2 + [0.03] * 10:
    for freq in arp:
        cp.start_tone(freq)
        for colour in colours:
            cp.pixels.fill(colour)
            time.sleep(len)
        cp.pixels.fill(black)
        cp.stop_tone()


That starts slowly so you can see what's going on.

kevinjwalters
 
Posts: 627
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.