ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 초음파 센서로 움직임 감지하여 led, buzzer 작동하기
    카테고리 없음 2022. 8. 7. 20:18

     

    import threading
    import RPi.GPIO as g
    import time
    import datetime
    
    def buzer_function() :
        buzzer = 18
    
        g.setmode(g.BCM)
        g.setup(buzzer, g.OUT)
    
        print ( "Buzzer => On, ", counter )
    
        pwm = g.PWM(buzzer, 1)
        pwm.start(50.0)
    
        time.sleep(1)
    
    def swave_function():
        global  counter
    
        trig = 2
        echo = 3
    
        g.setmode(g.BCM)                     
        g.setup(trig, g.OUT)                  
        g.setup(echo, g.IN)                    
    
        print ( "Press Ctrl + C to quit" )   
    
        while True:
            now = time
    
            now_compare = datetime.datetime.now()
            today18 = now_compare.replace(hour=8, minute=0, second=0, microsecond=0)
            today22 = now_compare.replace(hour=22, minute=0, second=0, microsecond=0)
    
            if ( now_compare >  today18 and now_compare <  today22 ) :
    
                g.output(trig, False)
                time.sleep(0.4)
    
                g.output(trig, True)         
                time.sleep(0.00001)           
                g.output(trig, False)         
    
                while g.input(echo) == 0:    
                    start = time.time()
    
                while g.input(echo) == 1:  
                    stop = time.time()
    
                time_interval = stop - start      
                distance = time_interval * 17000
                distance = round(distance, 2)
    
                print ( now.strftime('%H:%M:%S'), " Distance => ", distance, "cm ,", counter)
    
                if ( distance < 160 ) :
                    print ( "Buzzer => On, ", counter )
                    counter += 1
    
                    if ( counter == 2 ) :
                        buzer_function()
                        
                        time.sleep(1)
    
    def delay_function():
        global  counter
    
        while True:
            print("Count =>", counter)
            time.sleep(6)
            counter = 0
    
    def pir_function() :
        global  counter
    
        sensor = 14
        
        g.setmode(g.BCM)
        g.setup(sensor,g.IN)
    
        print("Waiting for a sensor to settle.\nPress Ctrl + C to quit\n")
        time.sleep(1)
        
        while(True):
            now = time
    
            now_compare = datetime.datetime.now()
            today18 = now_compare.replace(hour=8, minute=0, second=0, microsecond=0)
            today22 = now_compare.replace(hour=22, minute=0, second=0, microsecond=0)
    
            if ( now_compare >  today18 and now_compare <  today22 ) :
                time.sleep(0.4)
    
                if g.input(sensor):
                    print(now.strftime('%H:%M:%S'),"Motion Detected", counter)
                    counter += 1
    
                    if ( counter == 1 ) :
                        buzer_function()
                        
                        time.sleep(1)
    
                else:
                    print(now.strftime('%H:%M:%S'), "Motion ..........", counter)
    
    
    def main_thread():
    
    	thread=threading.Thread(target=pir_function) 
    	thread.daemon=True 
    	thread.start() 
    
    if __name__ == "__main__":
    
        global  counter
        counter = 0
    
        try :
            main_thread()
            delay_function()
            # pir_function()
        except KeyboardInterrupt:
            print ( "bye~" )
        finally:
            g.cleanup()
Designed by Tistory.