HW-MS03 Microwave sensor need help with syntax while/else #10571
Replies: 13 comments 18 replies
-
|
Beta Was this translation helpful? Give feedback.
-
@marinehm About formatting: When adding code, either enclose the code in lines containing three backticks ``` or indent the code lines by like four spaces. An easy way to get the 3 backtick lines is to fully highlight the pasted code and push Ctrl-E. |
Beta Was this translation helpful? Give feedback.
-
I'm uncertain if this works (not tested). Here the code: from machine import Pin
from machine import Timer
class Motion:
def __init__(self, motion_pin, led_pin, off_delay):
# motion_pin, not motion
self.motion = Pin(motion_pin, mode=Pin.IN)
self.led = Pin(led_pin, mode=Pin.OUT, value=0)
self.off_delay = off_delay
self.timer = None
self.motion.irq(
handler=self._callback,
trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING,
)
def _callback(self, pin):
if self.timer:
self.timer.deinit()
self.timer = None
if pin.value():
self.led.value(1)
else:
# This should start the Timer
# if your port does not support software timers,
# then use a positive ID for initialization
self.timer = Timer(
-1,
mode=Timer.ONE_SHOT,
period=self.off_delay,
callback=self._no_motion,
)
def _no_motion(self, timer):
self.led.value(0)
motion1 = Motion(motion_pin=8, led_pin=6, off_delay=5_000) The |
Beta Was this translation helpful? Give feedback.
-
# MicroPython v1.19.1 on 2022-06-18; Raspberry Pi Pico with RP2040
# Thonny 4.0.2
# HW-MS03 Microwave motion sensor
# Created by Nick Sebring
# 01-24-2023
##################################################################
# IMPORT NECESSARY MODULES
from machine import Pin
from time import sleep
import utime
# ASSIGN PINS
led = Pin(6, Pin.OUT)
hw = Pin(8, Pin.IN)
my_pin = hw.value()
# MAKE THINGS HAPPEN
while True:
if my_pin == 0:
# my_pin ==0: prints "Scanning..." FOREVER!
# my_pin !=0: prints "Motion detected!" FOREVER!
# my_pin == 1: prints "Scanning..." FOREVER!
# my_pin != 1: prints "Motion detected!" FOREVER!
led.off()
print("Scanning...")
sleep(5) #gives me time to figure what's going on
else:
led.on()
print("Motion detected!")
sleep(5) #gives me time to figure what's going on I just noticed; each time I run it, it toggles whether the led is on or off. WHAT THE HECK!? |
Beta Was this translation helpful? Give feedback.
-
I like the ctrl E |
Beta Was this translation helpful? Give feedback.
-
As a general point it is rarely necessary to write while hw == 0:
# do stuff
led.on() # No need for else
print("motion detected") The |
Beta Was this translation helpful? Give feedback.
-
If you want to test the input pin every 5 seconds, you need to test hw.value() within the while loop. You are testing the pin only once at the beginning before the loop, is that as intended? |
Beta Was this translation helpful? Give feedback.
-
Copying my Arduino sketch that works flawlessly. I just want to learn micropython and play with some new Raspberry Pi Picos.
|
Beta Was this translation helpful? Give feedback.
-
This doesn't work either:
|
Beta Was this translation helpful? Give feedback.
-
I don't see the definition of "high". In case of doubt, use print to get what is happening. |
Beta Was this translation helpful? Give feedback.
-
In the REPL: if I type: |
Beta Was this translation helpful? Give feedback.
-
Something strange just happened. I was pulling off the sensor to hook it back up to my ESP8266 and run the Arduino sketch on it. The LED came on without the sensor connected. Playing around with this some more. The jumper wire still connected to the Pico HW Pin. It acts as a capacitance touch sensor. Senses my finger at close to 2 inches from the wire and the LED comes on. No Sensor! This will work for me with turning on the light for my shed. BUT, I wanted the motion detector for my hall and bathroom light. For now, I will use the esp8266/Arduino.IDE until I investigate this phenomenon some more. |
Beta Was this translation helpful? Give feedback.
-
I need some work with Micropython.
|
Beta Was this translation helpful? Give feedback.
-
Having trouble with While Else statement
Beta Was this translation helpful? Give feedback.
All reactions