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.
20 public:
21 PirLowLevel(int pin);
22 bool detect();
23 operator bool() { return detect(); }
24
25 private:
26
28};
29
30// An passive infrared sensor 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// Pir 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
42class Pir {
43public:
48 Pir(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
73};
74
75} // namespace fl
uint8_t transition(uint32_t now)
Returns a 0–255 ramp value: • ramps 0→255 over risingTime • holds 255 until latchMs–fallingTime • ram...
Definition pir.cpp:54
PirLowLevel mPir
Definition pir.h:68
UIButton mButton
Definition pir.h:71
TimeRamp mRamp
Definition pir.h:69
bool detect(uint32_t now)
Returns true if the PIR is “latched on” (within latchMs of last trigger).
Definition pir.cpp:45
void activate(uint32_t now)
Manually start the latch cycle (e.g. on startup)
Definition pir.h:65
bool mLastState
Definition pir.h:70
Pir(int pin, uint32_t latchMs=5000, uint32_t risingTime=1000, uint32_t fallingTime=1000, const char *button_name=nullptr)
Definition pir.cpp:37
bool detect()
Definition pir.cpp:32
PirLowLevel(int pin)
Definition pir.cpp:28
DigitalPin mPin
Definition pir.h:27
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16