一次淡入和淡出 LED 灯带的一部分
Fade in and out portions of an LED-strip at a time
我的代码有一些问题。我想要它做什么:
- LED 淡入 0-12
- 延迟 0.5 秒
- LED 淡入 13-26
- 延迟(x 量)
- 淡出 LED 0-12
- 延迟0.5秒(同上)
- 淡出 LED 13-26
它的作用:
- LED 淡入 0-12
- 延迟(x 量)
- 淡出 LED 0-12
- LED 淡入 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);
};
};
我错过了什么?
您的函数 rgbFadeInAndOut
将淡入然后淡出指定的 LED,然后再将控制权返回给调用函数。
如果您想要您想要的行为,只需将淡入和淡出分成两个单独的函数: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);
我的代码有一些问题。我想要它做什么:
- LED 淡入 0-12
- 延迟 0.5 秒
- LED 淡入 13-26
- 延迟(x 量)
- 淡出 LED 0-12
- 延迟0.5秒(同上)
- 淡出 LED 13-26
它的作用:
- LED 淡入 0-12
- 延迟(x 量)
- 淡出 LED 0-12
- LED 淡入 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);
};
};
我错过了什么?
您的函数 rgbFadeInAndOut
将淡入然后淡出指定的 LED,然后再将控制权返回给调用函数。
如果您想要您想要的行为,只需将淡入和淡出分成两个单独的函数: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);