Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8041

MicroPython • Re: MP3-Player with Pico and DFPlayer Mini

$
0
0
Anyway, write a little piece of code that just does the play/next function, then integrate what you learn into your main code.
Ok, so I wrote a code to control the button-inputs (single-click, double-/triple-click and hold).
I then created 3 modules: One to control wether the player should be paused or resumed ("play_pause"), one to start the next song in line ("sequence") and one called "control" which checks which input was given through the button ("control").

I could probably just put "control" as the main loop, but for testing I wanted to be able to switch between modules for the main loop.

Now, I have one of two problems, depending on which module I put in the main loop.

If I put "control", it just spams "play/pause" and sometimes interrupts the player. I don't really get this, since the "click-times"-counter should start at 0, no?

If I put "sequence" I have the same problem as before (as expected), where the loop just trucks along and won't be interrupted by my button inputs.

What am I missing/doing wrong?

Code:

import timefrom machine import Pinfrom dfplayermini import DFPlayerMiniimport random  # for later use (shuffle)# DFPlayer Mini Pinsplayer1 = DFPlayerMini(1, 4, 5)#Buttonbutton1 = Pin(3, Pin.IN, Pin.PULL_UP)v = 15#Volumeclick_timeout = 400  # How long should the pico wait for further clicks?long_press_time = 700# How long does the button have to be held to cound as being held?# Setting up DFPlayerplayer1.set_volume(v)player1.stop()def play_pause():#controls the pause-button    if player1.get_status() == 1:        player1.pause()    else:        player1.start()def sequence():#plays the songs in sequence    while button1.value()==0:        pass    time.sleep(0.1)    player1.play_next()    # Do nothing while module is playing    # If button is pressed we can play the next song    while player1.get_status() == 1:        control()        def control():#controls what happens after specific button inputs    click_times = []    if button1.value()==0:        press_start = time.ticks_ms()#set a timer for the button        while button1.value() == 0:            time.sleep(0.05)            if time.ticks_diff(time.ticks_ms(), press_start) > long_press_time:                print("random")  # Long Press                while button1.value() == 0:                    pass  # wait until let go to prevent further clicks                time.sleep(0.1)                click_times = []  # reset click counter                break #for now, this interrupts/pauses the sequence for a short period        else:            click_times.append(time.ticks_ms())  # register click            time.sleep(0.05)          # wait for further clicks        start_wait = time.ticks_ms()        while time.ticks_diff(time.ticks_ms(), start_wait) < click_timeout:            if button1.value() == 0:                break        else:            # Timeout => see how often the button was clicked            num_clicks = len(click_times)            if num_clicks == 0:                pass            elif num_clicks == 1:                print("play/pause")                play_pause()            elif num_clicks == 2:                print("next")                sequence()            elif num_clicks >= 3:                print("previous")                player1.play_previous()                while player1.get_status() == 1:                    pass                sequence()            click_times = []#reset click counterwhile True:    sequence()

Statistics: Posted by CptAmmogeddon — Sat Apr 05, 2025 11:38 am



Viewing all articles
Browse latest Browse all 8041

Trending Articles