I need help with with writing a sketch to use a pir sensor and servos. Im not sure how to add the pir sensor to the sketch or where to put it in the sketch I know that people will reference the button sketch but it doesn’t tell me where or how to add it, this is what it says but It doesn’t tell me where to place the pir sensor in sketch, all Im trying to do is have 6 servos to do motion if someone walks up. I can wire up sensor see it working on sensor screen but I don’t know where or how to put it in sketch can someone help me
I have read all of the post I can fine but I can’t find one that tells me where or how to do it
goto main_loop # Run the main loop when the script starts (see below).
# This subroutine returns 1 if the button is pressed, 0 otherwise.
# To convert the input value (0-1023) to a digital value (0 or 1) representing
# the state of the button, we make a comparison to an arbitrary threshold (500).
# This subroutine puts a logical value of 1 or a 0 on the stack, depending
# on whether the button is pressed or not.
sub button
0 get_position 500 less_than
return
# This subroutine uses the BUTTON subroutine above to wait for a button press,
# including a small delay to eliminate noise or bounces on the input.
sub wait_for_button_press
wait_for_button_open_10ms
wait_for_button_closed_10ms
return
# Wait for the button to be NOT pressed for at least 10 ms.
sub wait_for_button_open_10ms
get_ms # put the current time on the stack
begin
# reset the time on the stack if it is pressed
button
if
drop get_ms
else
get_ms over minus 10 greater_than
if drop return endif
endif
repeat
# Wait for the button to be pressed for at least 10 ms.
sub wait_for_button_closed_10ms
get_ms
begin
# reset the time on the stack if it is not pressed
button
if
get_ms over minus 10 greater_than
if drop return endif
else
drop get_ms
endif
repeat
# An example of how to use wait_for_button_press
I had found this from Paul
but It doesn’t tell me where to add it to a sketch or how it knows what pervo pin the pir sensor is on
Yes, the Maestro can do that. Basically, you have to write a program like this:
begin # loop forever
detector if # you write the "detector" function
20 # do the following loop 20 times (30 seconds * 20 = 10 minutes)
begin dup while
position1 # you write this function - go to position 1
15000 delay # wait 15 seconds (max possible delay is 32767)
position2 # you write this function - go to position 2
15000 delay # wait 15 seconds
1 minus
end
drop # remove the counter from the stack
end
repeat
The functions might be as simple as:
def detector
0 get_position 500 less_than
return
def position1
4000 1 servo
return
def position2
8000 1 servo
return
Hello.
You can check the input from a sensor on a Maestro’s input channel using the GET_POSITION
command. Our PIR sensor should send a low signal when it detects motion, so GET_POSITION
should return a 0 when it detects and 1023 otherwise. To the Maestro, this works the same way as a button.
How you add the sensor to your sketch and where you should put it depends on what behavior you want. It is not clear to me how you want your code to behave after it senses someone (e.g. continue looping forever, run through your script once and check the sensor again, run for a certain amount of time), but if you just want your script to wait until the PIR sensor detects something before starting, you can add a WHILE
loop like this to the start of your current script:
begin
11 get_position 500 greater_than while
repeat
If you try adding this and run into problems, can you post your complete script along with a description of what it is doing and what you want it to do?
Brandon
Thank you for the response
I just want it to run through the script once and check the sensor again
Ok I added begin 11 get_position 500 greater_than while repeat see below to the sketch
and It dint change anything the sketch still works but once you start it it will not stop. I just made a simple sketch to not clutter up anything.
the pir sensor can be made to go high with my hand I remove my hand and it will go low and it is on pin 11
begin
11 get_position 500 greater_than while
repeat
begin
500 0 0 0 0 0 9620
0 0 0 0 0 frame_0..10 # Frame 0
500 7744 frame_5 # Frame 1
500 9856 frame_5 # Frame 2
repeat
sub frame_0..10
10 servo -- 9 servo
8 servo
7 servo
6 servo
5 servo
4 servo
3 servo
2 servo
1 servo
0 servo
delay
return
sub frame_5
5 servo
delay
return
Thank you for posting your script. That script should stay in a while loop until the signal pin monitoring the PIR pin is low, then go to the next BEGIN/REPEAT
loop endlessly. If you want it to go back to monitoring the PIR sensor pin once it runs through the sequence, you can move the while loop so it is at the beginning of your program, but inside of your main loop. Then, your main BEGIN/REPEAT
loop would look something like this:
begin
begin
11 get_position 500 greater_than while #wait in while loop until sensor detects something
repeat
500 0 0 0 0 0 9620 # run the sequence
0 0 0 0 0 frame_0..10 # Frame 0
500 7744 frame_5 # Frame 1
500 9856 frame_5 # Frame 2
repeat # go back to the start and enter the while loop again
By the way, are you using the PIR Motion Sensor we carry? It sounds like you are saying that the PIR sensor is outputting a high signal when it detects your hand, but our PIR sensor should drive the alarm pin low when motion is sensed. If your PIR sensor works the opposite of ours, you could change the GREATER_THAN
command to a LESS_THAN
to flip the logic in the script to work with it.
Brandon
OK I tried to make it like you showed me but I get a error because of the begin block was never closed
I took away the extra begin and it will do the same thing ( I did change the greater to less ) It will wait for motion which I want, sees motion which I want, run the sketch which I want, then never stops which I don’t want.
# test
begin
begin
11 get_position 500 less_than while #wait in while loop until sensor detects something
repeat
begin
500 0 0 0 0 0 9620
0 0 0 0 0 frame_0..10 # Frame 0
500 7744 frame_5 # Frame 1
500 9856 frame_5 # Frame 2
repeat # go back to the start an enter the while loop again
sub frame_0..10
10 servo
9 servo
8 servo
7 servo
6 servo
5 servo
4 servo
3 servo
2 servo
1 servo
0 servo
delay
return
sub frame_5
5 servo
delay
return
It looks like you left an extra BEGIN
in your code that is not in the last version I posted. I suspect it will work correctly if you remove the BEGIN
just before the 500 0 0 0 0 0 9620
line of code.
Brandon
Thank you for your help! Just trying to understand how in the world does it run with begin begin at the top of the code? and yes it does works! like you said. YOUR THE MAN!
1 Like
I am glad it is working for you now!
The BEGIN
command only specifies the beginning of a loop. Your code can have more than one loop in it if you want. For example, the way that code is working now is by having a while loop inside of your main loop. The first BEGIN
specifies the start of your main loop (where your code will return to when it reaches the end of your sequence), and the second BEGIN
specifies the start of the while loop.
Brandon
ThankYou
I do have one more thing I have a midi player that I would like to add in the sketch to give a voice to my joseph talking head. Do you have any thoughts on how to incorporate into the sketch to be timed with the mouth movement?
The Maestro cannot deal with MIDI signals directly. Do you have a specific device in mind to play the sound? Depending on the device, it might be possible to trigger it with a Maestro channel.
Brandon
Hi
I have a Adafruit wave shield for Arduino here that I had hoped to use but
If I need to get something else I will
I am not very familiar with the Adafruit Wave Shield, but it looks like it requires an Arduino, so I do not see any reason why you would not be able to communicate between the Maestro and the Arduino to trigger when the audio file is played (e.g. use the Arduino to monitor a Maestro channel configured as an output and play an audio file when it goes high).
By the way, you mentioned MIDI, but from a glancing through the documentation for that shield, it looks like it plays WAV files.
Brandon
Your right it does play wav files.
Hi again
Its been a few years and two strokes, I have been trying to understand the sketch that we worked on.
I hope you don’t mind revisiting it again. For the life of me I cant remember what the ( begin
11 get _position ) is for, is that the pin I put the pir sensor on?
Im sorry for bothering support again but Im trying to learn all of this again.
It sounds like you are referring to these lines of code at the beginning of your script:
begin
11 get_position 500 less_than while
repeat
If so, this code will wait in a while loop until the reading on channel 11 goes low. So, you should be able to connect the output pin on your PIR sensor to pin 11 on the Maestro to make it exit the while loop and continue with the rest of your script when it triggers. Please note that pin 11 on your Maestro should be configured as an input in the “Channel Settings” tab of the Maestro Control Center.
Brandon
I’m sorry that I sound like I’m kicking a dead horse but is there any where I can find a copy of a working script, I’m not doing something right an I just need to see what Im doing wrong
I am not sure if your code has changed since you posted the latest version of your code back in August of 2017, but it sounded like that version (found in this post) worked for you after you removed the first BEGIN
command.
Brandon