Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //the time we give the sensor to calibrate (10-60 secs according to the datasheet)
- int calibrationTime = 30;
- //the time when the sensor outputs a low impulse
- unsigned long lowIn; // this the correct type
- //the amount of milliseconds the sensor has to be low
- //before we assume all motion has stopped
- const unsigned long pause = 8000; // corrected type
- boolean lockLow = true;
- boolean takeLowTime;
- const int pirPin = 3; //the digital pin connected to the PIR sensor's output
- const int ledPin = 13;
- /////////////////////////////
- //SETUP
- void setup() {
- Serial.begin(9600);
- pinMode(pirPin, INPUT);
- pinMode(ledPin, OUTPUT);
- digitalWrite(pirPin, LOW); // why?
- //give the sensor some time to calibrate
- Serial.print(F("calibrating sensor "));
- for (int i = 0; i < calibrationTime; i++) {
- Serial.print(F("."));
- // delay(000); // why are you including these?
- }
- Serial.println(F(" done"));
- Serial.println(F("SENSOR ACTIVE"));
- // delay(0); // why?
- }
- ////////////////////////////
- //LOOP
- void loop() {
- unsigned long currentTime = millis();
- boolean pirState = digitalRead(pirPin);
- // read the PIR once, set the LED state once.
- digitalWrite(ledPin, !pirState); //the led visualizes the sensors output pin state
- if (pirState) {
- if (lockLow) {
- //makes sure we wait for a transition to LOW before any further output is made:
- lockLow = false;
- Serial.println(F("---"));
- Serial.print(F("motion detected at "));
- Serial.print(currentTime / 1000);
- Serial.println(F(" sec"));
- //delay(0000); // why bother with this?
- }
- takeLowTime = true;
- }
- if (!pirState) {
- if (takeLowTime) {
- lowIn = currentTime; //save the time of the transition from high to LOW
- takeLowTime = false; //make sure this is only done at the start of a LOW phase
- }
- //if the sensor is low for more than the given pause,
- //we assume that no more motion is going to happen
- if (!lockLow && (currentTime - lowIn > pause)) {
- //makes sure this block of code is only executed again after
- //a new motion sequence has been detected
- lockLow = true;
- Serial.print(F("motion ended at ")); //output
- Serial.print((currentTime - pause) / 1000);
- Serial.println(F(" sec"));
- // delay(0); //again, why?
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement