Added Day #9 and edit Readme

This commit is contained in:
Joachim Hummel 2023-12-09 21:46:10 +00:00
parent deb59ee3bd
commit 9e54e37cb0
2 changed files with 51 additions and 0 deletions

37
9-dezember.py Normal file
View 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

View File

@ -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!
------------------------------------------------------------------------------------------------------
<img src="https://git.unixweb.net/unixweb/pihut-advent-kalender/raw/branch/master/images/pihut-advent-kalender.jpg"> <img src="https://git.unixweb.net/unixweb/pihut-advent-kalender/raw/branch/master/images/pihut-advent-kalender.jpg">