0

CPB CircuitPython Code doesnt run
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CPB CircuitPython Code doesnt run

by dannybalanta on Fri May 07, 2021 10:02 pm

Hi there! I need some help with my circuit python code. First then all, I want to play two sounds when buttons A and B are pressed and also, i want to show lights when playing both sounds. I have uploaded the code separately: it works when I wanna play audios, and it works too when the lights are running, but when mixing both codes, it doesnt work at all (just showing green led while connected to a power source)

Code: Select all | TOGGLE FULL SIZE
import time
import board
import neopixel
import digitalio

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

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!

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

# Make the 2 input buttons
buttonA = digitalio.DigitalInOut(board.BUTTON_A)
buttonA.direction = digitalio.Direction.INPUT
buttonA.pull = digitalio.Pull.DOWN

buttonB = digitalio.DigitalInOut(board.BUTTON_B)
buttonB.direction = digitalio.Direction.INPUT
buttonB.pull = digitalio.Pull.DOWN

# The two files assigned to buttons A & B
audiofiles = ["rimshot.wav", "laugh.wav"]


def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return (0, 0, 0)
    if pos < 85:
        return (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)


def color_chase(color, wait):
    for i in range(10):
        pixels[i] = color
        time.sleep(wait)
        pixels.show()
    time.sleep(0.5)


    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)


def play_file(filename):
    print("Playing file: " + filename)
    wave_file = open(filename, "rb")
    with WaveFile(wave_file) as wave:
        with AudioOut(board.SPEAKER) as audio:
            audio.play(wave)
            while audio.playing:
                pass
    print("Finished")

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)

while True:
    if buttonA.value:
        play_file(audiofiles[0])
    if buttonB.value:
        play_file(audiofiles[1])



I'm using Mu editor and the check code options says everything is fine (green thumbnail), idk what is wrong and why they can't run mixed along

Thanks in advance

dannybalanta
 
Posts: 11
Joined: Thu Dec 31, 2020 7:38 pm

Re: CPB CircuitPython Code doesnt run

by dastels on Fri May 07, 2021 10:10 pm

From looking at the code it would appear to repeatedly play the colorchase demo followed by the flash demo.

The second whilte True loop will never execute since the first while True loop never ends.

Dave

dastels
 
Posts: 6907
Joined: Tue Oct 20, 2015 3:22 pm

Re: CPB CircuitPython Code doesnt run

by dannybalanta on Fri May 07, 2021 10:54 pm

So, how can I join them?¨Python doesnt have this brackets to know where something is starting or ending { }

dannybalanta
 
Posts: 11
Joined: Thu Dec 31, 2020 7:38 pm

Re: CPB CircuitPython Code doesnt run

by dastels on Sat May 08, 2021 10:21 am

You need to have a single main while True loop.

Scope is indicated/determined by indentation.

Have a look at Bill Earl's series on multi-tasking with C++/Arduino code:
https://learn.adafruit.com/multi-tasking-the-arduino-part-1
https://learn.adafruit.com/multi-tasking-the-arduino-part-2
https://learn.adafruit.com/multi-tasking-the-arduino-part-3

It might give you some ideas. The language is C++, not Python but the concepts are identical.

Dave

dastels
 
Posts: 6907
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.


cron