FastLED 3.9.15
Loading...
Searching...
No Matches
pir.h
Go to the documentation of this file.
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#include "fl/time_alpha.h"
9
10#include "fl/namespace.h"
11
12
13namespace fl {
14
15// A passive infrared sensor common on amazon.
16// For best results set the PIR to maximum sensitive and minimum delay time before retrigger.
17// Instantiating this class will create a ui UIButton when
18// compiling using the FastLED web compiler.
19class Pir {
20 public:
21 Pir(int pin);
22 bool detect();
23 operator bool() { return detect(); }
24
25 private:
26
28};
29
30// An advanced PIR that incorporates time to allow for latching and transitions fx. This is useful
31// for detecting motion and the increasing the brightness in response to motion.
32// Example:
33// #define PIR_LATCH_MS 15000 // how long to keep the PIR sensor active after a trigger
34// #define PIR_RISING_TIME 1000 // how long to fade in the PIR sensor
35// #define PIR_FALLING_TIME 1000 // how long to fade out the PIR sensor
36// PirAdvanced pir(PIN_PIR, PIR_LATCH_MS, PIR_RISING_TIME, PIR_FALLING_TIME);
37// void loop() {
38// uint8_t bri = pir.transition(millis());
39// FastLED.setBrightness(bri * brightness.as<float>());
40// }
41
43public:
48 PirAdvanced(int pin,
49 uint32_t latchMs = 5000,
50 uint32_t risingTime = 1000,
51 uint32_t fallingTime = 1000,
52 const char* button_name = nullptr);
53
55 bool detect(uint32_t now);
56
62 uint8_t transition(uint32_t now);
63
65 void activate(uint32_t now) { mRamp.trigger(now); }
66
67private:
70 bool mLastState = false;
72 bool mResetState = false;
73
74};
75
76} // namespace fl
UISlider transition("Transition", 0.0f, 0.0f, 1.0f, 0.01f)
bool detect()
Definition pir.cpp:32
DigitalPin mPin
Definition pir.h:27
Pir(int pin)
Definition pir.cpp:28
void activate(uint32_t now)
Manually start the latch cycle (e.g. on startup)
Definition pir.h:65
bool mResetState
Definition pir.h:72
bool mLastState
Definition pir.h:70
TimeRamp mRamp
Definition pir.h:69
UIButton mButton
Definition pir.h:71
bool detect(uint32_t now)
Returns true if the PIR is “latched on” (within latchMs of last trigger).
Definition pir.cpp:45
PirAdvanced(int pin, uint32_t latchMs=5000, uint32_t risingTime=1000, uint32_t fallingTime=1000, const char *button_name=nullptr)
Definition pir.cpp:37
Definition pir.h:19
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16