/*Arduino Code Example 1: Rotate the Servo to a Specific Angle when Motion is Detected
Perhaps you have a homemade prop that when triggered, you want it to open its mouth to let out a chilling scream or extend it’s arm at guests as they pass by using a servo motor. Let’s write an Arduino sketch that rotates our servo to a specific position when the PIR sensor detects motion and then returns it to the starting point once the motion has ended.
#include <Servo.h>
int pirPin = 12; // Arduino pin the PIR sensor is connected to
int servoPin = 9; //Arduino pin the servo is connected to
int motionStatus = 0; // variable to store the PIR sensor's motion status (high or low)
int pirState = 0; // variable to track the state change
Servo scaryServo; // create servo object to control our servo
void setup() {
Serial.begin(9600); // initialize the serial monitor
pinMode(pirPin, INPUT); // set the Arduino pin that PIR sensor is connected to as an INPUT
scaryServo.attach(servoPin); // attaches the servo on pin 9 to the servo object
scaryServo.write(0); // start the servo at 0 degrees
delay(5000); // give time for the PIR sensor to calibrate (30-60secs is best)
}
void loop() {
motionStatus = digitalRead(pirPin); // read the PIR pin's current output (is it HIGH or LOW?)
// if the motion status is HIGH
if (motionStatus == HIGH) {
scaryServo.write(90); // rotate the servo to 90 degrees
if (pirState == LOW ) {
Serial.println("Motion Detected"); // print result to the serial monitor
pirState = HIGH; // update the previous state to HIGH
}
}
//or else if motion status is low
else {
scaryServo.write(0); // rotate the servo back to 0 degrees
if (pirState == HIGH) {
Serial.println ("Motion Ended"); // print result to the serial monitor
pirState = LOW; // update the previous state to LOW
}
}
}*/
/*
Arduino Code Example 2: Sweep the Servo Back and Forth Once when Motion is Detected
Rather than turning the servo horn to a specific angle, let’s make a complete sweep from 0 to 180 degrees and then back down to 0 degrees.
Servo sweeps are ideal for animating the limbs, heads, or facial features of characters and figures in animatronics. By adjusting the sweep range and speed, you can simulate natural movements such as nodding, waving, or turning.
*/
/*
#include <Servo.h>
int pirPin = 12; // Arduino pin the PIR sensor is connected to
int servoPin = 9; //Arduino pin the servo is connected to
int motionStatus = 0; // variable to store the PIR sensor's motion status (high or low)
int pirState = 0; // variable to track the state change
int pos = 0; // variable to store the servo position
Servo scaryServo; // create servo object to control our servo
void setup() {
Serial.begin(9600); // initialize the serial monitor
pinMode(pirPin, INPUT); // set the Arduino pin that PIR sensor is connected to as an INPUT
scaryServo.attach(servoPin); // attaches the servo on pin 9 to the servo object
scaryServo.write(0); // start the servo at 0 degrees
delay(5000); // give time for the PIR sensor to calibrate (30-60secs is best)
}
void loop() {
motionStatus = digitalRead(pirPin); // read the PIR pin's current output (is it HIGH or LOW?)
// if the motion status is HIGH
if (motionStatus == HIGH) {
for (pos = 0; pos <= 180; pos+=1) { // goes from 0 to 180 degrees, in 1-degree steps
scaryServo.write(pos); // rotate servo to position in variable "pos"
delay(7); // wait 7 ms to reach each position
}
for (pos = 180; pos >= 0; pos-=1) { // goes from 180 to 0 degrees, in 1-degree steps
scaryServo.write(pos); // rotate servo to position in variable "pos"
delay(7); // wait 7 ms to reach each position
}
if (pirState == LOW ) {
Serial.println("Motion Detected"); // print result to the serial monitor
pirState = HIGH; // update the previous state to HIGH
}
}
//or else if motion status is low
else {
scaryServo.write(0); // rotate the servo to 0 degrees
if (pirState == HIGH) {
Serial.println ("Motion Ended"); // print result to the serial monitor
pirState = LOW; // update the previous state to LOW
}
}
}*/
/*
Arduino Code Example 3: Servo Movement Sequence
So far, we’ve rotated our servo to a specific angle and had it perform a sweep when the PIR sensor detects motion. Now, let’s imagine that we want to animate a prop’s jaw to move up and down according to a short audio recording.
Based on our previous experience with the servo sweep, it’s probably best to put our servo movement sequence in the Option 2 area. Otherwise, the sequence may keep repeating itself if your Time Delay is set too high.
*/
#include <Servo.h>
int pirPin = 12; // Arduino pin the PIR sensor is connected to
int servoPin = 9; //Arduino pin the servo is connected to
int motionStatus = 0; // variable to store the PIR sensor's motion status (high or low)
int pirState = 0; // variable to track the state change
Servo scaryServo; // create servo object to control our servo
void setup() {
Serial.begin(9600); // initialize the serial monitor
pinMode(pirPin, INPUT); // set the Arduino pin that PIR sensor is connected to as an INPUT
scaryServo.attach(servoPin); // attaches the servo on pin 9 to the servo object
scaryServo.write(0); // start the servo at 0 degrees
delay(5000); // give time for the PIR sensor to calibrate (30-60secs is best)
}
void loop() {
motionStatus = digitalRead(pirPin); // read the PIR pin's current output (is it HIGH or LOW?)
// if the motion status is HIGH
if (motionStatus == HIGH) {
if (pirState == LOW ) {
Serial.println("Motion Detected"); // print result to the serial monitor
pirState = HIGH; // update the previous state to HIGH
scaryServo.write(90);
delay(300);
scaryServo.write(0);
delay(100);
scaryServo.write(180);
delay(450);
// keep adding angles and delays to suit your needs
}
}
//or else if motion status is low
else {
if (pirState == HIGH) {
Serial.println ("Motion Ended"); // print result to the serial monitor
pirState = LOW; // update the previous state to LOW
scaryServo.write(0); // rotate the servo to 0 degrees
}
}
}