Skip to content

Commit 5f2bfc3

Browse files
Add initial source only examples (requires clean up, just copied source blocks from .ipynb's)
1 parent 90cb4af commit 5f2bfc3

16 files changed

+1339
-0
lines changed

source_only_examples/project1_a.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from machine import Pin
2+
3+
led_pin = Pin(34, Pin.OUT)
4+
led_pin.high()
5+
led_pin.low()
6+
7+
from time import sleep
8+
led_pin.high()
9+
sleep(1)
10+
led_pin.low()
11+
sleep(1)
12+
led_pin.high()
13+
sleep(1)
14+
led_pin.low()
15+
16+
for i in range(10):
17+
led_pin.high()
18+
sleep(1)
19+
led_pin.low()
20+
sleep(1)

source_only_examples/project1_b.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
2+
3+
from machine import ADC # Allows us to use "ADC" (analog-to-digital conversion) to read from our analog pin
4+
5+
led_pin = Pin(34, Pin.OUT) # Create a pin variable for the led pin (pin 34)
6+
potentiometer = ADC(Pin.board.A0) # Create an ADC variable for reading the potentiometer value from analog pin A0
7+
8+
# Try moving the potentiometer and re-running this cell and you should see this value change.
9+
print(potentiometer.read_u16()) # Use the "read_u16" method to read the value of our potentiometer.
10+
11+
# Now, let's blink the LED with different speeds based on the potentiometer input
12+
import time # Allows us to use "time.sleep()" to delay for a certain number of seconds
13+
14+
# Infinite loop so this cell keeps running until we stop it.
15+
while True:
16+
potPosition = potentiometer.read_u16() # Get the new potentiometer position (0 - 65535)
17+
18+
# Lets choose a delay that is proportional to the potentiometer reading
19+
# Since the range of the potentiometer is 0-65535 and we want delays between 0-2 seconds,
20+
# we will divide by (65535 / 2 ) = 32767.5
21+
delay = (potPosition / 32767.5)
22+
23+
# We will comment out the print for now since it can spam our console when the delay is fairly low
24+
# print(f"Potentiometer Value: {potPosition : 5}", end='\r') # Print our potentiometer reading (don't mind the fanciness of this line it just makes the print format nicely)
25+
26+
# Turn on the LED
27+
led_pin.high()
28+
29+
# Delay based on potentiometer position
30+
time.sleep(delay)
31+
32+
# Turn off the LED
33+
led_pin.low()
34+
35+
# Delay based on potentiometer position
36+
time.sleep(delay)

source_only_examples/project1_c.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
2+
3+
from machine import ADC # Allows us to use "ADC" (analog-to-digital conversion) to read from our analog pin
4+
5+
led_pin = Pin(34, Pin.OUT) # Create a pin variable for the led pin (pin 34)
6+
photoresistor = ADC(Pin.board.A0) # Create an ADC variable for reading the photoresistor value from analog pin A0
7+
8+
# Try moving the potentiometer and re-running this cell and you should see this value change.
9+
print(photoresistor.read_u16()) # Use the "read_u16" method to read the value of our potentiometer.
10+
11+
# Now, let's turn the LED on and off based on the photoresistor value.
12+
import time # Allows us to use "time.sleep()" to delay for a certain number of seconds
13+
14+
# We'll set our threshold to half of the maximum value of the ADC reading (65535)
15+
threshold = 65535 / 2
16+
17+
# Infinite loop so this cell keeps running until we stop it.
18+
while True:
19+
photoValue = photoresistor.read_u16() # Get the new photoresistor value (0 - 65535)
20+
21+
print(f"Photoresistor Value: {photoValue : 5}", end='\r') # Print our Photoresistor reading (don't mind the fanciness of this line it just makes the print format nicely)
22+
23+
# Turn on the LED but only if the photoresistor value is above the threshold
24+
if photoValue > threshold:
25+
led_pin.high()
26+
else:
27+
led_pin.low()
28+
29+
# A short delay to make the printout easier to read
30+
time.sleep(0.250)
31+

source_only_examples/project1_d.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
2+
3+
from machine import ADC # Allows us to use "ADC" (analog-to-digital conversion) to read from our analog pin
4+
5+
from machine import PWM # Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED
6+
7+
import time # Import the time module to use sleep for delays
8+
9+
pwmBlue = PWM(Pin(32), freq=1000, duty_u16=0) # Create a PWM object on pin 28 with a frequency of 1000Hz and an initial "on time" of 0 (off)
10+
pwmGreen = PWM(Pin(30), freq=1000, duty_u16=0) # Create a PWM object on pin 27 with a frequency of 1000Hz and an initial "on time" of 0 (off)
11+
pwmRed = PWM(Pin(28), freq=1000, duty_u16=0) # Create a PWM object on pin 26 with a frequency of 1000Hz and an initial "on time" of 0 (off)
12+
13+
# Let's create functions for various colors that we can call later
14+
15+
# Since our PWM "on time" or duty cycle is 16 bits, it is a value between 0 and 65535.
16+
# It's useful to store a variable for maximum brightness so we can use percentages of it easily.
17+
kMaximumBrightness = 65535 # Maximum brightness value for PWM
18+
19+
# These are "functions" that we can "call" to set the color of the LED.
20+
# Notice the "def" keyword, which is used to define a function in Python.
21+
# Now, we can call these later by just typing their names like `red()`, `green()`, etc.
22+
# And all the code inside the function will run.
23+
24+
def red():
25+
# The "duty_u16" method sets the duty cycle or "on time" for the PWM pin.
26+
pwmRed.duty_u16(kMaximumBrightness) # Set the red LED to full brightness
27+
pwmGreen.duty_u16(0) # Turn off the green LED
28+
pwmBlue.duty_u16(0) # Turn off the blue LED
29+
30+
def orange():
31+
pwmRed.duty_u16(kMaximumBrightness) # Set the red LED to full brightness
32+
pwmGreen.duty_u16(int(kMaximumBrightness * 0.25)) # Set the green LED to quarter brightness (by multiplying by 0.5)
33+
pwmBlue.duty_u16(0) # Turn off the blue LED
34+
35+
def yellow():
36+
pwmRed.duty_u16(kMaximumBrightness) # Set the red LED to full brightness
37+
pwmGreen.duty_u16(kMaximumBrightness) # Set the green LED to full brightness
38+
pwmBlue.duty_u16(0) # Turn off the blue LED
39+
40+
def green():
41+
pwmRed.duty_u16(0) # Turn off the red LED
42+
pwmGreen.duty_u16(kMaximumBrightness) # Set the green LED to full brightness
43+
pwmBlue.duty_u16(0) # Turn off the blue LED
44+
45+
def cyan():
46+
pwmRed.duty_u16(0) # Turn off the red LED
47+
pwmGreen.duty_u16(kMaximumBrightness) # Set the green LED to full brightness
48+
pwmBlue.duty_u16(kMaximumBrightness) # Set the blue LED to full brightness
49+
50+
def blue():
51+
pwmRed.duty_u16(0) # Turn off the red LED
52+
pwmGreen.duty_u16(0) # Turn off the green LED
53+
pwmBlue.duty_u16(kMaximumBrightness) # Set the blue LED to full brightness
54+
55+
def magenta():
56+
pwmRed.duty_u16(kMaximumBrightness) # Set the red LED to full brightness
57+
pwmGreen.duty_u16(0) # Turn off the green LED
58+
pwmBlue.duty_u16(kMaximumBrightness) # Set the blue LED to full brightness
59+
60+
def turnOff():
61+
pwmRed.duty_u16(0) # Turn off the red LED
62+
pwmGreen.duty_u16(0) # Turn off the green LED
63+
pwmBlue.duty_u16(0) # Turn off the blue LED
64+
65+
66+
# Remember, we've already defined our RGB pins and functions above.
67+
# Make sure you have run the cells above this one first!!!
68+
photoresistor = ADC(Pin.board.A0) # Create an ADC variable for reading the photoresistor value from analog pin A0
69+
potentiometer = ADC(Pin.board.A1) # Create an ADC variable for reading the potentiometer value from analog pin A1
70+
71+
# We'll set our photo-resistor threshold to a quarter of the maximum value of the ADC reading (65535)
72+
threshold = 65535 / 4
73+
potentiometerMax = 65535 # Maximum value for the potentiometer reading
74+
75+
# Infinite loop to continously read the photoresistor and potentiometer values
76+
while True:
77+
photoValue = photoresistor.read_u16() # Read the photoresistor value (0 to 65535)
78+
potPosition = potentiometer.read_u16() # Read the potentiometer value (0 to 65535)
79+
# Print the values to the console
80+
print(f"Photoresistor Value: {photoValue: 5}, Potentiometer Value: {potPosition : 5}", end='\r') # Print our readings (don't mind the fanciness of this line it just makes the print format nicely)
81+
82+
# Check if the photoresistor value is below the threshold
83+
if photoValue < threshold:
84+
# If the photoresistor value is below the threshold, set the LED color based on the potentiometer position
85+
# Let's split the range 0 - 65535 into 7 equal(ish) parts for different colors
86+
if potPosition > 0 and potPosition < 9362:
87+
red()
88+
if potPosition >= 9362 and potPosition < 18725:
89+
orange()
90+
if potPosition >= 18725 and potPosition < 28087:
91+
yellow()
92+
if potPosition >= 28087 and potPosition < 37450:
93+
green()
94+
if potPosition >= 37450 and potPosition < 46812:
95+
cyan()
96+
if potPosition >= 46812 and potPosition < 56175:
97+
blue()
98+
if potPosition >= 56175 and potPosition <= 65535:
99+
magenta()
100+
101+
else: # Note that this "else" aligns with the photovalue < Threshold condition above.
102+
turnOff() # Turn off the LED if the photoresistor value is above the threshold
103+
104+
time.sleep(0.250) # Sleep for 0.25 seconds to avoid flooding the console with prints

source_only_examples/project2_a.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
2+
from machine import PWM # Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED
3+
4+
pwmSpeaker = PWM(Pin(34), freq=0, duty_u16=0) # Create a PWM object on pin 34 with a frequency of 0Hz and an initial "on time" of 0 (off)
5+
6+
from time import sleep # Import the sleep function to pause execution for a specified duration
7+
beatLength = 0.25 # Define the length of a beat in seconds (0.5 seconds). Change this to adjust the tempo of the music.
8+
restLength = 0.05 # Define the length of a rest in seconds (0.25 seconds). Change this to adjust the length of pauses in the music.
9+
10+
def play(note, beats):
11+
# To define a list we put it in square brackets []
12+
# These list is used to look up the frequency of each note
13+
notes = ['c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', ' ']
14+
# This list contains the frequencies corresponding to the notes in the "notes" list
15+
# For example the 4th item in the "notes" list is 'f' and the 4th item in the "frequencies" list is 175Hz, its frequency
16+
frequencies = [131, 147, 165, 175, 196, 220, 247, 262, 294, 330, 349, 392, 440, 494, 0]
17+
18+
# We will now search our lists for a match
19+
frequency = 0
20+
21+
# This loop will step through each note in the "notes" list and look for a match with the "note" variable
22+
# Notice the len(notes) function returns the number of items in the "notes" list
23+
# The range function creates a sequence of numbers from 0 to len(notes) - 1
24+
# The "for" loop will repeat for each number in that sequence
25+
for i in range(len(notes)):
26+
if notes[i] == note: # If the note matches the current note in the list
27+
frequency = frequencies[i] # Get the corresponding frequency
28+
29+
pwmSpeaker.freq(frequency) # Set the frequency of the PWM signal to the specified frequency
30+
if frequency == 0: # If the frequency is 0, it means we are playing a rest (no sound)
31+
pwmSpeaker.duty_u16(0) # Set the duty cycle to 0 (off)
32+
else: # If the frequency is not 0, we are playing a note
33+
pwmSpeaker.duty_u16(32768) # Set the duty cycle to 50% on time (half as loud as possible)
34+
sleep(beats * beatLength) # Wait for the specified number of beats (0.5 seconds per beat)
35+
pwmSpeaker.duty_u16(0) # Turn off the speaker by setting duty cycle to 0
36+
sleep(beats * restLength) # Wait for the specified number of rests (0.25 seconds per rest)
37+
38+
# Happy Birthday melody using the play() function
39+
play('g', 2) # ha
40+
play('g', 1) # ppy
41+
play('a', 4) # birth
42+
play('g', 4) # day
43+
play('C', 4) # to
44+
play('b', 4) # you
45+
46+
play(' ', 2) # pause for 2 beats
47+
48+
play('g', 2) # ha
49+
play('g', 1) # ppy
50+
play('a', 4) # birth
51+
play('g', 4) # day
52+
play('D', 4) # to
53+
play('C', 4) # you
54+
55+
play(' ', 2) # pause for 2 beats
56+
57+
play('g', 2) # ha
58+
play('g', 1) # ppy
59+
play('G', 4) # birth
60+
play('E', 4) # day
61+
play('C', 4) # dear
62+
play('b', 4) # your
63+
play('a', 6) # name
64+
65+
play(' ', 2) # pause for 2 beats
66+
67+
play('F', 2) # ha
68+
play('F', 1) # ppy
69+
play('E', 4) # birth
70+
play('C', 4) # day
71+
play('D', 4) # to
72+
play('C', 6) # you
73+

source_only_examples/project2_b.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
2+
from machine import PWM # Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED
3+
4+
firstKeyPin = Pin(33, Pin.IN, Pin.PULL_UP) # Create a Pin object for the first key on pin 33, set as input with pull-up resistor
5+
secondKeyPin = Pin(32, Pin.IN, Pin.PULL_UP) # Create a Pin object for the second key on pin 32, set as input with pull-up resistor
6+
thirdKeyPin = Pin(31, Pin.IN, Pin.PULL_UP) # Create a Pin object for the third key on pin 35, set as input with pull-up resistor
7+
8+
pwmSpeaker = PWM(Pin(34), freq=0, duty_u16=0) # Create a PWM object on pin 34 with a frequency of 0Hz and an initial "on time" of 0 (off)
9+
10+
# In an infinite loop, we will check the state of the keys and play a note when a key is pressed
11+
while True:
12+
if firstKeyPin.value() == 0: # If the first key is pressed (value is low)
13+
pwmSpeaker.freq(262) # Play the note C (262Hz)
14+
pwmSpeaker.duty_u16(32768) # Set the duty cycle to 50% (half as loud as possible)
15+
elif secondKeyPin.value() == 0: # If the second key is pressed (value is low)
16+
pwmSpeaker.freq(330) # Play the note E (330Hz)
17+
pwmSpeaker.duty_u16(32768) # Set the duty cycle to 50% (half as loud as possible)
18+
elif thirdKeyPin.value() == 0: # If the third key is pressed (value is low)
19+
pwmSpeaker.freq(392) # Play the note G (392Hz)
20+
pwmSpeaker.duty_u16(32768) # Set the duty cycle to 50% (half as loud as possible)
21+
else: # If no key is pressed
22+
pwmSpeaker.freq(0)
23+
pwmSpeaker.duty_u16(0) # turn off the speaker
24+
25+
# Frequency Table: Feel free to change the frequencies for each button press to play different notes.
26+
# note frequency
27+
# c 262 Hz
28+
# d 294 Hz
29+
# e 330 Hz
30+
# f 349 Hz
31+
# g 392 Hz
32+
# a 440 Hz
33+
# b 494 Hz
34+
# C 523 Hz

0 commit comments

Comments
 (0)