Skip to content

Commit eb1d517

Browse files
committed
Code cleanup in module 5 and added some new more basic interrupt examples.
1 parent f0a73a4 commit eb1d517

5 files changed

Lines changed: 105 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from machine import Pin
2+
import time
3+
4+
led = Pin(2, Pin.OUT)
5+
button = Pin(13, Pin.IN, Pin.PULL_UP)
6+
7+
counter = 0
8+
last_press_time = time.ticks_ms()
9+
10+
def reverseGPIO():
11+
if led.value():
12+
led.value(0)
13+
else:
14+
led.value(1)
15+
16+
def button_handler(pin):
17+
global counter
18+
global last_press_time
19+
current_time = time.ticks_ms()
20+
delta_time = time.ticks_diff(current_time, last_press_time)
21+
if delta_time > 200:
22+
counter += 1
23+
print("Button pressed", counter, "times")
24+
reverseGPIO()
25+
last_press_time = current_time
26+
else:
27+
print("Skipped bounce")
28+
29+
button.irq(trigger=Pin.IRQ_RISING, handler=button_handler)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from button import Button
2+
from machine import Pin
3+
4+
led = Pin(2, Pin.OUT)
5+
6+
def reverseGPIO():
7+
if led.value():
8+
led.value(0)
9+
else:
10+
led.value(1)
11+
12+
button = Button(13, reverseGPIO)

05_advanced/code/PWM_Music.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import time
33
from MusicPlayer import *
44

5-
bpm = 140
5+
bpm = 120
66

77
ms_pb = int((1000*60)//bpm)
88
note_time = int(ms_pb // 4)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from machine import Pin, PWM
2+
import time
3+
4+
5+
class ultrasonic:
6+
7+
def __init__(self, pinNum):
8+
self.echoPin = Pin(pinNum, Pin.IN, 0)
9+
self.startEcho = 0
10+
self.endEcho = 0
11+
self.distance = 0
12+
self.lastMeasure = 0
13+
self.soundVelocity=340
14+
self.echoPin.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.measure)
15+
16+
def measure(self, pin):
17+
if pin.value():
18+
self.startEcho = time.ticks_us()
19+
else:
20+
self.endEcho = time.ticks_us()
21+
self.distance = self.calculateCM(self.startEcho, self.endEcho)
22+
self.lastMeasure = self.endEcho
23+
print("Distance: ", self.distance, "CM")
24+
25+
def calculateCM(self, start, stop):
26+
ticks=time.ticks_diff(stop,start)
27+
return int(ticks*self.soundVelocity//2//10000)
28+
29+
30+
trigPulse = PWM(Pin(13, Pin.OUT), freq=10, duty=1)
31+
us = ultrasonic(14)
32+
33+
while True:
34+
pass
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+

05_advanced/code/button.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from machine import Pin
2+
import time
3+
4+
class Button:
5+
def __init__(self, pinNum, callback):
6+
self.button = Pin(pinNum, Pin.IN, Pin.PULL_UP)
7+
self.callback = callback
8+
self.last_press_time = time.ticks_ms()
9+
self.button.irq(trigger=Pin.IRQ_RISING, handler=self.button_handler)
10+
11+
def button_handler(self, pin):
12+
current_time = time.ticks_ms()
13+
delta_time = time.ticks_diff(current_time, self.last_press_time)
14+
if delta_time > 200:
15+
self.callback()
16+
self.last_press_time = current_time

0 commit comments

Comments
 (0)