Hello everyone,
I could use some help with the following project, first of all: I am still a beginner. The code below is a combination of my own work and our good friend (?) Chat-GPT. Especially when creating the two modes, Chat helped me a lot.
I am using this DFPlayer-Library btw: https://www.penguintutor.com/projects/pico-mp3player
Actually, the code works quite well at first, up to the point where the DFPlayer should get into a continuous loop of playback.
The DFPlayer plays a song to the end unless it receives other inputs. So far so good. However, I am now at a loss as to how I can get it to play the next song (or a new random song in shuffle mode) automatically (both in shuffle mode and in sequential mode) and still be able to “listen” to the key inputs at the same time.
Ergo: How can I create a continuous loop (per mode) which can be interrupted by the keys without the DFPlayer playing the next song every few ms?
Help is very welcome. Many thanks in advance!
I could use some help with the following project, first of all: I am still a beginner. The code below is a combination of my own work and our good friend (?) Chat-GPT. Especially when creating the two modes, Chat helped me a lot.
I am using this DFPlayer-Library btw: https://www.penguintutor.com/projects/pico-mp3player
Actually, the code works quite well at first, up to the point where the DFPlayer should get into a continuous loop of playback.
The DFPlayer plays a song to the end unless it receives other inputs. So far so good. However, I am now at a loss as to how I can get it to play the next song (or a new random song in shuffle mode) automatically (both in shuffle mode and in sequential mode) and still be able to “listen” to the key inputs at the same time.
Ergo: How can I create a continuous loop (per mode) which can be interrupted by the keys without the DFPlayer playing the next song every few ms?
Help is very welcome. Many thanks in advance!
Code:
import timefrom machine import Pinfrom dfplayermini import DFPlayerMiniimport random # For random song selectionplayer1 = DFPlayerMini(1, 4, 5)button1 = Pin(3, Pin.IN, Pin.PULL_UP)button2 = Pin(0, Pin.IN, Pin.PULL_UP)aux_detect = Pin(8, Pin.IN, Pin.PULL_UP)relay = Pin(9, Pin.OUT)S0 = Pin(6, Pin.OUT)S1 = Pin(7, Pin.OUT)S2 = Pin(10, Pin.OUT)x = 0y = 0relay_timer = 0 # Timer for BT changeoverrelay.value(0)switched = False # For AUX cable detectioncurrent_channel = 0# Mode variable (0 = sequence, 1 = shuffle)mode = 0current_song = 0 # Current song (for sequence mode)# Function for switching the multiplexer channeldef switch_channel(channel): S0.value((channel >> 0) & 1) S1.value((channel >> 1) & 1) S2.value((channel >> 2) & 1) switch_channel(0)read_value = player1.set_volume(15)read_value = player1.stop()# Check the buttonsdef check_buttons(): ““”Checks whether one of the buttons is pressed“”” if button1.value() == 0 or button2.value() == 0: return True return False # Function for random playbackdef play_random(): ““”Plays a random song from the SD card“”” song = random.randint(1, 5) # Adjust the number of songs print(f “Playing random song: {song}”) player1.play(song)# Function for switching between sequence and shuffledef switch_mode(): global mode, current_song if mode == 0: # Switch from sequence to shuffle print(“Switching to shuffle mode”) current_song = 0 # Reset the sequence number play_random() mode = 1 else: # Switch from shuffle to sequence print(“Switching to sequence mode”) mode = 0 # Function for playing the next song in sequence modedef play_next_in_sequence(): ““”Plays the next song in sequence“”” global current_song current_song += 1 print(f “Playing next song in sequence: {current_song}”) player1.play(current_song) # Main loopwhile True: # Check the AUX input if aux_detect.value() == 0 and not switched: # Check only if the cable is plugged in and has not yet been switched print(“AUX cable plugged in”) switch_channel(1) # Change channel y = 1 switched = True # Set the variable to True to prevent the channel from being switched again relay.value(0) time.sleep(0.1)if aux_detect.value() == 1 and switched: # Check whether the cable has been removed switched = False # Allow switching again if the cable is removed time.sleep(0.1) # Check button 2 (AUX, BT, SD control) if button2.value() == 0: while button2.value() == 0: pass time.sleep(0.1) if y == 0: print(“AUX”) switch_channel(1) y = 1 time.sleep(0.2) elif y == 1: print(“BT”) switch_channel(2) relay.value(1) # Switch on relay relay_timer = time.ticks_ms() # Reset timer when switching to BT y = 2 time.sleep(0.2) elif y == 2: print(“SD”) relay_timer = time.ticks_ms() # Reset timer when switching to BT switch_channel(0) time.sleep(1) # Wait 1 second before the song starts (wait for button press) if check_buttons(): # Check whether a button has been pressed print(“Action interrupted by button press”) else: read_value = player1.set_volume(15) player1.play_next() y = 0 time.sleep(0.2) # Monitor the timer to switch off the relay when 5 minutes have passed if y != 2 and relay.value() == 1: # If the status is “SD” and the relay is on if time.ticks_diff(time.ticks_ms(), relay_timer) > 3000: # 3 seconds for test purposes print(“Deactivating relay due to inactivity.”) relay.value(0) # Deactivate relay when 3 seconds have elapsed time.sleep(0.1) # Check button 1 (play/pause and random mode) if button1.value() == 0 and y == 0: time.sleep(0.1) # Debounce button1_time = time.ticks_ms() click_count = 1 # Number of clicks # Check for long press (> 1 second) while button1.value() == 0: if time.ticks_diff(time.ticks_ms(), button1_time) > 1000: switch_mode() # Switch between modes time.sleep(0.1) break # No more multiple clicks possible # If no long press: Check for multiple clicks else: double_click_time = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), double_click_time) < 400: if button1.value() == 0: # Second click recognized click_count += 1 time.sleep(0.1) # Debounce again while button1.value() == 0: pass # Wait until released time.sleep(0.1) # Short debouncing after release if click_count == 2: double_click_time = time.ticks_ms() # Wait for third click continue if click_count == 3: print(“prev”) # triple click → “prev” time.sleep(0.1) break else: # If there is no third click within the waiting time if click_count == 2: print(“next”) # double-click → “next” if mode == 0: play_next_in_sequence() elif mode == 1: play_random() # Play random song in shuffle mode elif click_count == 1: if x == 0: print(„play“) player1.start() else: print(„pause“) player1.pause() x = 1 if x == 0 else 0 time.sleep(0.2) # Last Debouncing Statistics: Posted by CptAmmogeddon — Wed Apr 02, 2025 3:20 pm