wildsquirrelfrenzy
Posts: 1
Joined: Fri Aug 10, 2018 4:54 am

Trying to make a countdown timer to trigger a camera, seeing odd behavior.

Fri Aug 10, 2018 5:16 am

My goal of this project is to use a pir sensor to detect motion and trigger a camera to start and stop recording. I want it to start recording the moment there is motion, but not stop until there has been no more motion for a set time period (5 seconds in this example). This is because I don't want it to stop recording if the subject just stops moving briefly. Right now I'm just debugging, but eventually I'll replace the print commands with the camera trigger commands.

However I am seeing very unusual behavior I can't figure out.

Code: Select all

import RPi.GPIO as GPIO        #Import GPIO library
import time                    #Import time library

GPIO.setmode(GPIO.BOARD)       #Set GPIO pin numbering
pir1 = 40                      #The bottom right last pin
GPIO.setup(pir1, GPIO.IN)        #Setup GPIO pin1 which is defined above as PIR as input

lastmotion = 0        #set this variable in the beginning to be higher than necessary
motion5s = 0                    #set the motion in last 5 seconds variable to 0 initially so no motion is detected at first

print ("Sensor initializing . . .")
time.sleep(4) #Give sensor time to startup
print ("Active")

while True:
  if GPIO.input(pir1) == True:     #If PIR pin is high
    lastmotion = time.time()       # Then update the time of last motion
  motionseconds = time.time() - lastmotion      # Get the amount of time since last motion
  if motionseconds < 5:
    motion5s = 1                   #set the motion in last 5 seconds variable to true
  elif motionseconds >= 5:
    motion5s = 0                   #set the motion in last 5 seconds variable to false
  if motion5s == True:
    print ("Motion was detected in last 5 seconds, was detected ") + str(motionseconds) + (" ago.")
  elif motion5s == False:
    print ("No motion detected within last 5 seconds, was detected ") + str(motionseconds) + (" ago.")
  time.sleep(0.25)           #quarter second delay
You can see I basically update time any time the pir sensor is high, and then use the current time minus the last time the pir sensor was high, to determine how long it has been.

However when running the code, here is what I see both before and after motion

Code: Select all

No motion detected within last 5 seconds, was detected 12.5270428658 ago.
No motion detected within last 5 seconds, was detected 12.7774059772 ago.
No motion detected within last 5 seconds, was detected 13.027780056 ago.
No motion detected within last 5 seconds, was detected 13.2781639099 ago.
No motion detected within last 5 seconds, was detected 13.5285508633 ago.
No motion detected within last 5 seconds, was detected 13.7789318562 ago.
No motion detected within last 5 seconds, was detected 14.0293040276 ago.
No motion detected within last 5 seconds, was detected 14.2796728611 ago.
No motion detected within last 5 seconds, was detected 14.5300650597 ago.
No motion detected within last 5 seconds, was detected 14.7804820538 ago.
No motion detected within last 5 seconds, was detected 15.0308680534 ago.
No motion detected within last 5 seconds, was detected 15.2812428474 ago.
No motion detected within last 5 seconds, was detected 15.5316300392 ago.
No motion detected within last 5 seconds, was detected 15.7820198536 ago.
Motion was detected in last 5 seconds, was detected 3.09944152832e-06 ago.
Motion was detected in last 5 seconds, was detected 4.05311584473e-06 ago.
Motion was detected in last 5 seconds, was detected 3.09944152832e-06 ago.
Motion was detected in last 5 seconds, was detected 3.09944152832e-06 ago.
Motion was detected in last 5 seconds, was detected 4.05311584473e-06 ago.
Motion was detected in last 5 seconds, was detected 3.09944152832e-06 ago.
Motion was detected in last 5 seconds, was detected 3.09944152832e-06 ago.
Motion was detected in last 5 seconds, was detected 3.09944152832e-06 ago.
Motion was detected in last 5 seconds, was detected 2.86102294922e-06 ago.
Motion was detected in last 5 seconds, was detected 3.09944152832e-06 ago.
Motion was detected in last 5 seconds, was detected 3.09944152832e-06 ago.
Motion was detected in last 5 seconds, was detected 5.00679016113e-06 ago.
Motion was detected in last 5 seconds, was detected 4.05311584473e-06 ago.
Motion was detected in last 5 seconds, was detected 3.81469726562e-06 ago.
Motion was detected in last 5 seconds, was detected 5.96046447754e-06 ago.
Motion was detected in last 5 seconds, was detected 2.86102294922e-06 ago.
Motion was detected in last 5 seconds, was detected 3.09944152832e-06 ago.
Motion was detected in last 5 seconds, was detected 8.10623168945e-06 ago.
Motion was detected in last 5 seconds, was detected 3.81469726562e-06 ago.
Motion was detected in last 5 seconds, was detected 4.05311584473e-06 ago.
Motion was detected in last 5 seconds, was detected 5.00679016113e-06 ago.
Motion was detected in last 5 seconds, was detected 4.05311584473e-06 ago.
Motion was detected in last 5 seconds, was detected 4.05311584473e-06 ago.
Motion was detected in last 5 seconds, was detected 3.09944152832e-06 ago.
I'm really confused why
1) The time displayed switches from a regular number to scientific notation once motion is detected even though the same exact variable is specified.
2) Why the very first time motion is detected, it already shows 3 seconds ago. My loop goes every .25 seconds, so motion should be less than that.
3) I'm confused why the numbers are jumping all over the board, for example going from 3 seconds to 8 seconds to 3 seconds again, all within a .25 second time period. The numbers "should" always be about .25 higher than the last time except for the case where motion was just detected, it should be close to 0.

This is my first time with python so if there is a better way to do this, please let me know.

pcmanbob
Posts: 4336
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Trying to make a countdown timer to trigger a camera, seeing odd behavior.

Fri Aug 10, 2018 11:38 am

Hi.

I suspect the reason your print out is changing to scientific notation is because the number is to long to be printed on the screen, this may also have some thing to do with the second problem you are having.

So I changed your code a bit to limit the number of figures after the decimal point to just 10 characters.
see if that solves your problems

Code: Select all

import RPi.GPIO as GPIO        #Import GPIO library
import time                    #Import time library

GPIO.setmode(GPIO.BOARD)       #Set GPIO pin numbering
pir1 = 40                      #The bottom right last pin
GPIO.setup(pir1, GPIO.IN)        #Setup GPIO pin1 which is defined above as PIR as input

lastmotion = 0        #set this variable in the beginning to be higher than necessary
motion5s = 0                    #set the motion in last 5 seconds variable to 0 initially so no motion is detected at first

print ("Sensor initializing . . .")
time.sleep(4) #Give sensor time to startup
print ("Active")

while True:
  if GPIO.input(pir1) == True:     #If PIR pin is high
    lastmotion = time.time()       # Then update the time of last motion
  motionseconds = time.time() - lastmotion      # Get the amount of time since last motion
  if motionseconds < 5:
    motion5s = 1                   #set the motion in last 5 seconds variable to true
  elif motionseconds >= 5:
    motion5s = 0                   #set the motion in last 5 seconds variable to false
  if motion5s == True:
    print ("Motion was detected in last 5 seconds, was detected %0.10f " %(motionseconds) + " ago.")
  elif motion5s == False:
    print ("No motion detected within last 5 seconds, was detected %0.10f " %(motionseconds) + " ago.")
  time.sleep(0.25)           #quarter second delay
  
note you don't need to str the results if you do it this way and the entire print line is contained within one set of brackets.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Return to “Python”

Who is online

Users browsing this forum: No registered users and 17 guests