-
-
Save efatsi/2f14b8a77ffcf95616870898ace34cec to your computer and use it in GitHub Desktop.
PIR detection library for Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pir.h" | |
Pir pir; | |
void setup() {} | |
void loop() { | |
pir.update(); | |
if (pir.movementOn) { | |
Serial.println("Movement detected!"); | |
} | |
if (pir.movementOff) { | |
Serial.println("Movement stopped."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pir.h" | |
Pir::Pir() { | |
pinMode(PIR_PIN, INPUT); | |
_resetInterval(); | |
// give the sensor a couple seconds to wake up | |
_intervalStart = millis() + 5000; | |
} | |
void Pir::update() { | |
_resetMovementVariables(); | |
_makeReading(); | |
if (millis() > _intervalStart + READING_INTERVAL) { | |
_analyzeInterval(); | |
_resetInterval(); | |
} | |
} | |
void Pir::_resetMovementVariables() { | |
movementOn = false; | |
movementOff = false; | |
} | |
void Pir::_makeReading() { | |
int pirReading = analogRead(PIR_PIN); | |
if (pirReading < _pirLow) _pirLow = pirReading; | |
if (pirReading > _pirHigh) _pirHigh = pirReading; | |
} | |
void Pir::_analyzeInterval() { | |
int difference = _pirHigh - _pirLow; | |
if (!_seesMovement) { | |
if (difference > MOVEMENT_THRESHOLD) { | |
movementOn = true; | |
_seesMovement = true; | |
_lastSustainingMeasurement = millis(); | |
} | |
} else { | |
if (difference > SUSTAIN_THRESHOLD) { | |
_lastSustainingMeasurement = millis(); | |
} else { | |
if (millis() > _lastSustainingMeasurement + SUSTAIN_BUFFER) { | |
movementOff = true; | |
_seesMovement = false; | |
} | |
} | |
} | |
} | |
void Pir::_resetInterval() { | |
_pirLow = 4096; | |
_pirHigh = 0; | |
_intervalStart = millis(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef Pir_h | |
#define Pir_h | |
#include "application.h" | |
#define PIR_PIN A0 | |
#define READING_INTERVAL 250 | |
#define SUSTAIN_BUFFER 750 | |
#define MOVEMENT_THRESHOLD 2500 | |
#define SUSTAIN_THRESHOLD 1000 | |
class Pir { | |
public: | |
Pir(); | |
void update(); | |
bool movementOn = false; | |
bool movementOff = false; | |
private: | |
void _resetMovementVariables(); | |
void _makeReading(); | |
void _analyzeInterval(); | |
void _resetInterval(); | |
long _intervalStart; | |
long _lastSustainingMeasurement; | |
int _pirLow; | |
int _pirHigh; | |
bool _seesMovement = false; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment