I am currently playing with my HC-SR04 ultrasonic sensor to be able to detect wether my entry door is closed or not.
Now, I have done:
Code: Select all
import time
import machine
from machine import Pin, RTC
RTC_USEC_POS = 7
RTC_SEC_POS = 6
RTC_MIN_POS = 5
MIN_TO_SEC = 60
SEC_TO_USEC = 1000
TRIGGER_TIME = 10
TRIGGER_PIN = 'GP11'
ECHO_PIN = 'GP12'
LOW = 0
HIGH = 1
delay = 0
rtc = machine.RTC()
raising_time = rtc.now()
falling_time = rtc.now()
def changingEdge(pin):
print("Changing Edge %s"%pin)
flags = pin.irq().flags()
if flags & Pin.IRQ_RISING:
raising_time = rtc.now()
print("rtc raising: %s" % raising_time)
elif flags & Pin.IRQ_FALLING:
falling_time = rtc.now()
print("rtc falling: %s" % falling_time)
ellapsed_minutes = falling_time[RTC_MIN_POS] - raising_time[RTC_MIN_POS]
ellapsed_seconds = falling_time[RTC_SEC_POS] - raising_time[RTC_SEC_POS]
ellapsed_useconds = falling_time[RTC_USEC_POS] - raising_time[RTC_USEC_POS]
delay = ellapsed_minutes * MIN_TO_SEC * SEC_TO_USEC + ellapsed_seconds * SEC_TO_USEC + ellapsed_useconds
print("Delay: " % delay)
triggerPin = Pin(TRIGGER_PIN, mode = Pin.OUT)
echoPin = Pin(ECHO_PIN, mode = Pin.IN)
echoPin.irq(trigger = Pin.IRQ_RISING | Pin.IRQ_FALLING, handler = changingEdge)
echoPin.irq()()
while delay==0:
input("Press Enter to trigger...")
triggerPin.value(HIGH)
time.sleep_us(TRIGGER_TIME)
triggerPin.value(LOW)
MicroPython v1.6-89-g440d33a on 2016-02-27; WiPy with CC3200
When I launch it, I have an error:
execfile('HC-SR04.py')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "HC-SR04.py", line 48, in <module>
TypeError: can't convert to int
So, I was wondering if I was doing something wrong in my code to have this error thown.
Thank you for the help