trouble with coding >> PIRs & LEDs >> help please
Archiver
Posts: 46,084
hello - I'm looking to connect 2 PIR motion sensors to the BASIC
stamp in order to trigger 2 LEDs.
basically 4 components connected in parallel ...
having PIR#1 control LED#1, and PIR#2 control LED#2 ... I wanted to
do this using the basic stamp's ports P0, P1, P3, & P4
using P0 & P1 as the inputs for the PIRs, and P3 & P4 as the outputs
for the LEDs
(input/output relations: P0 & P3 ... P1 & P4)
the goal was to have both LEDS in an off state until motion is
detected, then have the LED flash for 1 second and then shut off
until motion is detected again ... I'm not certain if my BASIC coding
is correct, since I am a beginner to all of this, but i tried to use
a subroutine "flash" to accomplish this.
the first code (code 1) is how I want to define the in/out for the
basic stamp to recognize everything, but i don't know how to add it
to the second part of the code (code 2) to make it all work
together....
any help on this would be GREATLY appreciated,
Regards,
SD
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
code 1
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'{$STAMP BS2}
OUTPUT 0 'set P0 to be an output
OUTPUT 1 'set P1 to be an output
INPUT 3 'set P3 to be an input
INPUT 4 'set P4 to be an input
MAIN
OUT0 = IN3 'set LED1 = PIR1
OUT1 = IN4 'set LED2 = PIR2
GOTO MAIN 'jump back to beginning
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
code 2
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'{$STAMP BS2}
infiniteLoop: 'label
'if motion is detected, goto subroutine Flash1
IF IN3 = 1 THEN Flash1
IF IN4 = 1 THEN Flash2
GOTO infiniteLoop 'loop
Flash1: 'subroutine labeled Flash1
HIGH 0 'turn on LED
PAUSE 1000 'wait 1 second
LOW 0 'turn off LED
RETURN 'go back to the rest of the program
Flash2: 'subroutine labeled Flash2
HIGH 1 'turn on LED
PAUSE 1000 'wait 1 second
LOW 1 'turn off LED
RETURN 'go back to the rest of the program
stamp in order to trigger 2 LEDs.
basically 4 components connected in parallel ...
having PIR#1 control LED#1, and PIR#2 control LED#2 ... I wanted to
do this using the basic stamp's ports P0, P1, P3, & P4
using P0 & P1 as the inputs for the PIRs, and P3 & P4 as the outputs
for the LEDs
(input/output relations: P0 & P3 ... P1 & P4)
the goal was to have both LEDS in an off state until motion is
detected, then have the LED flash for 1 second and then shut off
until motion is detected again ... I'm not certain if my BASIC coding
is correct, since I am a beginner to all of this, but i tried to use
a subroutine "flash" to accomplish this.
the first code (code 1) is how I want to define the in/out for the
basic stamp to recognize everything, but i don't know how to add it
to the second part of the code (code 2) to make it all work
together....
any help on this would be GREATLY appreciated,
Regards,
SD
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
code 1
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'{$STAMP BS2}
OUTPUT 0 'set P0 to be an output
OUTPUT 1 'set P1 to be an output
INPUT 3 'set P3 to be an input
INPUT 4 'set P4 to be an input
MAIN
OUT0 = IN3 'set LED1 = PIR1
OUT1 = IN4 'set LED2 = PIR2
GOTO MAIN 'jump back to beginning
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
code 2
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'{$STAMP BS2}
infiniteLoop: 'label
'if motion is detected, goto subroutine Flash1
IF IN3 = 1 THEN Flash1
IF IN4 = 1 THEN Flash2
GOTO infiniteLoop 'loop
Flash1: 'subroutine labeled Flash1
HIGH 0 'turn on LED
PAUSE 1000 'wait 1 second
LOW 0 'turn off LED
RETURN 'go back to the rest of the program
Flash2: 'subroutine labeled Flash2
HIGH 1 'turn on LED
PAUSE 1000 'wait 1 second
LOW 1 'turn off LED
RETURN 'go back to the rest of the program
Comments
you ask. Note that it can only detect motion on one sensor at a time.
To detect motion simultaneously requires a trickier programming
technique (it can be done though). I'm assuming that you're using
standard LEDs so the program handles the blinking your specified.
' {$STAMP BS2}
' {$PBASIC 2.5}
Pir1 PIN 3
Pir2 PIN 4
Led1 PIN 0
Led2 PIN 1
Motion CON 1
IsOn CON 1
IsOff CON 0
timer VAR Nib
Setup:
LOW Led1 ' make LED pins outputs,
and off
LOW Led2
Main:
IF (Pir1 = Motion) THEN
FOR timer = 1 TO 10
Led1 = IsOn
PAUSE 50
Led1 = IsOff
PAUSE 50
NEXT
ENDIF
IF (Pir2 = Motion) THEN
FOR timer = 1 TO 10
Led2 = IsOn
PAUSE 50
Led2 = IsOff
PAUSE 50
NEXT
ENDIF
GOTO Main
Some notes:
* You don't have to define a pin as input unless it was previously an
output
(all BASIC Stamp pins are reset to inputs on power-up)
* PBASIC allows techniques to make programs very readable
(Download "The Elements of PBASIC Style" from our web site)
* RETURN is only valid with a GOSUB
-- Jon Williams
-- Applications Engineer, Parallax
-- Dallas Office
Original Message
From: Stefan Dukaczewski [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=Rvw7cR4z343Nf9f3j1jEfpEb4F_Bry-ujCWPb2SSIJBYfGXjOI5iavnGQlqC5v0XuyofPE_WOcl5wRhRRI3D]hcprogression@h...[/url
Sent: Tuesday, October 28, 2003 1:11 AM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] trouble with coding >> PIRs & LEDs >> help please
hello - I'm looking to connect 2 PIR motion sensors to the BASIC
stamp in order to trigger 2 LEDs.
basically 4 components connected in parallel ...
having PIR#1 control LED#1, and PIR#2 control LED#2 ... I wanted to
do this using the basic stamp's ports P0, P1, P3, & P4
using P0 & P1 as the inputs for the PIRs, and P3 & P4 as the outputs
for the LEDs
(input/output relations: P0 & P3 ... P1 & P4)
the goal was to have both LEDS in an off state until motion is
detected, then have the LED flash for 1 second and then shut off
until motion is detected again ... I'm not certain if my BASIC coding
is correct, since I am a beginner to all of this, but i tried to use
a subroutine "flash" to accomplish this.
the first code (code 1) is how I want to define the in/out for the
basic stamp to recognize everything, but i don't know how to add it
to the second part of the code (code 2) to make it all work
together....
any help on this would be GREATLY appreciated,
Regards,
SD
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
code 1
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'{$STAMP BS2}
OUTPUT 0 'set P0 to be an output
OUTPUT 1 'set P1 to be an output
INPUT 3 'set P3 to be an input
INPUT 4 'set P4 to be an input
MAIN
OUT0 = IN3 'set LED1 = PIR1
OUT1 = IN4 'set LED2 = PIR2
GOTO MAIN 'jump back to beginning
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
code 2
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'{$STAMP BS2}
infiniteLoop: 'label
'if motion is detected, goto subroutine Flash1
IF IN3 = 1 THEN Flash1
IF IN4 = 1 THEN Flash2
GOTO infiniteLoop 'loop
Flash1: 'subroutine labeled Flash1
HIGH 0 'turn on LED
PAUSE 1000 'wait 1 second
LOW 0 'turn off LED
RETURN 'go back to the rest of the program
Flash2: 'subroutine labeled Flash2
HIGH 1 'turn on LED
PAUSE 1000 'wait 1 second
LOW 1 'turn off LED
RETURN 'go back to the rest of the program
To UNSUBSCRIBE, just send mail to:
basicstamps-unsubscribe@yahoogroups.com
from the same email address that you subscribed. Text in the Subject
and Body of the message will be ignored.
Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/
This message has been scanned by WebShield. Please report SPAM to
abuse@p....