Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo myservo; // create servo object to control a servo
- // a maximum of eight servo objects can be created
- int pos = 0; // variable to store the servo position
- int ledPin = 4; // choose the pin for the LED
- int inputPin = 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
- void setup()
- {
- myservo.attach(9); // attaches the servo on pin 9 to the servo object
- pinMode(ledPin, OUTPUT); // declare LED as output
- pinMode(inputPin, INPUT); // declare sensor as input
- Serial.begin(9600);
- }
- void loop()
- {
- for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 180 degrees
- {
- if (inputPin <= 2) {
- digitalWrite(ledPin, LOW);
- myservo.write(pos);
- delay(30);
- }
- else {
- digitalWrite(ledPin, HIGH);
- }
- }
- for(pos = 90; pos>=1; pos -= 1) //goes from 180 to 0
- {
- if (inputPin <= 2) {
- myservo.write(pos);
- digitalWrite(ledPin, LOW);
- delay(30); //wait
- }
- else {
- digitalWrite(ledPin, HIGH);
- }
- }
- val = digitalRead(inputPin); // read input value
- if (val == HIGH) { // check if the input is HIGH
- if (pirState == LOW) {
- delay(3500);
- // We only want to print on the output change, not state
- pirState = HIGH;
- }
- } else {
- //digitalWrite(ledPin, LOW); // turn LED OFF
- if (pirState == HIGH){
- // we have just turned off
- Serial.println("Motion ended!");
- // We only want to print on the output change, not state
- pirState = LOW;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement