from pir_sensor import pirClass from led_handler import ledClass from machine import Pin ''' Example code for using PIR sensor with esp32 - Interrupts - Loop ''' pirRising = pirClass(4) pirFalling = pirClass(33) led1 = ledClass(2) #Function using interupts def pirIRQ(): pirRising.pir.irq(trigger=Pin.IRQ_RISING, handler=pirInterruptRising) pirFalling.pir.irq(trigger=Pin.IRQ_FALLING, handler=pirInterruptFalling) #Interrupt when pir activated def pirInterruptRising(pin): print("PIR Activated") led1.ledOn() #Interrupt when pir deactivates def pirInterruptFalling(pin): print("PIR Deactivated") led1.ledOff()
from led_handler import ledClass from ky037_sensor import KY037 from machine import Pin import time ''' NOTE - using IRQ for KY037 can lead to unexpected interrupts as it may detect multiple interrupts and then queue them up ''' #button assigned to pin 33 mic = 33 micSensor = KY037(mic) #leds assigned to pin 2 and 5 LED1 = 2 ledGreen = ledClass(LED1) #function for button interupt that checks distance def micIRQ(): micSensor.mic.irq(trigger=Pin.IRQ_RISING, handler=interruptRising) #Interrupt when button pushed def interruptRising(pin): ledGreen.ledChange() time.sleep(1) #function for a loop that constantly checks def micLoop(): while True: