Canada
Asked — Edited
Resolved Resolved by Rich!

Trying To Get A Pir Working

I feel like a blind man wandering around in the bush at night hoping to find my way home!

So here's where I'm at with this project, I've put some comments in so you can see what I'm trying to accomplish. Any and all help will be appreciated.



:loop
#timer to power down
$x = 0
RepeatUntil($x = 30)
$x++
Sleep(1000)

EndRepeatUntil
repeatUntil(1=2)

#what to do when timer gets to 30

set(D2,OFF)
#$x = GetDigital(D2)
ControlCommand("Script Manager", ScriptStart, "power down")#do this once

sleep(5000) 
WaitForChange(GetDigital(D2))
sleep(5000)

#do this when the PIR is triggered after "power down"

$motion=getDigital(D2)
IF($motion=1) #pin went high
ControlCommand("Script Manager", ScriptStart, "Power up")#do this once
endif

#do this if PIR is triggered anytime after it has powered up

$motion=getdigital(D2)
if($motion=1)
#stay powered up - need something here - what?
sleep(10000)
EndIf

# if Pir is not triggered in 30 sec start the timer again

If($x = 30)
GoTo(loop)
endif
EndRepeatUntil


Thanks

PS. I would pay money to go to a course somewhere to learn EZ-Robot scripting!


ARC Pro

Upgrade to ARC Pro

Don't limit your robot's potential – subscribe to ARC Pro and transform it into a dynamic, intelligent machine.

#17  

I plugged in a Parallex PIR and watched it on the "Read ADC" and the "ADC Graph".When on low it shows .35V and some chatter on the bottom line of the graph. When it is triggered it goes to 2.5V and up the the line second from the top on the graph.

I have tried all sorts of different setting and nothing makes a difference. The"Monitoring" script goes to the "LowPowerMonitoring" script and then immediately to the "Power up" script . I have put in a delay in the "LowPowerMonitoring" script, to give the PIR time to reset. That did not make a difference, either.

#18  

Use the ADC value control instead...That will give you the actual value instead of a reference voltage... This way you can see at what point (actual value) where the PIR is triggered... Then use that value in your code.... Also run the monitoring code within the script and look at the script flow... see at what line (and if there is an error) the script decides to jump to power up script. This might help you figure out why it is bypassing some of your code... Another tip is to add print() statements here and there so you can check values as the code runs....

#19  

@Richard R When the PIR is low the value is 25 when it is triggered it goes to 195.I set the high to be >190. I put "print()" in both the monitor script and the lowpowermonitoring script and if reads >25 in both. As usual still not working.

Watching the LowPowermonitor script run, it runs through the script - does not "WaitForChange" Prints >25

United Kingdom
#20  

If the value is fluctuating while low (as I believe you said it is "chattering" on the low end) then WaitForChange() wont be waiting long as the value will be changing and the script will be assuming the chatter is motion being detected.

You could do it slightly differently and add in another script that's always running which checks the ADC value and if it's above a certain value it sets a variable, if below it changes the variable... i.e.


:loop
#Fetch ADC value
$PIR = GetADC(ADC7)

If($PIR > 200)
  $motion = 1
Else
  $motion = 0
EndIF

Sleep(100)
Goto(loop)

When the PIR is triggered it will set $motion to 1. When it's not then $motion is reset to 0

You can then use $motion in your original script to replace the GetDigital() command

#21  

Thanks Rich, I applied your new script and it senses the change as does the "Monitoring script" but nothing happens. I must have something out of place in the "monitoring script" below;


# Monitor for movement for 30 seconds

# Set a lable for the start
:start

# Reset the counter
$Monitor_Counter = 0

# Repeat
RepeatUntil($Monitor_Counter = 30)

# Increase the counter by 1
$Monitor_Counter++

# Check for detection on PIR - since we need to check 5 times per second use repeat

# checking counter needed
$Monitor_Check_Counter = 0

RepeatUntil($Monitor_Check_Counter = 5)
# Add 1 to the check counter
$Monitor_Check_Counter++

# Check the status of the digital port
# Check the status of the ADC port
#$PIR = GetDigital(D2)
$PIR = GetADC(ADC7)

