Added 5 December
This commit is contained in:
parent
1d31318c7b
commit
78b95c0c02
98
5-dezember.py
Normal file
98
5-dezember.py
Normal 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)
|
||||||
|
|
17
README.md
17
README.md
@ -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">
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user