#include <Servo.h>
Servo camera;
int motion_sensor = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int echo_pin = 4;
int trig_pin = 3;
void setup() {
Serial.begin(9600);
pinMode(motion_sensor, INPUT); // declare sensor as input
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
camera.attach(9);
}
int pos = 0;
float readDistanceCM() {
digitalWrite(trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
int duration = pulseIn(echo_pin, HIGH);
return duration * 0.034 / 2;
}
void loop() {
//Motion sensor part
val = digitalRead(motion_sensor); // read input value
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
//distance sensor part
float distance = readDistanceCM();
bool isNearby = distance < 100;
////pesudo code
// AIgenWrite(isNearby);
Serial.print("Measured distance: ");
Serial.println(readDistanceCM());
delay(100);
//camera angle control
for (pos = 0; pos < 360; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
camera.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}