PIR triggering issues

Halstaff

New Member
I'm having difficulty getting my Parallax PIR to work properly in my program. I've used the simple test code below and everything works fine. The PIR waits for motion to trigger, it lights the LED on b.1, resets to 0 and waits for motion to retrigger. I've used this code successfully in several controllers I've built with the 08 Picaxe and with the exception of some false triggers outside during the day, it's worked fine.

Code:
#Picaxe 18M2

Testit:
                
                readadc b.7,b0
                debug b0
                pause 5                     'Brief pause on the checking loop
                b0 = b0 + 5 * b.7
                if b0 < 100 then Testit 
                
                goto Routine
    
Routine:

                high b.1                
                pause 3000
                low b.1
                pause 5000
                goto Testit
The problem comes into play when I add the PIR code in with the rest of the program. The PIR will trigger without any motion and will not reset to 0 causing the program to immediately restart when it loops back. I've added an LED into the PIR wire and it stays lit most of the time but does have periods where it will go out. Here's the code for the complete program which is controlling one of the scenes in my Halloween display. Any help would be appreciated.

Code:
'Picaxe 18M2 Seance Room Controller

'B.0 is Bookshelf motor and light
'B.1 is Light for Rocking chair
'B.2 Audio in for jaw servo
'B.3 Starts audio
'B.4 is Wiper Motor for rocking chair
'B.6 is Skeleton jaw servo
'B.5 is DVD servo
'B.7 is PIR



#Picaxe 18M2

Init: 					'Just a starting point label	
					
  debug 
  Pause 6000'0				'Wait 60 secs for PIR to 'settle down
  Symbol btime = b4 			 
  Symbol delay = 5000 			'Sets variable delay for making the whole thing wait for a while after pressing the button
  Servo b.5,75
  

Testit:

	readadc b.7,b0
	debug b0
	pause 5				'Brief pause on the checking loop
	b0 = b0 + 5 * b.7
	if b0 < 100 then Testit 
	
	goto Routine
    
Routine:

  
	high b.1				'Light comes on Rocking Chair
	high b.4				'Skeleton starts rocking
	high b.0				'Bookshelf starts moving and its light comes on

	 
 	high b.3 				'Serial output pin idle-high
      wait 2 				'Allow module to fire up
  	serout b.3,T4800,($EF) 		'STOP module
 	wait 2
  	serout b.3,T4800,($01) 		'PLAY song 001.mp3


 
 	 
	
	w3=5000                     	'Counter for number of loops needed, every 1000 = about 15 seconds +/-

	b2=8 					'Set pause delay
	b3=180				'Set Servo Min position and also offset - mouth open
	b4=214				'Set Servo Max position - mouth closed
	Servo b.6,b3 			'Initialises b.6 as a servo output and sets servo to the start position

OP:

ADCA: 					'Reads input voltage in 256 steps Pin 1 (leg 6)
	
	Readadc b.2,b5 			'Read input voltage into b0
	b1=225-b5+b3 			'Add offset for servo's CCW output 
	If b1<b3 then gosub servomax 	'Test for less than servo min position and if less, set at servo min
	If b1>b4 then gosub servomin 	'Test for more than servo min position and if more, set at servo max

MoveA: 					'Move servoA
	servopos b.6, b1 			'Pulse pin 4 (leg 3) width=b1 - Using servopos can help prevent jitter

	pause b2 				'Wait Delay b2

      w3=w3-1                       'Decrement the counter
      if w3>0 then goto OP          'Test for counter being larger than 0 if yes, go round the loop again

      goto Projection
      
      goto OP 		
		 
servomin:
	b1=b3 
	return	

servomax:
	b1=b4
	return 
	
Projection:

	low b.1				'Light goes off on Rocking Chair
	low b.4				'Skeleton stops rocking
	
	goto Pressdown 

pressdown:
	servopos b.5,160 			'Move servo to other end to press button and start Madame Leota 
	pause 500 				'Hold the button down for a half a second	
	servopos b.5,75 			'Move the servo back to the other position

	w3=5000
Madame:

	w3=w3-1                       'Decrement the counter
      if w3>0 then goto Madame      'Test for counter being larger than 0 if yes, go round the loop again

	goto Hold
	
Hold:
	low b.0				'Bookshelf stops and its light goes out		
	for btime = 1 to 30 		'30 is the number of seconds of retriggger delay
	pause 1000 				'Pause for 1 sec 
	next btime
	goto Testit	 			'Go back and wait for the PIR again
 

John West

Senior Member
I'd like to help, but I'm none too good at code and I don't know what you're up to with this bit:

b0 = b0 + 5 * b.7

Perhaps some further code comments might be in order.
 

John West

