From 78b95c0c02720204e5b2e84687cd204cb1559e15 Mon Sep 17 00:00:00 2001 From: Joachim Hummel Date: Tue, 5 Dec 2023 16:27:53 +0000 Subject: [PATCH] Added 5 December --- 5-dezember.py | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 17 +++++++++ 2 files changed, 115 insertions(+) create mode 100644 5-dezember.py diff --git a/5-dezember.py b/5-dezember.py new file mode 100644 index 0000000..1e96276 --- /dev/null +++ b/5-dezember.py @@ -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) + diff --git a/README.md b/README.md index 7b6e28f..ba7e39b 100644 --- a/README.md +++ b/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 + +------------------------------------------------------------------------------------------------------ +