Skip to content

Instantly share code, notes, and snippets.

@KyleMartinTech
Created March 22, 2018 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KyleMartinTech/0007b7d093c94b4556c09d4de82eff5c to your computer and use it in GitHub Desktop.
Save KyleMartinTech/0007b7d093c94b4556c09d4de82eff5c to your computer and use it in GitHub Desktop.
//This is the code to go along with this video: https://youtu.be/xdurnklqtGM
//Program uses a Motion sensor and sound sensor to only rotate the camera while there is someone in the room.
#include <Servo.h>
#include <arduino.h>
Servo myservo1;
Servo myservo2;
int pos1 = 0; // variable to store the servo position
int pos2 = 45; // servo 2 positions, the range of motion of this servo will be 1/2 that of the rotation servo
const int soundInput = 4;
const int motionInput = 3;
const int LED = 2;
int timer = 0;
int direction = 0;
int count = 0;
void setup() {
pinMode (soundInput, INPUT);
pinMode (motionInput, INPUT);
pinMode (LED, OUTPUT);
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches the second servo to pin 10
}
void loop() {
while(direction == 0 && pos1 < 180){
digitalWrite(LED,HIGH);
if (digitalRead(soundInput) == HIGH){
timer = 0;
}
myservo1.write(pos1);
myservo2.write(pos2);
pos1++;
pos2 = (pos1/2)+45;
while((timer<10000 || digitalRead(motionInput) == HIGH) && count < 1000){
if (digitalRead(soundInput) == HIGH){
timer = 0;
}
delay(1);
count++;
timer++;
}
count = 0;
while(timer>=10000 && digitalRead(motionInput) == LOW){
digitalWrite(LED,LOW);
if (digitalRead(soundInput) == HIGH){
timer = 0;
}
delay(1);
}
}
direction = 1;
while(direction = 1 && pos1 <= 0){
digitalWrite(LED,HIGH);
if (digitalRead(soundInput) == HIGH){
timer = 0;
}
myservo1.write(pos1);
myservo2.write(pos2);
pos1--;
pos2 = (pos1/2)+45;
while((timer<10000 || digitalRead(motionInput) == HIGH) && count < 1000){
if (digitalRead(soundInput) == HIGH){
timer = 0;
}
delay(1);
count++;
timer++;
}
count = 0;
while(timer>=10000 && digitalRead(motionInput) == LOW){
digitalWrite(LED,LOW);
if (digitalRead(soundInput) == HIGH){
timer = 0;
}
delay(1);
}
}
direction = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment