0

路易吉在这里。在我的 LED 楼梯项目中添加 2 个 PIR 传感器时遇到困难。(上和下)。

两个 PIR 都连接到 Arduino Mega 板上的不同 5v PIN 和组。我正在使用 LED 阵列,因为我使用了连接不同条带的不同 PIN。(由于电缆管理,我没有使用 LED 灯条本身的数据)。

我正在编写一个简单的代码,它在有 1 个传感器时可以正常工作。

int sensor = 37;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)


#include <FastLED.h>
#define NUM_STRIPS 15
#define NUM_LEDS_PER_STRIP 20
#define NUM_LEDS NUM_LEDS_PER_STRIP * NUM_STRIPS
CRGB leds[NUM_STRIPS * NUM_LEDS_PER_STRIP];

void setup() {
  
  delay(500); // initial delay 3 seconds
  pinMode(leds, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  FastLED.addLeds<WS2812B, 26>(leds, 0, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 27>(leds, NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 28>(leds, 2 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 29>(leds, 3 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 30>(leds, 4 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 31>(leds, 5 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 32>(leds, 6 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 33>(leds, 7 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 34>(leds, 8 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 35>(leds, 9 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);

  FastLED.addLeds<WS2812B, 40>(leds, 10 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 41>(leds, 11 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 42>(leds, 12 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 45>(leds, 13 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 44>(leds, 14 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  Serial.begin(9600);        // initialize serial
}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
     for(int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB::White;
    FastLED.show();
    leds[i] = CRGB::White;
       
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
    }
  } 
  }
  
  else {
      for(int i = 0; i < NUM_LEDS; i++) {
    FastLED.show();
    leds[i] = CRGB::Black;
                // delay 200 milliseconds 
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }
}
  }

但是当我添加第二个 PIR 时,它没有按预期工作,PIN 37(DOWN)上的 PIR 在很短的时间内被触发 3 次,而 PIN 49(向上)按预期工作。

在 2 个 PIR 的代码下方:


//SENSOR1
int sensor1 = 37;              // the pin that the sensor is atteched to
int state1 = LOW;             // by default, no motion detected
int val1 = 0;                 // variable to store the sensor status (value)

//SENSOR2
int sensor2 = 49;              // the pin that the sensor is atteched to
int state2 = LOW;             // by default, no motion detected
int val2 = 0;                 // variable to store the sensor status (value)


#include <FastLED.h>
#define NUM_STRIPS 15
#define NUM_LEDS_PER_STRIP 20
#define NUM_LEDS NUM_LEDS_PER_STRIP * NUM_STRIPS
CRGB leds[NUM_STRIPS * NUM_LEDS_PER_STRIP];

void setup() {
  
  delay(500); // initial delay 3 seconds
  pinMode(leds, OUTPUT);      // initalize LED as an output
  pinMode(sensor1, INPUT);    // initialize sensor as an input
  pinMode(sensor2, INPUT);    // initialize sensor as an input
      FastLED.addLeds<WS2812B, 26>(leds, 0, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 27>(leds, NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 28>(leds, 2 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 29>(leds, 3 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 30>(leds, 4 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 31>(leds, 5 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 32>(leds, 6 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 33>(leds, 7 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 34>(leds, 8 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 35>(leds, 9 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);

  FastLED.addLeds<WS2812B, 40>(leds, 10 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 41>(leds, 11 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 42>(leds, 12 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 45>(leds, 13 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 44>(leds, 14 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  Serial.begin(9600);        // initialize serial
}

void loop(){
  val1 = digitalRead(sensor1);   // read sensor value
  if (val1 == HIGH) {           // check if the sensor is HIGH
    for(int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB::White;
    FastLED.show();
    leds[i] = CRGB::White;    
    if (state1 == LOW) {
      Serial.println("SENSORE 37!"); 
      state1 = HIGH;       // update variable state to HIGH
      delay (1500); 
    }
  } 
  }

  val2 = digitalRead(sensor2);   // read sensor value
  if (val2 == HIGH) {           // check if the sensor is HIGH
     for(int i = NUM_LEDS; i >0; i--) {
    leds[i] = CRGB::White;
    FastLED.show();
    leds[i] = CRGB::White;
       
    if (state2 == LOW) {
      Serial.println("SENSORE 49!"); 
      state2 = HIGH;       // update variable state to HIGH
      delay (1500); 
          }
  } 
  }
    
  else {
      for(int i = 0; i < NUM_LEDS; i++) {
    FastLED.show();
    leds[i] = CRGB::Black;
                // delay 200 milliseconds 
      if (state1 || state2 == HIGH ){
        Serial.println("Motion stopped!");
    }
  }
}
  }

有人可以帮我吗,因为我不确定发生了什么,而且我从几周以来一直在解决这个问题?

谢谢,如果我错过了任何重要信息,请现在告诉我

4

0 回答 0