FastLED 3.9.12
Loading...
Searching...
No Matches
pir.h
1#pragma once
2
3#include <stdint.h>
4
5#include "digital_pin.h"
6#include "fl/ptr.h"
7#include "fl/ui.h"
8
9#include "fl/namespace.h"
10
11
12namespace fl {
13
14// A passive infrared sensor common on amazon.
15// For best results set the PIR to maximum sensitive and minimum delay time before retrigger.
16// Instantiating this class will create a ui UIButton when
17// compiling using the FastLED web compiler.
18class Pir {
19 public:
20 Pir(int pin, const char* button_name = nullptr);
21 bool detect();
22 operator bool() { return detect(); }
23
24 private:
25 UIButton mButton;
26 DigitalPin mPin;
27};
28
29// An advanced PIR that incorporates time to allow for latching and transitions fx. This is useful
30// for detecting motion and the increasing the brightness in response to motion.
31// Example:
32// #define PIR_LATCH_MS 15000 // how long to keep the PIR sensor active after a trigger
33// #define PIR_RISING_TIME 1000 // how long to fade in the PIR sensor
34// #define PIR_FALLING_TIME 1000 // how long to fade out the PIR sensor
35// PirAdvanced pir(PIN_PIR, PIR_LATCH_MS, PIR_RISING_TIME, PIR_FALLING_TIME);
36// void loop() {
37// uint8_t bri = pir.transition(millis());
38// FastLED.setBrightness(bri * brightness.as<float>());
39// }
40
42 public:
43 PirAdvanced(int pin, uint32_t latchMs = 5000, uint32_t risingTime = 1000, uint32_t fallingTime = 1000);
44 bool detect(uint32_t now); // Clamps transition() to false (transition() == 0) or true (transition() > 0).
45 // When off this will be 0.
46 // When on this will be a value between 0 and 255, defined by the transition params
47 // risingTime and fallingTime which are passed into the constructor.
48 uint8_t transition(uint32_t now); // Detects on and off and also applies transitions.
49
50 // Activate the PIR sensor. This is useful for starting the PIR sensor on startup.
51 void activate(uint32_t now) { mLastTrigger = now; }
52
53 private:
54 Pir mPir;
55 uint32_t mLatchMs;
56 uint32_t mRisingTime;
57 uint32_t mFallingTime;
58 uint32_t mLastTrigger;
59 bool mLastState;
60 uint8_t mLastTransition = 0;
61};
62
63} // namespace fl
Definition pir.h:18
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16