Compare commits
17 Commits
73620c0edd
...
master
Author | SHA1 | Date | |
---|---|---|---|
4de81692e2 | |||
321e99414d | |||
9e54e37cb0 | |||
deb59ee3bd | |||
2f82574cbf | |||
542334c49f | |||
9b2b6d37ac | |||
d33ef55963 | |||
10a4fdd78f | |||
78b95c0c02 | |||
1d31318c7b | |||
7825d5fff2 | |||
fcf71d1d3f | |||
f7a0c34f40 | |||
a013072810 | |||
ee7171de72 | |||
c5d1da1799 |
122
10-dezember-leds.py
Normal file
122
10-dezember-leds.py
Normal file
@ -0,0 +1,122 @@
|
||||
# Imports
|
||||
from machine import Pin, PWM
|
||||
import time, sys
|
||||
|
||||
# Set up LED pins
|
||||
red = Pin(18, Pin.OUT)
|
||||
amber = Pin(19, Pin.OUT)
|
||||
green = Pin(20, Pin.OUT)
|
||||
|
||||
# Set up the Break Beam pin
|
||||
beam = Pin(26, Pin.IN, Pin.PULL_DOWN)
|
||||
|
||||
# Set up the Buzzer pin as PWM
|
||||
buzzer = PWM(Pin(13))
|
||||
|
||||
# Set buzzer PWM frequency to 1000
|
||||
buzzer.freq(1000)
|
||||
|
||||
# Start with the buzzer volume off (duty 0)
|
||||
buzzer.duty_u16(0)
|
||||
|
||||
# Create game variables
|
||||
starttime = 0
|
||||
timecheck = 0
|
||||
scorecounter = 0
|
||||
state = 0
|
||||
targetscore = 100
|
||||
|
||||
print("Game starts after the beep!")
|
||||
|
||||
#Long beep to signal game start
|
||||
buzzer.duty_u16(10000)
|
||||
time.sleep(2)
|
||||
buzzer.duty_u16(0)
|
||||
|
||||
print("GO!")
|
||||
print("-------------------------------")
|
||||
|
||||
# Store our start time (seconds)
|
||||
starttime = time.time()
|
||||
|
||||
while True: # Run this block until code stopped
|
||||
|
||||
time.sleep(0.0001) # Very short delay
|
||||
|
||||
# Take the current time and minus the original start time
|
||||
# This gives us the number of seconds since we started the game
|
||||
timecheck = time.time() - starttime
|
||||
|
||||
if timecheck >= 30: # If 30 or more seconds have passed
|
||||
|
||||
# LEDs off
|
||||
red.value(0)
|
||||
amber.value(0)
|
||||
green.value(0)
|
||||
|
||||
# Beep to signal game end
|
||||
buzzer.duty_u16(10000)
|
||||
time.sleep(0.2)
|
||||
buzzer.duty_u16(0)
|
||||
|
||||
# Print the target and player's score
|
||||
print("-------------------------------")
|
||||
print("GAME OVER! YOU LOSE :(")
|
||||
print("The target was",targetscore,", you scored",scorecounter)
|
||||
print("-------------------------------")
|
||||
|
||||
# Exit the program
|
||||
sys.exit()
|
||||
|
||||
elif scorecounter >= targetscore: # If player's score has hit the target
|
||||
|
||||
# LEDs off
|
||||
red.value(0)
|
||||
amber.value(0)
|
||||
green.value(0)
|
||||
|
||||
# Beep to signal game end
|
||||
buzzer.duty_u16(10000)
|
||||
time.sleep(0.2)
|
||||
buzzer.duty_u16(0)
|
||||
|
||||
# Print time taken to win
|
||||
print("-------------------------------")
|
||||
print("YOU WIN!")
|
||||
print("You took",timecheck,"seconds!")
|
||||
print("-------------------------------")
|
||||
|
||||
# Exit the program
|
||||
sys.exit()
|
||||
|
||||
elif state == 0 and beam.value() == 0: # If state is 0 AND our pin is LOW
|
||||
|
||||
scorecounter = scorecounter + 1 # Add +1 to our score counter
|
||||
|
||||
state = 1 # Change state to 1
|
||||
|
||||
print("SCORE =",scorecounter,"/",targetscore) # Print the score and target
|
||||
print("Time remaining:", (30 - timecheck)) # take our timecheck variable away from 30 - gives the remaining time
|
||||
|
||||
if scorecounter < (targetscore / 100 * 33): # If our score is less than 33% of the target
|
||||
|
||||
red.value(1) # Red LED on
|
||||
amber.value(0)
|
||||
green.value(0)
|
||||
|
||||
elif (targetscore/ 100 * 33) < scorecounter < (targetscore / 100 * 66): # If our score is between 33% and 66% of the target
|
||||
|
||||
red.value(1) # Red LED on
|
||||
amber.value(1) # Amber LED on
|
||||
green.value(0)
|
||||
|
||||
elif scorecounter > (targetscore / 100 * 66): # If our score is over 66% of the target
|
||||
|
||||
red.value(1) # Red LED on
|
||||
amber.value(1) # Amber LED on
|
||||
green.value(1) # Green LED on
|
||||
|
||||
elif state == 1 and beam.value() == 1: # If state is 1 AND our pin is HIGH
|
||||
|
||||
state = 0 # Change the state to 0
|
||||
|
71
10-dezember.py
Normal file
71
10-dezember.py
Normal file
@ -0,0 +1,71 @@
|
||||
# Imports
|
||||
from machine import Pin, PWM
|
||||
import time, sys
|
||||
|
||||
# Set up the Break Beam pin
|
||||
beam = Pin(26, Pin.IN, Pin.PULL_DOWN)
|
||||
|
||||
# Set up the Buzzer pin as PWM
|
||||
buzzer = PWM(Pin(13))
|
||||
|
||||
# Set buzzer PWM frequency to 1000
|
||||
buzzer.freq(1000)
|
||||
|
||||
# Start with the buzzer volume off (duty 0)
|
||||
buzzer.duty_u16(0)
|
||||
|
||||
# Create game variables
|
||||
starttime = 0
|
||||
timecheck = 0
|
||||
scorecounter = 0
|
||||
state = 0
|
||||
|
||||
print("Game starts after the beep!")
|
||||
|
||||
#Long beep to signal game start
|
||||
buzzer.duty_u16(10000)
|
||||
time.sleep(2)
|
||||
buzzer.duty_u16(0)
|
||||
|
||||
print("GO!")
|
||||
|
||||
# Store our start time (seconds)
|
||||
starttime = time.time()
|
||||
|
||||
while True: # Run this block until code stopped
|
||||
|
||||
time.sleep(0.0001) # Very short delay
|
||||
|
||||
# Take the current epoch time and minus the start time
|
||||
# This gives us the number of seconds since we started the game
|
||||
timecheck = time.time() - starttime
|
||||
|
||||
if timecheck >= 30: # If 30 or more seconds have passed
|
||||
|
||||
print("GAME OVER!")
|
||||
|
||||
# Beep to signal game end
|
||||
buzzer.duty_u16(10000)
|
||||
time.sleep(0.2)
|
||||
buzzer.duty_u16(0)
|
||||
|
||||
# Print the player's score
|
||||
print("YOUR SCORE:",scorecounter)
|
||||
|
||||
# Exit the program
|
||||
sys.exit()
|
||||
|
||||
elif state == 0 and beam.value() == 0: # If state is 0 AND our pin is LOW
|
||||
|
||||
scorecounter = scorecounter + 1 # Add +1 to our score counter
|
||||
|
||||
state = 1 # Change state to 1
|
||||
|
||||
print("SCORE =",scorecounter) # Print our new score counter
|
||||
print("Time remaining:", (30 - timecheck)) # take our timecheck variable away from 30 - gives the remaining time
|
||||
|
||||
elif state == 1 and beam.value() == 1: # If state is 1 AND our pin is HIGH
|
||||
|
||||
state = 0 # Change the state to 0
|
||||
|
||||
|
28
11-dezember.py
Normal file
28
11-dezember.py
Normal file
@ -0,0 +1,28 @@
|
||||
# Define the display and size (128x32)
|
||||
display = SSD1306_I2C(128, 32, i2c)
|
||||
|
||||
counter = 0 # Start our counter at zero
|
||||
|
||||
while True: # Loop forever
|
||||
|
||||
display.fill(0) # Clear the display
|
||||
|
||||
print(counter) # Print the current count
|
||||
|
||||
# Show the counter on the display
|
||||
# The display library expects strings only
|
||||
# Counter is a number (integer) so we convert it to text (a string) with 'str'
|
||||
display.text("The Endless",0,0)
|
||||
display.text("Counter!",0,12)
|
||||
display.text((str(counter)),0,24)
|
||||
|
||||
# Update the display
|
||||
display.show()
|
||||
|
||||
# Short delay
|
||||
time.sleep(0.1)
|
||||
|
||||
# Add 1 to our counter
|
||||
counter += 1
|
||||
|
||||
|
33
12-dezember.py
Normal file
33
12-dezember.py
Normal file
@ -0,0 +1,33 @@
|
||||
# Imports
|
||||
import time
|
||||
from machine import Pin
|
||||
from neopixel import NeoPixel
|
||||
|
||||
# Define the strip pin number (28) and number of LEDs (15)
|
||||
strip = NeoPixel(Pin(28), 15)
|
||||
|
||||
# Colour variables
|
||||
red = 255,0,0
|
||||
green = 0,255,0
|
||||
blue= 0,0,255
|
||||
|
||||
# Define colour list
|
||||
colours = [red, green, blue]
|
||||
|
||||
while True: # Run forever
|
||||
|
||||
# Iterate over the colours
|
||||
for j in colours:
|
||||
|
||||
# Then iterate over 15 leds
|
||||
for i in range(15):
|
||||
|
||||
# Set each LED in the range to red
|
||||
strip[i] = (j)
|
||||
|
||||
# Delay - the speed of the chaser
|
||||
time.sleep(0.1)
|
||||
|
||||
# Send the data to the strip
|
||||
strip.write()
|
||||
|
146
13-dezember.py
Normal file
146
13-dezember.py
Normal file
@ -0,0 +1,146 @@
|
||||
import time
|
||||
from machine import Pin
|
||||
from neopixel import NeoPixel
|
||||
|
||||
#Define the strip pin number (28) and number of LEDs (15)
|
||||
strip = NeoPixel(Pin(28), 15)
|
||||
|
||||
sleep = (0.01)
|
||||
|
||||
i = 0
|
||||
green1 = 255
|
||||
green2 = 50
|
||||
green3 = 10
|
||||
green4 = 5
|
||||
green5 = 1
|
||||
|
||||
red1 = 0
|
||||
red2 = 0
|
||||
red3 = 0
|
||||
red4 = 0
|
||||
red5 = 0
|
||||
|
||||
blue1 = 255
|
||||
blue2 = 50
|
||||
blue3 = 10
|
||||
blue4 = 5
|
||||
blue5 = 1
|
||||
|
||||
|
||||
while True: # Run forever
|
||||
|
||||
while i < 14:
|
||||
if i == 0:
|
||||
strip.fill((0,0,0))
|
||||
strip[i] = (red1,green1,blue1)
|
||||
time.sleep(sleep)
|
||||
strip.write()
|
||||
i = i + 1
|
||||
strip.write()
|
||||
|
||||
elif i == 1:
|
||||
strip.fill((0,0,0))
|
||||
strip[i] = (red1,green1,blue1)
|
||||
time.sleep(sleep)
|
||||
strip[i-1] = (red2,green2,blue2)
|
||||
time.sleep(sleep)
|
||||
i = i + 1
|
||||
strip.write()
|
||||
|
||||
elif i == 2:
|
||||
strip.fill((0,0,0))
|
||||
strip[i] = (red1,green1,blue1)
|
||||
time.sleep(sleep)
|
||||
strip[i-1] = (red2,green2,blue2)
|
||||
time.sleep(sleep)
|
||||
strip[i-2] = (red3,green3,blue3)
|
||||
time.sleep(sleep)
|
||||
i = i + 1
|
||||
strip.write()
|
||||
|
||||
elif i == 3:
|
||||
strip.fill((0,0,0))
|
||||
strip[i] = (red1,green1,blue1)
|
||||
time.sleep(sleep)
|
||||
strip[i-1] = (red2,green2,blue2)
|
||||
time.sleep(sleep)
|
||||
strip[i-2] = (red3,green3,blue3)
|
||||
time.sleep(sleep)
|
||||
strip[i-3] = (red4,green4,blue4)
|
||||
time.sleep(sleep)
|
||||
i = i + 1
|
||||
strip.write()
|
||||
|
||||
elif i >= 4:
|
||||
strip.fill((0,0,0))
|
||||
strip[i] = (red1,green1,blue1)
|
||||
time.sleep(sleep)
|
||||
strip[i-1] = (red2,green2,blue2)
|
||||
time.sleep(sleep)
|
||||
strip[i-2] = (red3,green3,blue3)
|
||||
time.sleep(sleep)
|
||||
strip[i-3] = (red4,green4,blue4)
|
||||
time.sleep(sleep)
|
||||
strip[i-4] = (red5,green5,blue5)
|
||||
time.sleep(sleep)
|
||||
i = i + 1
|
||||
strip.write()
|
||||
|
||||
while i > 0:
|
||||
if i == 14:
|
||||
strip.fill((0,0,0))
|
||||
strip[i] = (red1,green1,blue1)
|
||||
time.sleep(sleep)
|
||||
i = i - 1
|
||||
strip.write()
|
||||
|
||||
elif i == 13:
|
||||
strip.fill((0,0,0))
|
||||
strip[i] = (red1,green1,blue1)
|
||||
time.sleep(sleep)
|
||||
strip[i+1] = (red2,green2,blue2)
|
||||
time.sleep(sleep)
|
||||
i = i - 1
|
||||
strip.write()
|
||||
|
||||
elif i == 12:
|
||||
strip.fill((0,0,0))
|
||||
strip[i] = (red1,green1,blue1)
|
||||
time.sleep(sleep)
|
||||
strip[i+1] = (red2,green2,blue2)
|
||||
time.sleep(sleep)
|
||||
strip[i+2] = (red3,green3,blue3)
|
||||
time.sleep(sleep)
|
||||
i = i - 1
|
||||
strip.write()
|
||||
|
||||
elif i == 11:
|
||||
strip.fill((0,0,0))
|
||||
strip[i] = (red1,green1,blue1)
|
||||
time.sleep(sleep)
|
||||
strip[i+1] = (red2,green2,blue2)
|
||||
time.sleep(sleep)
|
||||
strip[i+2] = (red3,green3,blue3)
|
||||
time.sleep(sleep)
|
||||
strip[i+3] = (red4,green4,blue4)
|
||||
time.sleep(sleep)
|
||||
i = i - 1
|
||||
strip.write()
|
||||
|
||||
elif i <= 10:
|
||||
|
||||
strip.fill((0,0,0))
|
||||
strip[i] = (red1,green1,blue1)
|
||||
time.sleep(sleep)
|
||||
strip[i+1] = (red2,green2,blue2)
|
||||
time.sleep(sleep)
|
||||
strip[i+2] = (red3,green3,blue3)
|
||||
time.sleep(sleep)
|
||||
strip[i+3] = (red4,green4,blue4)
|
||||
time.sleep(sleep)
|
||||
strip[i+4] = (red5,green5,blue5)
|
||||
time.sleep(sleep)
|
||||
i = i - 1
|
||||
strip.write()
|
||||
|
||||
|
57
14-dezember.py
Normal file
57
14-dezember.py
Normal file
@ -0,0 +1,57 @@
|
||||
# Imports
|
||||
import time
|
||||
from machine import Pin
|
||||
from neopixel import NeoPixel
|
||||
|
||||
# Define our button pin
|
||||
button = Pin(3, Pin.IN, Pin.PULL_DOWN)
|
||||
|
||||
# Define the strip pin number (28) and number of LEDs (15)
|
||||
strip = NeoPixel(Pin(28), 15)
|
||||
|
||||
# Colour variables
|
||||
red = 255,0,0
|
||||
green = 0,255,0
|
||||
blue = 0,0,255
|
||||
white = 255,255,255
|
||||
purple = 170,0,255
|
||||
yellow = 255,255,0
|
||||
|
||||
# Define colour list
|
||||
colours = [red, green, blue, white, purple, yellow]
|
||||
|
||||
# Create index variable starting at 0
|
||||
myindex = 0
|
||||
|
||||
# Variable with the number of items in our list (3)
|
||||
# We -1 as the index starts at 0, and we want to use this for the colour list index number (0, 1 or 2)
|
||||
# This is useful as it means we don't have to count the colours if we add more
|
||||
indexlength = len(colours) -1
|
||||
|
||||
while True: # Run forever
|
||||
|
||||
time.sleep(0.4) # Delay
|
||||
|
||||
if button() == 1: # If button pressed
|
||||
|
||||
# If the index variable is less than or equal to the lengh of the index
|
||||
if myindex < indexlength:
|
||||
|
||||
# Add +1 to the index variable
|
||||
myindex = myindex + 1
|
||||
|
||||
# If the index variable is over the index length
|
||||
else:
|
||||
|
||||
# Set index variable back to 0 (the first item in our list)
|
||||
myindex = 0
|
||||
|
||||
## Now this code runs AFTER the if statements...
|
||||
|
||||
# Fill the strip with the current list index colour
|
||||
strip.fill((colours[myindex]))
|
||||
|
||||
# Write the data to the LED strip
|
||||
strip.write()
|
||||
|
||||
|
24
3-dezember.py
Normal file
24
3-dezember.py
Normal file
@ -0,0 +1,24 @@
|
||||
from machine import Pin
|
||||
import time
|
||||
|
||||
knopf1 = Pin(13, Pin.IN, Pin.PULL_DOWN)
|
||||
knopf2 = Pin(8, Pin.IN, Pin.PULL_DOWN)
|
||||
knopf3 = Pin(3, Pin.IN, Pin.PULL_DOWN)
|
||||
rot = Pin(20, Pin.OUT)
|
||||
gelb = Pin(19, Pin.OUT)
|
||||
gruen = Pin(18, Pin.OUT)
|
||||
rot.value(0)
|
||||
gelb.value(0)
|
||||
gruen.value(0)
|
||||
|
||||
while True:
|
||||
time.sleep(0.2)
|
||||
|
||||
if knopf1.value() == 1:
|
||||
print("Knopf 1 gedrückt")
|
||||
rot.toggle()
|
||||
if knopf2.value() == 1:
|
||||
print("Knpf 2 gedrückt")
|
||||
gelb.toggle()
|
||||
if knopf3.value() == 1:
|
||||
gruen.toggle()
|
28
4-dezember-led-blinken.py
Normal file
28
4-dezember-led-blinken.py
Normal file
@ -0,0 +1,28 @@
|
||||
from machine import ADC, Pin, PWM
|
||||
import time
|
||||
|
||||
# Anschluss Poti an GP27 und an Plus 3.3 Volt und Minus
|
||||
poti = ADC(Pin(27))
|
||||
|
||||
# LED
|
||||
gruen = PWM(Pin(18))
|
||||
gelb = PWM(Pin(19))
|
||||
rot = PWM(Pin(20))
|
||||
|
||||
# Variable mdelay = 0
|
||||
gruen.freq(1000)
|
||||
gelb.freq(1000)
|
||||
rot.freq(1000)
|
||||
|
||||
reading = 0
|
||||
|
||||
while True:
|
||||
|
||||
reading = poti.read_u16()
|
||||
print(reading)
|
||||
gruen.duty_u16(reading)
|
||||
gelb.duty_u16(reading)
|
||||
rot.duty_u16(reading)
|
||||
time.sleep(0.001)
|
||||
|
||||
|
28
4-dezember-led-regelung.py
Normal file
28
4-dezember-led-regelung.py
Normal file
@ -0,0 +1,28 @@
|
||||
from machine import ADC, Pin, PWM
|
||||
import time
|
||||
|
||||
# Anschluss Poti an GP27 und an Plus 3.3 Volt und Minus
|
||||
poti = ADC(Pin(27))
|
||||
|
||||
# LED
|
||||
gruen = PWM(Pin(18))
|
||||
gelb = PWM(Pin(19))
|
||||
rot = PWM(Pin(20))
|
||||
|
||||
# Variable mdelay = 0
|
||||
gruen.freq(1000)
|
||||
gelb.freq(1000)
|
||||
rot.freq(1000)
|
||||
|
||||
reading = 0
|
||||
|
||||
while True:
|
||||
|
||||
reading = poti.read_u16()
|
||||
print(reading)
|
||||
gruen.duty_u16(reading)
|
||||
gelb.duty_u16(reading)
|
||||
rot.duty_u16(reading)
|
||||
time.sleep(0.001)
|
||||
|
||||
|
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)
|
||||
|
44
6-dezember.py
Normal file
44
6-dezember.py
Normal file
@ -0,0 +1,44 @@
|
||||
# Imports
|
||||
from machine import ADC, Pin
|
||||
import time
|
||||
|
||||
# Set up the LED pins
|
||||
red = Pin(18, Pin.OUT)
|
||||
amber = Pin(19, Pin.OUT)
|
||||
green = Pin(20, Pin.OUT)
|
||||
|
||||
# Define pin for our sensor
|
||||
lightsensor = ADC(Pin(26))
|
||||
|
||||
while True: # Run forever
|
||||
|
||||
# Read sensor value and store it in a variable called 'light'
|
||||
light = lightsensor.read_u16()
|
||||
|
||||
# Use the round function to limit the decimal places to 1
|
||||
lightpercent = round(light/65535*100,1)
|
||||
|
||||
# Print our reading percentage with % symbol
|
||||
print(str(lightpercent) +"%")
|
||||
|
||||
# 1 second delay between readings
|
||||
time.sleep(1)
|
||||
|
||||
if lightpercent <= 30: # If percentage is less than or equal to 30
|
||||
|
||||
red.value(1) # Red LED on
|
||||
amber.value(0)
|
||||
green.value(0)
|
||||
|
||||
elif 30 < lightpercent < 60: # If percentage is between 30 and 60
|
||||
|
||||
red.value(0)
|
||||
amber.value(1) # Amber LED on
|
||||
green.value(0)
|
||||
|
||||
elif lightpercent >= 60: # If percentage is greater than or equal to 60
|
||||
|
||||
red.value(0)
|
||||
amber.value(0)
|
||||
green.value(1) # Green LED on
|
||||
|
63
7-dezember.py
Normal file
63
7-dezember.py
Normal file
@ -0,0 +1,63 @@
|
||||
|
||||
# Imports
|
||||
from machine import Pin, PWM
|
||||
import time
|
||||
|
||||
# Set up the LED pins
|
||||
red = Pin(18, Pin.OUT)
|
||||
amber = Pin(19, Pin.OUT)
|
||||
green = Pin(20, Pin.OUT)
|
||||
|
||||
# Set up the Buzzer pin as PWM
|
||||
buzzer = PWM(Pin(13))
|
||||
|
||||
# Set PWM duty to 0% at program start
|
||||
buzzer.duty_u16(0)
|
||||
|
||||
# Set up PIR pin with pull down
|
||||
pir = Pin(26, Pin.IN, Pin.PULL_DOWN)
|
||||
|
||||
# Warm up/settle PIR sensor
|
||||
print("Warming up...")
|
||||
time.sleep(10) # Delay to allow the sensor to settle
|
||||
print("Sensor ready!")
|
||||
|
||||
def alarm(): # Our alarm function
|
||||
|
||||
# Set PWM duty (volume up)
|
||||
buzzer.duty_u16(500)
|
||||
|
||||
for i in range(5): # Run this 5 times
|
||||
|
||||
buzzer.freq(1000) # Higher pitch
|
||||
|
||||
red.value(1) # Red ON
|
||||
amber.value(1) # Amber ON
|
||||
green.value(1) # Green ON
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
buzzer.freq(500) # Lower pitch
|
||||
|
||||
red.value(0) # Red OFF
|
||||
amber.value(0) # Amber OFF
|
||||
green.value(0) # Green OFF
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
# Set PWM duty (volume off)
|
||||
buzzer.duty_u16(0)
|
||||
|
||||
while True: # Run forever
|
||||
|
||||
time.sleep(0.01) # Delay to stop unnecessary program speed
|
||||
|
||||
if pir.value() == 1: # If PIR detects movement
|
||||
|
||||
print("Bewegung erkannt!")
|
||||
|
||||
alarm() # Call our function
|
||||
|
||||
print("Sensor aktiv") # Let us know that the sensor is active again
|
||||
|
||||
|
68
8-dezember.py
Normal file
68
8-dezember.py
Normal file
@ -0,0 +1,68 @@
|
||||
# Imports
|
||||
import onewire, ds18x20, time
|
||||
from machine import Pin, PWM
|
||||
|
||||
# Set up the LED pins
|
||||
red = Pin(18, Pin.OUT)
|
||||
amber = Pin(19, Pin.OUT)
|
||||
green = Pin(20, Pin.OUT)
|
||||
|
||||
# Set up the Buzzer pin as PWM
|
||||
buzzer = PWM(Pin(13))
|
||||
|
||||
# Start PWM duty to 0% at program start
|
||||
buzzer.duty_u16(0)
|
||||
|
||||
# Set the data pin for the sensor
|
||||
SensorPin = Pin(26, Pin.IN)
|
||||
|
||||
# Tell MicroPython that we're using a DS18B20 sensor, and which pin it's on
|
||||
sensor = ds18x20.DS18X20(onewire.OneWire(SensorPin))
|
||||
|
||||
# Look for DS18B20 sensors (each contains a unique rom code)
|
||||
roms = sensor.scan()
|
||||
|
||||
def alarm(): # Our alarm function
|
||||
|
||||
buzzer.duty_u16(10000) # Buzzer duty (volume) up
|
||||
|
||||
for i in range(5): # Run this 5 times
|
||||
|
||||
buzzer.freq(5000) # Higher pitch
|
||||
|
||||
# LEDs ON
|
||||
red.value(1)
|
||||
amber.value(1)
|
||||
green.value(1)
|
||||
|
||||
time.sleep(0.2) # wait 1 second
|
||||
|
||||
buzzer.freq(1000) # Lower pitch
|
||||
|
||||
# LEDs OFF
|
||||
red.value(0)
|
||||
amber.value(0)
|
||||
green.value(0)
|
||||
|
||||
time.sleep(0.2) # wait 1 second
|
||||
|
||||
buzzer.duty_u16(0) # Buzzer duty (volume) off
|
||||
|
||||
while True: # Run forever
|
||||
|
||||
time.sleep(5) # Wait 5 seconds between readings
|
||||
|
||||
for rom in roms: # For each sensor found (just 1 in our case)
|
||||
|
||||
sensor.convert_temp() # Convert the sensor units to centigrade
|
||||
time.sleep(1) # Always wait 1 second after converting
|
||||
|
||||
reading = sensor.read_temp(rom) # Take a temperature reading
|
||||
|
||||
print(reading) # Print the reading
|
||||
|
||||
if reading < 25: # If reading is less than or equal to 18
|
||||
|
||||
alarm() # Call our alarm function
|
||||
|
||||
|
37
9-dezember.py
Normal file
37
9-dezember.py
Normal file
@ -0,0 +1,37 @@
|
||||
# Imports
|
||||
from machine import Pin
|
||||
import time
|
||||
|
||||
gruen = Pin(18, Pin.OUT)
|
||||
gelb = Pin(19, Pin.OUT)
|
||||
rot = Pin(20, Pin.OUT)
|
||||
|
||||
|
||||
# Set up tilt sensor pin
|
||||
tilt = Pin(26, Pin.IN, Pin.PULL_DOWN)
|
||||
|
||||
# Set up a counter variable at zero
|
||||
tiltcount = 0
|
||||
|
||||
# Create a state variable at zero
|
||||
state = 0
|
||||
|
||||
while True: # Run forever
|
||||
|
||||
time.sleep(1.1) # Short delay
|
||||
rot.value(1)
|
||||
|
||||
|
||||
if state == 0 and tilt.value() == 1: # If state is 0 and our pin is HIGH
|
||||
rot.value(0)
|
||||
tiltcount = tiltcount + 1 # Add +1 to tiltcount
|
||||
|
||||
state = 1 # Change state to 1
|
||||
|
||||
print("tilts =",tiltcount) # Print our new tiltcount
|
||||
|
||||
if state == 1 and tilt.value() == 0: # If state is 1 and our pin is LOW
|
||||
|
||||
state = 0 # Change the state to 0
|
||||
|
||||
|
177
README.md
177
README.md
@ -10,4 +10,181 @@ Another great day of learning how to code with the Raspberry Pi Pico and MicroPy
|
||||
Created a dashing, flashing sequence of LEDs
|
||||
-------------------------------------------------------------------------------------------------------
|
||||
|
||||
Day 3 Complete!
|
||||
|
||||
Great job makers, you ve now got the skills to use physical inputs with MicroPython (which will come in handy for the rest of the calendar) and can code if statements in all sorts of interesting new ways!
|
||||
|
||||
Today you have learnt:
|
||||
|
||||
How to create a circuit with physical inputs
|
||||
How to code physical inputs with MicroPython
|
||||
What if statements are and how to use them
|
||||
How to use elif and else within if statements
|
||||
How to use 'and and 'or' within if statement conditions
|
||||
Combined physical inputs with physical outputs
|
||||
|
||||
-------------------------------------------------------------------------------------------------------
|
||||
|
||||
Day 4 Complete!
|
||||
|
||||
Well done makers! We know today was a little heavier as we introduced slightly more advanced topics such as analogue and PWM, so take the time to go back over today's examples and play with the code if you'd like more practice - make it, break it, fix it, learn!
|
||||
|
||||
Today you have:
|
||||
|
||||
Learnt about analogue and the difference between analogue and digital signals
|
||||
Built a circuit with a potentiometer
|
||||
Leant how to use the ADC built into the Raspberry Pi Pico
|
||||
Controlled LEDs with an analogue signal
|
||||
Learnt about PWM including duty cycle and frequency
|
||||
|
||||
------------------------------------------------------------------------------------------------------
|
||||
|
||||
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
|
||||
|
||||
------------------------------------------------------------------------------------------------------
|
||||
|
||||
Day 6 Complete!
|
||||
|
||||
Good job makers! We've learnt how to use another sensor today, as well as introducing some new ways to manipulate values and strings.
|
||||
|
||||
We're going to use this sensor again before the end of the calendar as it's a great partner for a component hiding in one of the other boxes...
|
||||
|
||||
So, what did we learn today? Today we:
|
||||
|
||||
Learnt how to wire a light sensor circuit
|
||||
Learnt how to manipulate data to make it more useful to us and our programs
|
||||
Learnt how to convert values to strings with the str function
|
||||
Learnt how use the round function to limit decimal places
|
||||
Learnt how to combine multiple strings in prints
|
||||
|
||||
------------------------------------------------------------------------------------------------------
|
||||
|
||||
Day 7 Complete!
|
||||
|
||||
That last activity included a lot of detail so we're going to leave it there for today and not overload your fresh coder brains!
|
||||
|
||||
The PIR sensor can be so much fun to use and they come in all different shapes and sizes too, you'll be making your own home security system in no time.
|
||||
|
||||
So what did we cover on day #7? Today you have:
|
||||
|
||||
Built a circuit with a PIR sensor
|
||||
Learnt how to use a PIR sensor with MicroPython and the Pico
|
||||
Created a mini alarm system!
|
||||
Learnt about the range function
|
||||
Revisited functions
|
||||
|
||||
------------------------------------------------------------------------------------------------------
|
||||
|
||||
Day 8 Complete!
|
||||
|
||||
Another day, another component completed! You now have a temperature sensor in your growing arsenal of parts to make projects with, and we're sure you'll use this one time and time again.
|
||||
|
||||
As you're probably starting to realise, a lot of these sensors and components are coded in a very similar way. Sometimes we need the help of imported libraries, sometimes we need resistors/additional hardware, but the fundamental way we write code for them (with loops, if statements and variables) is pretty consistent.
|
||||
|
||||
So what did we cover on day #8? Today you have:
|
||||
|
||||
Built a circuit with a temperature sensor
|
||||
Learnt how to use a temperature sensor with MicroPython and the Pico
|
||||
Used your first 1-wire component and the 1-wire library
|
||||
Created a temperature monitor and temperature alarm system
|
||||
Learnt more about for loops
|
||||
|
||||
------------------------------------------------------------------------------------------------------
|
||||
|
||||
Day 9 Complete!
|
||||
|
||||
Well done makers, we've cracked another sensor. Today's sensor (some might call it a switch) was a little easier than others, especially as you're now so familiar with MicroPython and the approaches we're taking, but we thought a little breather would be good before we adventure on to the fun yet slightly-more-advanced goodies in the final three boxes!
|
||||
|
||||
Today you have:
|
||||
|
||||
Learnt what a tilt sensor/switch is and why you might use one
|
||||
Learnt how to wire a tilt sensor into your circuit (easy peasy!)
|
||||
How to use variables and if statements in a way that improves the robustness and accuracy of our project
|
||||
Made a basic tilt alarm
|
||||
Re-used lots of things you've already learnt!
|
||||
|
||||
------------------------------------------------------------------------------------------------------
|
||||
|
||||
Day 10 Complete!
|
||||
|
||||
We hope you enjoyed the games in today's break beam activities - why not get the whole family involved to show off your project and see who can score the highest for ultimate bragging rights?
|
||||
|
||||
You now have another sensor in your ever-growing box of bits to make projects with - perhaps you'll combine it with the PIR sensor from day #7 to make an awesome alarm with multiple sensors?
|
||||
|
||||
This is the last time we'll be using the LEDs in the calendar, so go ahead and remove them from your circuit (it's best to keep them in the same bag as the resistors so that you don't mix them up). We also won't be using the buzzer tomorrow, so you can remove that as well.
|
||||
|
||||
So what did we cover on day 10? Today you have:
|
||||
|
||||
Built a circuit with break beam sensors
|
||||
Learnt how to code break beams sensors (with their 'always HIGH' way of working)
|
||||
Created a fun break beam game
|
||||
Learnt additional time module options, including epoch time
|
||||
Learnt how to use the sys module to end a program
|
||||
Created nested if statements
|
||||
Used simple maths to turn values into percentages with MicroPython
|
||||
Continued to re-use all off the things you've learnt over the last 10 days
|
||||
|
||||
------------------------------------------------------------------------------------------------------
|
||||
|
||||
Day 11 Complete!
|
||||
|
||||
Aren't OLED displays just bags of fun? We've only just scratched the surface as well! There are lots of clever ways you can use these kinds of displays with graphics, fonts and other tricks, but we wanted to keep our activities relatively simple for beginners.
|
||||
|
||||
You'll find a ton of resources and other examples on the internet that you can use - we even have our own graphics tutorial which can apply to these displays too (with a few tweaks to the code).
|
||||
|
||||
Let's recap, what did we learn on day #11? Today we:
|
||||
|
||||
Learnt how to wire an OLED to our Raspberry Pi Pico
|
||||
Introduced the I2C communication protocol
|
||||
Leant how to install packages in Thonny
|
||||
Learnt how to code an I2C OLED display, including:
|
||||
How to write text to the display
|
||||
How to write multiple lines of text
|
||||
How to alter the position of text
|
||||
How to display sensor data on OLED displays
|
||||
A few little tricks like markers!
|
||||
Re-used knowledge and components from previous boxes, such as:
|
||||
State variables
|
||||
Converting integers to strings
|
||||
Nested if statements
|
||||
Light sensors
|
||||
...and more!
|
||||
|
||||
------------------------------------------------------------------------------------------------------
|
||||
|
||||
Day 12 Complete!
|
||||
|
||||
This was a little longer than some of our other days as these LED strips are such a fun and versatile part to play with (and there's so much more you can do!).
|
||||
|
||||
We're pretty sure most of you will be turning your strip into a fancy monitor backlight - perhaps some will combine it with the PIR to turn it on when there's motion in front of their PC, or maybe some cleverly hidden break beam sensors will be used as a hand-activated colour changer? Whatever you make, we'd love to hear about your projects in the comments section!
|
||||
|
||||
So what did we cover on day #12? Today you have:
|
||||
|
||||
Learnt what an addressable LED is and why they're sooo fun!
|
||||
Built a circuit with addressable LEDs
|
||||
Learnt about RGB and how to create colours with RGB values (and where to find them)
|
||||
Learnt how to code addressable LEDs with MicroPython
|
||||
Learnt different ways to light the entire strip...some more efficient than others!
|
||||
Created colour variables
|
||||
Leant how to create and use lists in MicroPython, including list indexes
|
||||
Used nested for loops
|
||||
Leant how to use len in MicroPython
|
||||
Faded addressable LEDs using range (both counting up and down)
|
||||
|
||||
------------------------------------------------------------------------------------------------------
|
||||
|
||||
<img src="https://git.unixweb.net/unixweb/pihut-advent-kalender/raw/branch/master/images/pihut-advent-kalender.jpg">
|
||||
|
||||
|
Before Width: | Height: | Size: 485 KiB After Width: | Height: | Size: 485 KiB |
BIN
pdf/Pico-R3-A4-Pinout.pdf
Normal file
BIN
pdf/Pico-R3-A4-Pinout.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user