pir and ldr combo code

ossipee

New Member
Hi all very new to picaxe and there is probably a better way to do this, but did not find a lot of code for pir's out there in googles, so any suggestions are great, this is part of a chicken protection system, I live in a rural Maine US town Porter if you care to know, that has lots of critters that like to eat chickens everything from weasels to bears. I keep a few, 8 or so Rhode Island Reds for fresh eggs, and wish to keep them safe. The idea is some pir sensors, an ldr sensor, an alarm siren, and bright lights which I want to turn on only after dark. The following code has been bread boarded and works as intended on an 08m2. This is my only my second bit of working project code, I am 52 yo newbie to this stuff so any ideas, pointers, or primers gratefully accepted.

Code:
main:
readadc 1,b1  'pir sensor
readadc 4,b4  'ldr sensor 
if b1> 0 and b4> 100 then sethigh  'turn on siren alarm 
if b1> 0 and b4< 100 then sethigh2  'turn on flood lights and siren alarm
if b1= 0 then setlow  'nothing is around all quiet
goto main

sethigh:  'siren only
high 0
goto main

sethigh2:  'siren and lights
high 0
high 2
goto main

setlow:  'no critters siren lights off
low 0
low 2
goto main
and so since I am going to need more than 1 pir sensor to cover coop and run area, this is the code scaled up for 6 pirs and one ldr, on a 14m2, and I'll bet there is a way better way to do this, but it seems to work. I wondered about putting all pir sensors to one pin but feared it would over load it. Would like to get it all on the little 08m2 board but as a newbie felt this was safer and easier to trouble shoot should a pir fail.
Code:
main:
readadc 1,b1     'ldr sensor
readadc 2,b2     'pir sensor 1
readadc 3,b3     'pir sensor 2
readadc 4,b4     'pir sensor 3
readadc 5,b5     'pir sensor 4
readadc c.0,b6   'pir sensor 5
readadc c.4,b7   'pir sensor 6
if b7>0 or b2>0 or b3>0 or b4>0 or b5>0 or b6> 0 and b1> 100 then sethigh 'its light out audible alarm only
if b7>0 or b2>0 or b3>0 or b4>0 or b5>0 or b6> 0 and b1< 100 then sethigh2'its dark out lights on audible on
if b7=0 and b2=0 and b3=0 and b4=0 and b5=0 and b6=0 then setlow'no critters about all quiet on chicken front
goto main

sethigh:
high c.2         'siren alarm'
goto main

sethigh2:
high c.1         'flood lights and alarm siren
high c.2
goto main

setlow:
low c.1          'all quiet
low c.2
goto main
Thanks for looking ossipee [river I live on banks of]
 

westaust55

Moderator
Welcome to the PICAXE forum.

Are the PIR outputs an analog signal or a contact with a state change with the PIR detects movement?
If it is simply a digital output contact changing state, then rather than using READADC commands to detect the contacts as open (= 0) or closed (=1).
Then you can assign each PIR output to a bit variable
LET bit0 = pinB.1 ; bit0 is a part of byte variable b0
LET bit1 = pinB.2
LET bit2 = pinB.3
LET bit3 = pinB.4
LET bit4 = pinC.0
LET bit5 = pinC.4 ; bit5 is a part of byte variable b0


And rather than having a lot of tests for byte variables and values > 0, you can just test if the byte variable b0 (made up of bit variables bit0 to bit7) is 0 or >0 where 0 is okay and any value greater than 0 is an alarm state.


Again, assuming the PIR outputs are simple contacts and not analog signals you should have pull down resistors on each PICAXE input so that the input is not floating when the contact is open leading to erroneous states and alarms.

It might be warranted to post your schematic diagram and some information on the PIR&#8217;s if you seek further feedback
 

ossipee

New Member
Thank you very much kind sir, I did not understand byte bit thing at all now I have small clue, the pir's when activated are around 3.3v so enough for the 14m2 to sense this is what I did with your very nice explanation, and seems to work will bread board whole thing tomorrow. Again many thanks!
Code:
LET bit0 = pinB.1 ; bit0 is a part of byte variable b0
LET bit1 = pinB.2
LET bit2 = pinB.3
LET bit3 = pinB.4
LET bit4 = pinB.5
LET bit5 = pinC.0 ; bit5 is a part of byte variable b0
SYMBOL Ldr = pinC.4 



main:
if b0>0 then alarm
if b0>0 and ldr>100 then alarmlight
goto main

alarm:
high C.2
goto main

alarmlight:
high C.1
high C.2
goto main
 
Last edited by a moderator:

westaust55

Moderator
You will need to repeat the check of the PIRs and update the bits within the byte on a cycle or the alarm and can never turn off when the movement has stopped.
I have taken the liberty to restructure your code as a possible example for you to work with.
Note also that I have included some indentation of the program lines so it is easier at a glance to see the structure of the program.


Code:
SYMBOL Ldr = pinC.4

main:
  DO
    LET bit0 = pinB.1 ; bit0 is a part of byte variable b0
    LET bit1 = pinB.2
    LET bit2 = pinB.3
    LET bit3 = pinB.4
    LET bit4 = pinB.5
    LET bit5 = pinC.0 ; bit5 is a part of byte variable b0

    if b0>0 THEN
      IF LDR > 100 THEN
	HIGH C.1
      ENDIF
      HIGH C.2
    ENDIF
    PAUSE 10000 ; delay 10,000 ms = 10 second - set what

    LOW C.1 ; turn off the light
    LOW C.2 ; turn off the alarm

  LOOP
 

jhowe

New Member
Would love to see your schematic if you get a chance.

I think this is a great idea for chicken protection.

On a low tech note, I have had good luck with just leaving a radio on in the coop.
The sound of human voices seems to scare off the critters.
 
Top