Senior Member
I'd like to help, but I'm none too good at code and I don't know what you're up to with this bit:

b0 = b0 + 5 * b.7

Perhaps some further code comments might be in order.
 

Jeff Haas

Senior Member
John - The "Testit" loop is for debouncing the PIR. To quote from an article, "What this means is that the controller will not respond to the PIR input until it has gone active and stayed active for some specified time; this doesn't have to be a long time (in human terms), about 1/4 second is plenty." Also, you typically have a pull-down resistor on the pin the PIR is attached to, so the software is looking for the pin to go high and there isn't a "floating" input. This also helps get rid of spurious results from the PIR.

Steve - A few notes, hope they help a bit.

First, how does the whole program work when you use a regular switch in place of the PIR? Is this just about the PIR's reliability?

Comments on the code, I may know less than you so excuse any silly comments...

Init section, line 2: Pause 6000'0 - Why is there an apostrophe in the pause value? That makes it a six-second pause instead of a 60-second pause. The PIR won't settle properly

I'm new to this concept in Picaxe, but I see two things defined that could conflict:
b0 - Used in Testit
b.0 - Used as output for bookshelf
Is this OK?

This line:
Readadc b.2,b5 'Read input voltage into b0 - Comment says it's reading into b0. Seems to me it's reading into b5.

Jeff
 

hippy

Technical Support
Staff member
I'd like to help, but I'm none too good at code and I don't know what you're up to with this bit:

b0 = b0 + 5 * b.7
That's a clever trick for ensuring there is a timeout period where a pin remains high, that is, it doesn't timeout and exit the DO-LOOP it's in until pin has been high and has remained only high for a period.

While the pin (B.7) is high (1) then b0 will increment ( N+K * 1 => N+K )

If the pin (B.7) is low (0) then b0 will be reset to zero ( N+K * 0 => 0 )

That could be the issue here as B.7 is read as an ADC input, then read as a digital input.

On the M2's, once used as an ADC input the digital input will then always return a zero value. The 'adcsetup' variable needs to be changed to re-enable the pin to be used for digital input after being used as an ADC input.
 

Halstaff

New Member
John - The "Testit" loop is for debouncing the PIR. To quote from an article, "What this means is that the controller will not respond to the PIR input until it has gone active and stayed active for some specified time; this doesn't have to be a long time (in human terms), about 1/4 second is plenty." Also, you typically have a pull-down resistor on the pin the PIR is attached to, so the software is looking for the pin to go high and there isn't a "floating" input. This also helps get rid of spurious results from the PIR.

Steve - A few notes, hope they help a bit.

First, how does the whole program work when you use a regular switch in place of the PIR? Is this just about the PIR's reliability?

Comments on the code, I may know less than you so excuse any silly comments...

Init section, line 2: Pause 6000'0 - Why is there an apostrophe in the pause value? That makes it a six-second pause instead of a 60-second pause. The PIR won't settle properly

I'm new to this concept in Picaxe, but I see two things defined that could conflict:
b0 - Used in Testit
b.0 - Used as output for bookshelf
Is this OK?

This line:
Readadc b.2,b5 'Read input voltage into b0 - Comment says it's reading into b0. Seems to me it's reading into b5.

Jeff
Jeff,
Thanks for the comments and I'm not sure you know less than me as I'm very new to this.
I don't think using both b0 and b.0 should be a conflict but I could be wrong which seems to happen a lot as I'm learning this.
The apostrophe is in the pause for testing purposes so that I don't have to wait. I've tried it with my usual 60 second pause to settle the PIR and it doesn't seem to fix the problem.
The readadc b.2,b5 is reading into b5, not b0. I've tried so many different things that I missed changing the comment.
After designing the Kiwi board and the Tenda/servo board to get my skulls to talk, it's been a challenge getting this last bit of programming right. I guess if it all went right the first time, there wouldn't be any challenge.
Steve
 

Halstaff

New Member
That's a clever trick for ensuring there is a timeout period where a pin remains high, that is, it doesn't timeout and exit the DO-LOOP it's in until pin has been high and has remained only high for a period.

While the pin (B.7) is high (1) then b0 will increment ( N+K * 1 => N+K )

If the pin (B.7) is low (0) then b0 will be reset to zero ( N+K * 0 => 0 )

That could be the issue here as B.7 is read as an ADC input, then read as a digital input.

On the M2's, once used as an ADC input the digital input will then always return a zero value. The 'adcsetup' variable needs to be changed to re-enable the pin to be used for digital input after being used as an ADC input.
That certainly sounds like it could be causing my problem. Excuse a beginners ignorance, but how would I change the code to fix this? I'm not getting it to work.
Thanks for the help.
Steve
 
Last edited:

Paix

