Advertisement
Guest User

Untitled

a guest
May 18th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. //deodonoid democode
  2. //stadin hesalaiset
  3. #include <Servo.h>
  4. Servo myservo; //creates a servo object
  5. int pos = 0; //variable to store servo position
  6. //amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)
  7. int calibrationTime = 10;
  8. //the time when the sensor outputs a low impulse
  9. long unsigned int lowIn;
  10. //the amount of milliseconds the sensor has to be low
  11. //before we assume all motion has stopped
  12. long unsigned int pause = 5000;
  13. boolean lockLow = true;
  14. boolean takeLowTime;
  15. int pir = 13; //digital pin connected to the PIR's output
  16. int pirPos = 12; //connects to the PIR's 5V pin
  17. int count=0;
  18. void setup(){
  19. myservo.attach(4); //attaches servo to pin 4
  20. Serial.begin(9600); //begins serial communication
  21. pinMode(pir, INPUT);
  22. pinMode(pirPos, OUTPUT);
  23. digitalWrite(pirPos, HIGH);
  24. //time for calibration
  25. Serial.println("calibrating sensor ");
  26. for(int i = 0; i < calibrationTime; i++){
  27. Serial.print(calibrationTime - i);
  28. Serial.print("-");
  29. delay(1000);
  30. }
  31. Serial.println();
  32. Serial.println("done");
  33. //this waits until the PIR's output is low before ending setup
  34. while (digitalRead(pir) == HIGH) {
  35. delay(500);
  36. }
  37. Serial.print("SENSOR ACTIVE");
  38. }
  39. void loop(){
  40. if(digitalRead(pir) == HIGH && count==0){ //if the PIR output is HIGH, turn servo
  41. /*turns servo from 0 to 180 degrees and back
  42. it does this by increasing the variable "pos" by 1 every 5 milliseconds until it hits 180
  43. and setting the servo's position in degrees to "pos" every 5 milliseconds
  44. it then does it in reverse to have it go back
  45. if you want to change the amount of degrees the servo turns, change the number 180 to the number of degrees you want it to turn
  46. **/
  47. count++;
  48. for(pos = 100; pos <180 ; pos += 1) //goes from 0 to 180 degrees
  49. { //in steps of one degree
  50. myservo.write(pos); //tells servo to go to position in variable "pos"
  51. delay(5); //waits for the servo to reach the position
  52. }
  53. for(pos = 180; pos>=100; pos-=1) //goes from 180 to 0 degrees
  54. {
  55. myservo.write(pos); //to make the servo go faster, decrease the time in delays for
  56. delay(5); //to make it go slower, increase the number.
  57. }
  58. if(lockLow){
  59. //makes sure we wait for a transition to LOW before further output is made
  60. lockLow = false;
  61. Serial.print("Motion detected!");
  62. delay(50);
  63. }
  64. takeLowTime = true;
  65. }
  66. if(digitalRead(pir) == LOW){
  67. if(takeLowTime){
  68. lowIn = millis(); //this will save the time of the transition from HIGH to LOW
  69. takeLowTime = false; //make sure this is only done at the start of a LOW phase
  70. }
  71. //if the sensor is low for more than the given pause,
  72. //we can assume the motion has stopped
  73. if(!lockLow && millis() - lowIn > pause){
  74. //makes sure this block of code is only executed again after
  75. //a new motion sequence has been detected
  76. lockLow = true;
  77. Serial.print("Motion ended!"); //output
  78. delay(100);
  79. count--;
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement