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, const char* button_name = nullptr);
22 bool detect();
23 operator bool() { return detect(); }
24
25 private:
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
54 bool detect(uint32_t now);
55
61 uint8_t transition(uint32_t now);
62
64 void activate(uint32_t now) { mRamp.trigger(now); }
65
66private:
69 bool mLastState = false;
70};
71
72} // namespace fl
UIButton mButton
Definition pir.h:26
bool detect()
Definition pir.cpp:31
Pir(int pin, const char *button_name=nullptr)
Definition pir.cpp:27
DigitalPin mPin
Definition pir.h:27
void activate(uint32_t now)
Manually start the latch cycle (e.g. on startup)
Definition pir.h:64
PirAdvanced(int pin, uint32_t latchMs=5000, uint32_t risingTime=1000, uint32_t fallingTime=1000)
Definition pir.cpp:36
bool mLastState
Definition pir.h:69
TimeRamp mRamp
Definition pir.h:68
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:50
bool detect(uint32_t now)
Returns true if the PIR is “latched on” (within latchMs of last trigger).
Definition pir.cpp:41
Definition pir.h:19
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16