From 9e54e37cb0d53dbc9e15b3913f85c88485ddbffe Mon Sep 17 00:00:00 2001 From: Joachim Hummel Date: Sat, 9 Dec 2023 21:46:10 +0000 Subject: [PATCH] Added Day #9 and edit Readme --- 9-dezember.py | 37 +++++++++++++++++++++++++++++++++++++ README.md | 14 ++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 9-dezember.py diff --git a/9-dezember.py b/9-dezember.py new file mode 100644 index 0000000..738488c --- /dev/null +++ b/9-dezember.py @@ -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 + + diff --git a/README.md b/README.md index 70c396b..c67ea50 100644 --- a/README.md +++ b/README.md @@ -104,5 +104,19 @@ So what did we cover on day #8? Today you have: ------------------------------------------------------------------------------------------------------ +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! + +------------------------------------------------------------------------------------------------------ +