Senior Member
Probably well off track, so don't take it too seriously. I notice that B.4 is described as a wiper motor to operate the rocking chair. Now I don't know if this is a model or a full sized set, which leads me to believe that you are switching on and off a windscreen/windshield wiper motor to rock a life sized rocking chair.

Neither Hippy, not John have yet asked for a look at your complete circuit diagram, but perhaps it might be time - please.

I suppose your circuit still fails when you disconnect the wiper motor from the circuit. My thinking is that it will pull a fair bit of current and if the wiring is, how shall I say this, geographical then you could be inducing glitches which may have some bearing on the problem.

You did say that the PIR code worked OK, but the problems started when you incorporated it with your other bits of program. Adding the ADC may just be the Achilies heel that the motor spikes were waiting for.

All off the top of my head and not reasoned from the code at all, just the comment about the wiper motor.

Does it work OK with the wiper motor out of circuit?
 

Halstaff

New Member
Probably well off track, so don't take it too seriously. I notice that B.4 is described as a wiper motor to operate the rocking chair. Now I don't know if this is a model or a full sized set, which leads me to believe that you are switching on and off a windscreen/windshield wiper motor to rock a life sized rocking chair.

Neither Hippy, not John have yet asked for a look at your complete circuit diagram, but perhaps it might be time - please.

I suppose your circuit still fails when you disconnect the wiper motor from the circuit. My thinking is that it will pull a fair bit of current and if the wiring is, how shall I say this, geographical then you could be inducing glitches which may have some bearing on the problem.

You did say that the PIR code worked OK, but the problems started when you incorporated it with your other bits of program. Adding the ADC may just be the Achilies heel that the motor spikes were waiting for.

All off the top of my head and not reasoned from the code at all, just the comment about the wiper motor.

Does it work OK with the wiper motor out of circuit?
I don't have a schematic as I designed the circuit in PEBBLE but I'll get a copy posted when I get home tonight.
So far I'm only using an LED on each of the motor channels to make sure there's no issue. If the wiper motor does cause a problem, I'll add a separate 5V power supply just for the wiper motor.
 

Halstaff

New Member
I know this isn't a schematic but being such a beginner with no formal electronics or programming education, I find it easier to design a circuit in PEBBLE.
I used a Kiwi board and much the same design that I've successfully used with the 08M Picaxe.

 

John West

Senior Member
Sorry for the double post above. The site often gets very slow for me in the evening. I'm -7 hours of England, I think. Early morning hours in England. This time it was several minutes between posts and observed results. I finally gave up on trying to help with the code problem when I couldn't get through at all.

Admin: Is this due to regular system maintenance? If so, I'd like to know what hours the system is typically occupied doing such, so I can save on time, energy and frustration trying to get through. Thanks.
 

srnet

Senior Member
"I know this isn't a schematic but being such a beginner with no formal electronics or programming education, I find it easier to design a circuit in PEBBLE."

Maybe so, but whilst you might find it easier to design that way, its a lot harder for everyone else to give you assistance.

Just draw the circuit with paper and pencil, it will save you heaps of time in the end ....
 

hippy

Technical Support
Staff member
On the M2's, once used as an ADC input the digital input will then always return a zero value. The 'adcsetup' variable needs to be changed to re-enable the pin to be used for digital input after being used as an ADC input.
That certainly sounds like it could be causing my problem. Excuse a beginners ignorance, but how would I change the code to fix this? I'm not getting it to work.
The simple brute force solution would be 'adcsetup=0' ( see Manual 2 ), but that doesn't seem to be the issue here.

The fundamental question is why you are treating the input as both analogue and digital ? And on top of that the timeout period depends on the analogue value being read.

Normally one would treat an input entirely as analogue or entirely as digital. A mix-and-match hack as here can work but it's dubious practice at best. It would really need an explanation as to why this is being used to validate it. To me, it looks like a solution thrown together which just happened to work rather than designed to perform the required task. A bit like having walked a tightrope doesn't mean you know how to walk a tightrope and could well fall off anything but the one you can walk.

I'm not sure what the code is intended to do and have got confused by the comments in post #3 which suggested how it should work, but that seems at odds with what the code would do. I think the best thing is to describe what you are trying to do with PIR trigger handling; does the PIR go high or go low when triggered, are you wanting to check it's gone high for a certain length of time or something else ?

If you are just looking for a high going pulse of a certain period you could probably just use -

Code:
Testit:
  Do : Loop Until pinB.7 = 1
  Do
    w0 = w0 + 1 * pinB.7
  Loop Until w0 > [i]N[/i]
And adjust N to be a number which gives a reliable result.

Bingo on the Bug Front!

While editing the original code in post #1 to the above, I noticed you used "B.7" not "pinB.7".

So what exactly was the code in the 08 which worked ?
 

