Advertisement
zinc55

Untitled

Feb 1st, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  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.  
  12.  
  13. void setup()
  14. {
  15. myservo.attach(9); // attaches the servo on pin 9 to the servo object
  16. pinMode(ledPin, OUTPUT); // declare LED as output
  17. pinMode(inputPin, INPUT); // declare sensor as input
  18. Serial.begin(9600);
  19. }
  20.  
  21.  
  22. void loop()
  23. {
  24. for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 180 degrees
  25. {
  26. if (inputPin <= 2) {
  27. digitalWrite(ledPin, LOW);
  28. myservo.write(pos);
  29. delay(30);
  30. }
  31. else {
  32. digitalWrite(ledPin, HIGH);
  33. }
  34. }
  35. for(pos = 90; pos>=1; pos -= 1) //goes from 180 to 0
  36. {
  37. if (inputPin <= 2) {
  38. myservo.write(pos);
  39. digitalWrite(ledPin, LOW);
  40. delay(30); //wait
  41. }
  42. else {
  43. digitalWrite(ledPin, HIGH);
  44. }
  45. }
  46. val = digitalRead(inputPin); // read input value
  47. if (val == HIGH) { // check if the input is HIGH
  48. if (pirState == LOW) {
  49. delay(3500);
  50. // We only want to print on the output change, not state
  51. pirState = HIGH;
  52.  
  53. }
  54. } else {
  55. //digitalWrite(ledPin, LOW); // turn LED OFF
  56. if (pirState == HIGH){
  57. // we have just turned off
  58. Serial.println("Motion ended!");
  59. // We only want to print on the output change, not state
  60. pirState = LOW;
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement