Created
December 22, 2024 20:19
-
-
Save statico/f834abdb780b1f3645e64d4d4cc45990 to your computer and use it in GitHub Desktop.
Revisions
-
statico created this gist
Dec 22, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,133 @@ from machine import Pin, PWM import time import random # Button GPIO pins button_pins = [5, 18, 19, 21, 22] # RGB LED GPIO pins red_pin = 32 green_pin = 33 blue_pin = 25 # Buzzer GPIO pin buzzer_pin = 13 # Frequencies for C major scale notes notes = {0: 262, 1: 294, 2: 330, 3: 349, 4: 392} # C4 # D4 # E4 # F4 # G4 # RGB LED colors for each button - yellow, red, blue, green, white colors = { 0: (255, 255, 0), # Yellow 1: (255, 0, 0), # Red 2: (0, 0, 255), # Blue 3: (0, 255, 0), # Green 4: (255, 255, 255), # White } # Initialize buttons buttons = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in button_pins] # Initialize RGB LED red = PWM(Pin(red_pin), freq=1000, duty=0) green = PWM(Pin(green_pin), freq=1000, duty=0) blue = PWM(Pin(blue_pin), freq=1000, duty=0) # Initialize buzzer buzzer = PWM(Pin(buzzer_pin)) buzzer.duty(0) # Turn off buzzer initially def set_rgb_color(r, g, b): red.duty(int(r / 255 * 1023)) green.duty(int(g / 255 * 1023)) blue.duty(int(b / 255 * 1023)) def play_button(button_index, duration=0.5): """Play a single button's light and sound""" buzzer.freq(notes[button_index]) buzzer.duty(16) r, g, b = colors[button_index] set_rgb_color(r, g, b) time.sleep(duration) buzzer.duty(0) set_rgb_color(0, 0, 0) time.sleep(0.2) # Brief pause between buttons def play_sequence(sequence): """Play back the entire sequence""" for button_index in sequence: play_button(button_index) def get_player_input(sequence_length): """Get player input and return the sequence they pressed""" player_sequence = [] while len(player_sequence) < sequence_length: for i, button in enumerate(buttons): if not button.value(): # Button is pressed play_button(i, 0.3) # Shorter duration for player input player_sequence.append(i) while not button.value(): # Wait for button release time.sleep(0.01) time.sleep(0.2) # Debounce break return player_sequence def play_game(): sequence = [] while True: # Add new step to sequence sequence.append(random.randint(0, 4)) print(f"Sequence length: {len(sequence)}") # Play sequence time.sleep(1) play_sequence(sequence) # Get player input player_sequence = get_player_input(len(sequence)) # Check if player's sequence matches if player_sequence != sequence: # Game over for _ in range(3): # Play error sound/light buzzer.freq(100) buzzer.duty(16) set_rgb_color(255, 0, 0) time.sleep(0.2) buzzer.duty(0) set_rgb_color(0, 0, 0) time.sleep(0.2) print(f"Game Over! Score: {len(sequence) - 1}") return len(sequence) - 1 time.sleep(0.5) # Brief pause before next round try: while True: print("Press any button to start new game") # Wait for any button press while all(button.value() for button in buttons): time.sleep(0.1) score = play_game() # Wait for buttons to be released while not all(button.value() for button in buttons): time.sleep(0.1) except KeyboardInterrupt: # Cleanup on exit buzzer.duty(0) set_rgb_color(0, 0, 0) red.deinit() green.deinit() blue.deinit() print("Exiting...")