From 321e99414d908f4b1c91c5b29acc553007987060 Mon Sep 17 00:00:00 2001 From: Joachim Hummel Date: Tue, 12 Dec 2023 23:13:29 +0000 Subject: [PATCH] Added new december files and edit Readme --- 10-dezember-leds.py | 122 ++++++++++++++++++++++++++++++++++++++++++++ 10-dezember.py | 71 ++++++++++++++++++++++++++ 11-dezember.py | 28 ++++++++++ 12-dezember.py | 33 ++++++++++++ README.md | 68 ++++++++++++++++++++++++ 5 files changed, 322 insertions(+) create mode 100644 10-dezember-leds.py create mode 100644 10-dezember.py create mode 100644 11-dezember.py create mode 100644 12-dezember.py diff --git a/10-dezember-leds.py b/10-dezember-leds.py new file mode 100644 index 0000000..5b50cd0 --- /dev/null +++ b/10-dezember-leds.py @@ -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 + diff --git a/10-dezember.py b/10-dezember.py new file mode 100644 index 0000000..4c0c4e7 --- /dev/null +++ b/10-dezember.py @@ -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 + + diff --git a/11-dezember.py b/11-dezember.py new file mode 100644 index 0000000..034d62b --- /dev/null +++ b/11-dezember.py @@ -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 + + diff --git a/12-dezember.py b/12-dezember.py new file mode 100644 index 0000000..c8bfa43 --- /dev/null +++ b/12-dezember.py @@ -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() + diff --git a/README.md b/README.md index c67ea50..f067a2a 100644 --- a/README.md +++ b/README.md @@ -118,5 +118,73 @@ Today you have: ------------------------------------------------------------------------------------------------------ +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) + +------------------------------------------------------------------------------------------------------ +