Joy-it RB-P-XPLR-SET handleiding

37 pagina's
PDF beschikbaar

Handleiding

Je bekijkt pagina 26 van 37
26
from machine import Pin, PWM
import utime
# Note frequencies (in Hz)
notes = {
'C4': 262,
'D4': 294,
'E4': 330,
'F4': 349,
'G4': 392,
'A4': 440,
'B4': 494,
'C5': 523
}
# Melody and duration (in ms)
melody = ['C4', 'D4', 'E4', 'C4', 'C4', 'D4', 'E4', 'C4', 'E4', 'F4', 'G4', 'E4',
'F4', 'G4']
durations = [500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 1000, 500, 500,
1000]
# Initialization of the buzzer
buzzer = PWM(Pin(21))
buzzer.freq(440) # Set a start frequency
# Function to play a note
def play_note(note, duration):
if note in notes:
buzzer.freq(notes[note]) # Set the frequency based on the note
buzzer.duty_u16(32767) # Start the PWM signal
utime.sleep_ms(duration) # Hold the note for the duration
buzzer.duty_u16(0) # Stop the PWM signal (switch o note)
utime.sleep_ms(50) # Short pause between the notes
# Play the melody
for note, duration in zip(melody, durations):
play_note(note, duration)
buzzer.deinit() # Deactivate the PWM channel when nished
SUMMARY: We connect an external buzzer to our Raspberry Pi Pico to play a simple, fun tune.
List of notes
Melody memory
Function for playing the notes

Bekijk gratis de handleiding van Joy-it RB-P-XPLR-SET, stel vragen en lees de antwoorden op veelvoorkomende problemen, of gebruik onze assistent om sneller informatie in de handleiding te vinden of uitleg te krijgen over specifieke functies.

Productinformatie

MerkJoy-it
ModelRB-P-XPLR-SET
CategorieNiet gecategoriseerd
TaalNederlands
Grootte21690 MB