FastLED 3.9.7
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// Instantiating this class will create a ui Button when
16// compiling using the FastLED web compiler.
17class Pir {
18 public:
19 Pir(int pin);
20 bool detect();
21 operator bool() { return detect(); }
22
23 private:
24 Button mButton;
25 DigitalPin mPin;
26};
27
28// An advanced PIR that incorporates time to allow for latching and transitions fx. This is useful
29// for detecting motion and the increasing the brightness in response to motion.
30// Example:
31// #define PIR_LATCH_MS 15000 // how long to keep the PIR sensor active after a trigger
32// #define PIR_RISING_TIME 1000 // how long to fade in the PIR sensor
33// #define PIR_FALLING_TIME 1000 // how long to fade out the PIR sensor
34// PirAdvanced pir(PIN_PIR, PIR_LATCH_MS, PIR_RISING_TIME, PIR_FALLING_TIME);
35// void loop() {
36// uint8_t bri = pir.transition(millis());
37// FastLED.setBrightness(bri * brightness.as<float>());
38// }
39
41 public:
42 PirAdvanced(int pin, uint32_t latchMs = 5000, uint32_t risingTime = 1000, uint32_t fallingTime = 1000);
43 bool detect(uint32_t now); // Just detects on and off.
44 // When off this will be 0.
45 // When on this will be a value between 0 and 255, defined by the transition params
46 // risingTime and fallingTime which are passed into the constructor.
47 uint8_t transition(uint32_t now); // Detects on and off and also applies transitions.
48
49 // Activate the PIR sensor. This is useful for starting the PIR sensor on startup.
50 void activate(uint32_t now) { mLastTrigger = now; }
51
52 private:
53 Pir mPir;
54 uint32_t mLatchMs;
55 uint32_t mRisingTime;
56 uint32_t mFallingTime;
57 uint32_t mLastTrigger;
58 bool mLastState;
59 uint8_t mLastTransition = 0;
60};
61
62} // namespace fl
Definition pir.h:17
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16