c_cpp 运动激活LED灯条

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 运动激活LED灯条相关的知识,希望对你有一定的参考价值。

// This Timer.h library came from here: https://github.com/JChristensen/Timer
#include "Timer.h"

#define avoidPin 4
#define relayPin 5
#define delayMs 100
#define timeoutMs 1000 * 30

int lastValue;
bool isTimerRunning = false;

Timer mainTimer;
Timer workTimer;

void setup()
{
  Serial.begin(115200);
  
  pinMode(relayPin, OUTPUT);
  pinMode(avoidPin, INPUT);
  
  
  Serial.println("Set pins in setup mode, listening every ms: " + String(delayMs));

  mainTimer.every(delayMs, mainCallback);
}

void mainCallback() {
  int avoidVal = digitalRead(avoidPin);

  // We only care if something changes (someone walks in front of the sensor)
  if (avoidVal != lastValue) {

      // It's different, is the timer running?
      if (isTimerRunning) {
        Serial.println("Don't need to do anything, timer is running");
      }
      else
      {
        // Timer not running, is somebody there?
        if (avoidVal == LOW) {
          
          digitalWrite(relayPin, HIGH);

          // Start a timer to turn them off
          workTimer.every(timeoutMs, workCallback, 1);
          
          isTimerRunning = true;

          Serial.println("LED is on, timer started, bool set");
          
        } else {
          
          Serial.println("Looks like nobody is there, not starting timer");
          
        }
      }

      lastValue = avoidVal;
      
  }

  // If they are the same value who cares, we only care about things that change
  
}

void workCallback() {
  isTimerRunning = false;

  digitalWrite(relayPin, LOW);
  Serial.println("LED off and bool = false");
}

void loop()
{
  mainTimer.update();
  workTimer.update();
}

以上是关于c_cpp 运动激活LED灯条的主要内容,如果未能解决你的问题,请参考以下文章

从 Arduino Uno 切换到 NodeMCU esp8266 的 LED 灯条颜色更改代码不起作用

Adafruit NeoPixel 库在处理超过 7 个 LED 灯条时不起作用

在设定的时间后,使可寻址的 LED 灯条从一种模式转移到下一种模式

WS2812B的第一个led在代码进入for循环时开始点亮

WS2812B LED灯工作原理

led显示屏之家