Advertisement
computermuseo

sensore pir con attivazione led/rele

May 26th, 2015
5,663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. int calibrationTime = 5;
  2.  
  3. //the time when the sensor outputs a low impulse
  4. long unsigned int lowIn;
  5.  
  6. //the amount of milliseconds the sensor has to be low
  7. //before we assume all motion has stopped
  8. long unsigned int pause = 5000;
  9.  
  10. boolean lockLow = true;
  11. boolean takeLowTime;
  12.  
  13. int pirPin = 3; //the digital pin connected to the PIR sensor's output
  14. int ledPin = 13;
  15.  
  16.  
  17. /////////////////////////////
  18. //SETUP
  19. void setup(){
  20. Serial.begin(9600);
  21. pinMode(pirPin, INPUT);
  22. pinMode(ledPin, OUTPUT);
  23. digitalWrite(pirPin, LOW);
  24.  
  25. //give the sensor some time to calibrate
  26. Serial.print("CALIBRAZIONE SENSORE ");
  27. for(int i = 0; i < calibrationTime; i++){
  28. Serial.print(".");
  29. delay(1000);
  30. }
  31. Serial.println(" fatto");
  32. Serial.println("SENSORE ATTIVO");
  33. delay(50);
  34. }
  35.  
  36. ////////////////////////////
  37. //LOOP
  38. void loop(){
  39.  
  40. if(digitalRead(pirPin) == HIGH){
  41. digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
  42. if(lockLow){
  43. //makes sure we wait for a transition to LOW before any further output is made:
  44. lockLow = false;
  45. Serial.println("---");
  46. Serial.print("movimento rilevato in ");
  47. Serial.print(millis()/1000);
  48. Serial.println(" sec");
  49. delay(50);
  50. }
  51. takeLowTime = true;
  52. }
  53.  
  54. if(digitalRead(pirPin) == LOW){
  55. digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
  56.  
  57. if(takeLowTime){
  58. lowIn = millis(); //save the time of the transition from high to LOW
  59. takeLowTime = false; //make sure this is only done at the start of a LOW phase
  60. }
  61. //if the sensor is low for more than the given pause,
  62. //we assume that no more motion is going to happen
  63. if(!lockLow && millis() - lowIn > pause){
  64. //makes sure this block of code is only executed again after
  65. //a new motion sequence has been detected
  66. lockLow = true;
  67. Serial.print("MOVIMENTO FINITO IN: "); //output
  68. Serial.print((millis() - pause)/1000);
  69. Serial.println(" sec");
  70. delay(50);
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement