SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <Servo.h> | |
| 2 | ||
| 3 | Servo myservo; // create servo object to control a servo | |
| 4 | // a maximum of eight servo objects can be created | |
| 5 | ||
| 6 | int pos = 0; // variable to store the servo position | |
| 7 | int ledPin = 4; // choose the pin for the LED | |
| 8 | int inputPin = 2; // choose the input pin (for PIR sensor) | |
| 9 | int pirState = LOW; // we start, assuming no motion detected | |
| 10 | int val = 0; // variable for reading the pin status | |
| 11 | int sevloco = 0; | |
| 12 | int pin13 = 13; | |
| 13 | ||
| 14 | ||
| 15 | void setup() | |
| 16 | {
| |
| 17 | myservo.attach(9); // attaches the servo on pin 9 to the servo object | |
| 18 | pinMode(ledPin, OUTPUT); // declare LED as output | |
| 19 | pinMode(inputPin, INPUT); // declare sensor as input | |
| 20 | Serial.begin(9600); | |
| 21 | } | |
| 22 | ||
| 23 | ||
| 24 | void loop() | |
| 25 | {
| |
| 26 | for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 180 degrees | |
| 27 | {
| |
| 28 | if (digitalRead(inputPin) == HIGH) {
| |
| 29 | myservo.write(pos); | |
| 30 | delay(30); //wait for move | |
| 31 | } | |
| 32 | } | |
| 33 | for(pos = 90; pos < 90; pos += 1) //goes from 180 to 0 | |
| 34 | {
| |
| 35 | if (inputPin <= 0) {
| |
| 36 | myservo.write(pos); //tell servo to move | |
| 37 | delay(30); //wait | |
| 38 | } | |
| 39 | ||
| 40 | } | |
| 41 | val = digitalRead(inputPin); // read input value | |
| 42 | if (val == HIGH) { // check if the input is HIGH
| |
| 43 | if (pirState == LOW) {
| |
| 44 | // we have just turned on | |
| 45 | Serial.println("Motion detected!");
| |
| 46 | ||
| 47 | digitalWrite(ledPin, HIGH); | |
| 48 | delay(3500); | |
| 49 | // We only want to print on the output change, not state | |
| 50 | pirState = HIGH; | |
| 51 | ||
| 52 | } | |
| 53 | } else {
| |
| 54 | digitalWrite(ledPin, LOW); // turn LED OFF | |
| 55 | if (pirState == HIGH){
| |
| 56 | // we have just turned of | |
| 57 | Serial.println("Motion ended!");
| |
| 58 | ||
| 59 | // We only want to print on the output change, not state | |
| 60 | pirState = LOW; | |
| 61 | } | |
| 62 | } | |
| 63 | } |