Added 5 December

This commit is contained in:
Joachim Hummel 2023-12-05 16:27:53 +00:00
parent 1d31318c7b
commit 78b95c0c02
2 changed files with 115 additions and 0 deletions

98
5-dezember.py Normal file
View File

@ -0,0 +1,98 @@
Imports
from machine import Pin, PWM
import time
# Set up the Buzzer pin as PWM
buzzer = PWM(Pin(13)) # Set the buzzer to PWM mode
# Create our library of tone variables for "Jingle Bells"
C = 523
D = 587
E = 659
G = 784
# Create volume variable (Duty cycle)
volume = 30000
# Play the tune
# "Jin..."
buzzer.duty_u16(volume) # Volume up
buzzer.freq(E) # Set frequency to the E note
time.sleep(0.1) # Delay
buzzer.duty_u16(0) # Volume off
time.sleep(0.2) # Delay
# "...gle"
buzzer.duty_u16(volume)
buzzer.freq(E)
time.sleep(0.1)
buzzer.duty_u16(0)
time.sleep(0.2)
# "Bells"
buzzer.duty_u16(volume)
buzzer.freq(E)
time.sleep(0.1)
buzzer.duty_u16(0)
time.sleep(0.5) # longer delay
# "Jin..."
buzzer.duty_u16(volume)
buzzer.freq(E)
time.sleep(0.1)
buzzer.duty_u16(0)
time.sleep(0.2)
# "...gle"
buzzer.duty_u16(volume)
buzzer.freq(E)
time.sleep(0.1)
buzzer.duty_u16(0)
time.sleep(0.2)
# "Bells"
buzzer.duty_u16(volume)
buzzer.freq(E)
time.sleep(0.1)
buzzer.duty_u16(0)
time.sleep(0.5) # longer delay
# "Jin..."
buzzer.duty_u16(volume)
buzzer.freq(E)
time.sleep(0.1)
buzzer.duty_u16(0)
time.sleep(0.2)
# "...gle"
buzzer.duty_u16(volume)
buzzer.freq(G)
time.sleep(0.1)
buzzer.duty_u16(0)
time.sleep(0.2)
# "All"
buzzer.duty_u16(volume)
buzzer.freq(C)
time.sleep(0.1)
buzzer.duty_u16(0)
time.sleep(0.2)
# "The"
buzzer.duty_u16(volume)
buzzer.freq(D)
time.sleep(0.1)
buzzer.duty_u16(0)
time.sleep(0.2)
# "Way"
buzzer.duty_u16(volume)
buzzer.freq(E)
time.sleep(0.1)
buzzer.duty_u16(0)
time.sleep(0.2)
# Duty to 0 to turn the buzzer off
buzzer.duty_u16(0)

View File

@ -39,5 +39,22 @@ Today you have:
------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------
Day 5 Complete!
We've covered lots today fellow makers, pat yourselves on the back!
As we cover more advanced topics such as functions, you may find that you need to refer back to these days to refresh your memory later on - and that's absolutely normal! Even the most seasoned professional programmers have to use search engines regularly (some even admit it!).
So what did we cover on day #5? Today you have:
Built a circuit with a buzzer, your first audio component
Learnt how to use the buzzer with MicroPython and the Pico
Learnt about PWM frequencies and duty cycle with buzzers
Used analogue inputs to control audio volume using PWM
Created a festive jingle with MicroPython
Learnt how to use functions to make your code easier to manage and more efficient
------------------------------------------------------------------------------------------------------
<img src="https://git.unixweb.net/unixweb/pihut-advent-kalender/raw/branch/master/images/pihut-advent-kalender.jpg"> <img src="https://git.unixweb.net/unixweb/pihut-advent-kalender/raw/branch/master/images/pihut-advent-kalender.jpg">