Halstaff

New Member
The simple brute force solution would be 'adcsetup=0' ( see Manual 2 ), but that doesn't seem to be the issue here.

The fundamental question is why you are treating the input as both analogue and digital ? And on top of that the timeout period depends on the analogue value being read.

Normally one would treat an input entirely as analogue or entirely as digital. A mix-and-match hack as here can work but it's dubious practice at best. It would really need an explanation as to why this is being used to validate it. To me, it looks like a solution thrown together which just happened to work rather than designed to perform the required task. A bit like having walked a tightrope doesn't mean you know how to walk a tightrope and could well fall off anything but the one you can walk.

I'm not sure what the code is intended to do and have got confused by the comments in post #3 which suggested how it should work, but that seems at odds with what the code would do. I think the best thing is to describe what you are trying to do with PIR trigger handling; does the PIR go high or go low when triggered, are you wanting to check it's gone high for a certain length of time or something else ?

If you are just looking for a high going pulse of a certain period you could probably just use -

Code:
Testit:
  Do : Loop Until pinB.7 = 1
  Do
    w0 = w0 + 1 * pinB.7
  Loop Until w0 > [i]N[/i]
And adjust N to be a number which gives a reliable result.

Bingo on the Bug Front!

While editing the original code in post #1 to the above, I noticed you used "B.7" not "pinB.7".

So what exactly was the code in the 08 which worked ?
Hippy,
You've let the cat out of the bag and revealed that I really don't know what I'm doing. A year ago I couldn't solder 2 wires together but I've been slowly learning with only the internet and some new friends I've met on this journey (thanks Fritz).
Much of the code I've used, I got by copying what others have done. More by trail and error, I've found things that work and have used them. I know that I don't have the knowledge of most of the people here but I'm trying to learn.
Here's the program I used for my bedroom scene using an 08 and the same Parallax PIR that I'm trying to get to work with the 18M2. All my previous circuits have used the 08 and this is the first time I've used another chip.
Steve


Code:
'Bedroom Scene

#Picaxe 08M

Init: 				'Just a starting point label	  		
  Pause 60000 			'Wait 60 secs for PIR to 'settle down 
  Symbol delay = 5000 		'Sets variable delay for making the whole thing wait for a while after pressing the button  
  Symbol time=b1	

		
	low 0	 			'Start ambient audio track
	low 1				'Start candles
		 
Testit: 
	readadc 4,b0
	pause 5			'Brief pause on the checking loop
	b0 = b0 + 5 * Pin4	'Pin 4 is PIR
	if b0 < 100 then Testit 		
	goto BEGIN 
	
Begin:
	high 2			'Start fans
	pause 3500			'Pause for fans to "blow out" candles
	high 0			'Stop ambient track and start book effect and audio track
	high 1	 		'Switch lights, candles off, blacklights on
	pause 2500
	low 2				'Stop fans
	pause 50000
	low 1				'Switch lights back, candles on, blacklights off
	low 0 			'Stop effect audio and start ambient track 
	for time = 1 to 45 	'20 is the number of seconds of retrigger delay
	pause 1000 			'Pause for 1 sec 
	next time
Goto Testit
 
Last edited:

hippy

Technical Support
Staff member
Hippy, You've let the cat out of the bag and revealed that I really don't know what I'm doing.
No shame in that and we all had to start somewhere. And as myself and others have pointed out at times you can learn more when things go wrong than when they go right and that's probably the case here.

Simply try changing -

b0 = b0 + 5 * b.7

to -

b0 = b0 + 5 * pinb.7

If that works and you can work out why the original code "pin4" ( equivalent pinB.7 here ) worked whereas "4" (equivalent "B.7" ) would not you'll have gained valuable experience.

It's a simple "porting error" in copying and converting code for one PICAXE to another and one that can be hard to spot. Note that I didn't spot it in post #5 when describing how it should work - a case of seeing what should be there as if it were there. Obvious when finally spotted, and most bugs are like that !
 

John West

Senior Member
A good try on getting your code in a scroll window, Halstaff, but you used the wrong bracket following your first "code" instruction. You can correct that by hitting the "Edit Post" button at the bottom of your post. I do it all the time.
 

Halstaff

New Member
Yep. Silly me. I didn't look past the first error. As I make multiple errors on a regular basis, one would think I'd learn to look for them from others as well. I'll learn.
Thanks guys. Just shows I should check my posts after submitting them.
 

John West

Senior Member
I know that I don't have the knowledge of most of the people here but I'm trying to learn.
Halstaff, you, and others like you are who this forum is for. I'm sure Rev Ed could care less if crotchety old codgers who have been doing electronics and programming all their lives have a forum in which to complain to each other. :)
 
Top