一次淡入和淡出LED条带的部分

时间:2016-07-08 09:19:47

标签: arduino led

我的代码存在一些问题。我想要它做什么:

  • 淡入淡出0-12
  • 延迟0.5秒
  • 淡入淡出13-26
  • 延迟(x金额)
  • 淡出LED 0-12
  • 延迟0.5秒(与上述相同)
  • 淡出LED 13-26

它的作用:

  • 淡入淡出0-12
  • 延迟(x金额)
  • 淡出LED 0-12
  • 淡入淡出13-26
  • 延迟(x金额) - 淡出LED 13-26

这是我的代码:

#include <Adafruit_NeoPixel.h>
#define SENSORPIN 4
#define LEDPIN 13

// variables will change:
int sensorState = 0, lastState=0;         // variable for reading the       pushbutton status

int PIN = 6;
int totalLEDs = 26;
int ledFadeTime = 10;
int lightuptime = 7000;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(totalLEDs, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  pinMode(SENSORPIN, INPUT);     
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup

  Serial.begin(9600);
}

void loop() {
  // read the state of the sensor value:
  sensorState = digitalRead(SENSORPIN);

  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState == LOW) {     
    rgbFadeInAndOut(255, 255, 255, ledFadeTime,0,13);   
    delay(50);
    rgbFadeInAndOut(255, 255, 255, ledFadeTime,13,26);
  }
  else {

  }

  lastState = sensorState;

}

void rgbFadeInAndOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait, uint8_t ledStart, uint8_t ledEnd) {
 for(uint8_t b = 0; b <255; b++) {
     for(uint8_t i=ledStart; i < ledEnd; i++) {
        strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
     }
     delay(25);
     strip.show();
    //delay(wait);
  };

  delay(lightuptime);

 for(uint8_t b=255; b > 0; b--) {
     for(uint8_t i = ledStart; i < ledEnd; i++) {
        strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
        if(b==1){
           strip.setPixelColor(i, 0, 0, 0);
        }
      }
      strip.show();
      delay(wait);
  };
};

我错过了什么?

1 个答案:

答案 0 :(得分:1)

您的函数rgbFadeInAndOut将淡入,然后在将控制权返回给调用函数之前逐渐淡出指定的Leds。

如果您需要所需的行为,只需将淡入和淡出分为两个单独的功能:rgbFadeIn(...)rgbFadeOut(...)

然后你做:

rgbFadeIn(0..12);
delay(500);   // note that 0.5 seconds is 500ms not 50
rgbFadeIn(13..26);
delay(x amount)
rgbFadeOut(0..12);
delay(500);   
rgbFadeOut(13..26);