# Check if it is high or low and take action
IF($PIR > 195)

 $motion = 1
Else
 $motion = 0
#EndIF
 # Motion detected, return to start
 Goto(start)
Endif
# Note - no else or elseif required since nothing happens if motion isnt detected
print($PIR)
# Wait a while to save system resources
Sleep(200)
EndRepeatUntil

# 1 second has passed by this point
# repeat 30 times
EndRepeatUntil



# 30 seconds passed - Enter low power plus monitoring mode
ControlCommand("Script Manager", ScriptStart, "LowPowerMonitoring")

# End the script
HALT()

#22  

Hi Rich, when you get a chance, could you have a look at this for me. Thanks

United Kingdom
#23  

Yeah, sorry run out of time yesterday.


# Monitor for movement for 30 seconds

# Set a lable for the start
:start

# Reset the counter
$Monitor_Counter = 0

# Repeat
RepeatUntil($Monitor_Counter = 30)

# Increase the counter by 1
$Monitor_Counter++

# Check for detection on PIR - since we need to check 5 times per second use repeat

# checking counter needed
$Monitor_Check_Counter = 0

RepeatUntil($Monitor_Check_Counter = 5)
# Add 1 to the check counter
$Monitor_Check_Counter++

# Check the status of the ADC port
$PIR = GetADC(ADC7)

# Check if it is high or low and take action
# Convert ADC to simple high/low
IF($PIR > 195)
$motion = 1
Else
$motion = 0
EndIF

IF($motion = 1)
# Motion detected, return to start
Goto(start)
Endif
# Note - no else or elseif required since nothing happens if motion isnt detected
print($PIR)
# Wait a while to save system resources
Sleep(200)
EndRepeatUntil

# 1 second has passed by this point
# repeat 30 times
EndRepeatUntil



# 30 seconds passed - Enter low power plus monitoring mode
ControlCommand("Script Manager", ScriptStart, "LowPowerMonitoring")

# End the script
HALT()

This is all from memory and without ARC. It looked like you had it detecting motion when there was none and vice versa. This was causing it to reset the timer and start again.

You could remove the whole converting it to 0 or 1 though, it's pretty redundant.

# Monitor for movement for 30 seconds

# Set a lable for the start
:start

# Reset the counter
$Monitor_Counter = 0

# Repeat
RepeatUntil($Monitor_Counter = 30)

# Increase the counter by 1
$Monitor_Counter++

# Check for detection on PIR - since we need to check 5 times per second use repeat

# checking counter needed
$Monitor_Check_Counter = 0

RepeatUntil($Monitor_Check_Counter = 5)
# Add 1 to the check counter
$Monitor_Check_Counter++

# Check the status of the ADC port
$PIR = GetADC(ADC7)


IF($PIR > 195)
# Motion detected, return to start
Goto(start)
Endif
# Note - no else or elseif required since nothing happens if motion isnt detected
print($PIR)
# Wait a while to save system resources
Sleep(200)
EndRepeatUntil

# 1 second has passed by this point
# repeat 30 times
EndRepeatUntil



# 30 seconds passed - Enter low power plus monitoring mode
ControlCommand("Script Manager", ScriptStart, "LowPowerMonitoring")

# End the script
HALT()

#24  

Thanks Rich, I made those changes, however, it did not work as expected. I noticed that the "LowPowerMonitor" script ran thru to the end and stopped, so I added in a "GoTo" command and I worked, BUT now everytime the PIR is triggered it runs thru the "PowerUp" command. After Power Up it returns to the "monitor" script though.


# Enter your low power mode commands here

sayezb("i am powering down")
:start
# Once in low power mode wait for a change on the PIR ADC port
WaitForChange(GetADC(ADC7))
$PIR = GetADC(ADC7)
If($PIR > 180)
$motion = 1
#print($PIR)

# Power up robot
Sleep(2000)
ControlCommand("Script Manager", ScriptStart, "Power Up")
EndIf
Goto(start)
# And end this script
#HALT()

Thanks, we are getting closer. Thanks for the nice comments you said about my project.