#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int inputPin = 2; // choose the input pin (for PIR sensor)
int val = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int ledPin = 13; // choose the pin for the LED
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(inputPin, INPUT); // declare sensor as input
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val == LOW) { // check if the input is HIGH
Serial.println(“Motion detected!”);
digitalWrite(ledPin, HIGH); // turn LED ON
// playTone(300, 160);
for(pos = 0; pos <= 180; pos += 1) {// goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
delay(150);
if (val == HIGH) {
Serial.println(“Motion ended!”);
digitalWrite(ledPin, LOW); // turn LED OFF
// playTone(0, 0);
delay(150);
}
}
Note: This turns the iris in two cycles once there’s motion.