#include <Servo.h>
// constants won't change
const int MOTION_SENSOR_PIN = 12; // Arduino pin connected to motion sensor's pin
const int SERVO0_PIN = 3; // Arduino pin connected to servo motor's pin
const int SERVO1_PIN = 5;
Servo servo0; // create servo object to control a servo
Servo servo1;
// variables will change:
int angle0 = 0;
int angle1 = 0; // the current angle of servo motor
int currentMotionState; // the current state of motion sensor
void setup() {
Serial.begin(1500); // initialize serial
pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
servo0.attach(SERVO0_PIN); // attaches the servo on pin 9 to the servo object
servo0.write(angle0);
servo1.attach(SERVO1_PIN); // attaches the servo on pin 9 to the servo object
servo1.write(angle1);
currentMotionState = digitalRead(MOTION_SENSOR_PIN);
}
void loop() {
currentMotionState = digitalRead(MOTION_SENSOR_PIN);
if (currentMotionState == HIGH && angle0 <= 30 ) { // pin state change: LOW -> HIGH
angle0 ++;
servo0.write(angle0); }
else
if (currentMotionState == HIGH && angle0 >= 0 ) {
angle0 --;
servo0.write(angle0);
}
if (currentMotionState == HIGH && angle1 <= 45 ) { // pin state change: LOW -> HIGH
angle1 ++;
servo1.write(angle1); }
else
if (currentMotionState == HIGH && angle1 >= 0 ) {
angle1 --;
servo1.write(angle1);
}
}