-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathultrasonic.py
More file actions
26 lines (22 loc) · 879 Bytes
/
Copy pathultrasonic.py
File metadata and controls
26 lines (22 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from machine import Pin, PWM
import time
class Ultrasonic:
def __init__(self, echoPinNum, triggerPinNum):
self.echoPin = Pin(echoPinNum, Pin.IN, 0)
self.trigPulse = PWM(Pin(triggerPinNum, Pin.OUT), freq=10, duty=1)
self.startEcho = 0
self.endEcho = 0
self.distance = 0
self.lastMeasure = 0
self.soundVelocity=340
self.echoPin.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.measure)
def measure(self, pin):
if pin.value():
self.startEcho = time.ticks_us()
else:
self.endEcho = time.ticks_us()
self.distance = self.calculateCM(self.startEcho, self.endEcho)
self.lastMeasure = self.endEcho
def calculateCM(self, start, stop):
ticks=time.ticks_diff(stop,start)
return int(ticks*self.soundVelocity//2//10000)