我尝试在一串WS2812 LED上同步RGB条带和颜色擦拭。我需要使用淡入淡出功能来检查每种颜色的先前状态,并决定是否淡入淡出,或者在下一种颜色进入时不做任何操作。例如,红色RGB会随着红色擦除淡出。对于紫色擦拭,蓝色会淡入,但红色RGB已经点亮,将保持点亮而不是从0开始并再次淡入。
我尝试过使用case语句,else / if语句和各种运算符,但似乎仍然无法正确使用。 Arduino论坛和Adafruit论坛似乎也没有任何答案。以下是我最近的尝试。如果有人有更好的想法,我可以提供任何帮助,甚至是全新的方法!谢谢!
#include <Adafruit_NeoPixel.h>
#define PINA 7 //ws2812 strip a
#define PINB 8 //ws2812 strip b
#define REDPIN 6 //RGB red
#define GREENPIN 5 //RGB green
#define BLUEPIN 3 //RGB blue
#define FADESPEED 5 // make this higher to slow down
#define NUMPIX 26 // number of pixels
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip_a = Adafruit_NeoPixel(NUMPIX, PINA, NEO_RGB + NEO_KHZ800);
Adafruit_NeoPixel strip_b = Adafruit_NeoPixel(NUMPIX, PINB, NEO_RGB + NEO_KHZ800);
int oldRed; //previous RGB red value
int oldGreen; //previous RGB green value
int oldBlue; //previous RGB blue value
void setup() {
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
strip_a.begin();
strip_a.show(); // Initialize all pixels to 'off'
strip_b.begin();
strip_b.show(); // Initialize all pixels to 'off'
analogWrite(BLUEPIN, 0); // Initialize RGB strip to 'off'
analogWrite(GREENPIN, 0);
analogWrite(REDPIN, 0);
Serial.begin(9600);
oldRed = 0; //Set initial RGB values to 0
oldGreen = 0;
oldBlue = 0;
}
void loop()
{
colorWipe(255,0,0); //wipe to red
delay(2500);
colorWipe(0,255,0); // wipe to green
delay(2500);
colorWipe(0,0,255); // wipe to blue
delay(2500);
colorWipe(255,255,255); // wipe to white
delay(2500);
colorWipe(0,0,0); // fade to black
delay(2500);
}
void colorWipe(int r,int g, int b){
for(int x = 0; x < NUMPIX; x++){ //incriment counter
strip_a.setPixelColor(x, r,g,b); //turn on pixel
strip_b.setPixelColor(x, r,g,b);
Serial.println(x);
if (r==0 && oldRed==255){ // if new value is 0 and old value is 255
int newRed = 255-(x*(255/NUMPIX)); // fade out in as many increments as there are pixels
analogWrite(REDPIN,newRed);
if(x = NUMPIX){ // if all cycles are complete
oldRed = 0; //reset old value to 0
analogWrite(REDPIN, 0); //make sure lights are completly off
}
}
if(r==255 && oldRed==0){
int newRed = x*(255/NUMPIX);
analogWrite(REDPIN, newRed);
if(x = NUMPIX) {
oldRed = 255;
analogWrite(REDPIN, 255);
}
}
/* if (r > oldRed){ // new value greater than old (fade on)
int newRed = x*(; // add another incriment to new red value
analogWrite(REDPIN,newRed);
oldRed=newRed; //reset old value to 255
}
if (r < oldRed); { // new value less than old (fade off)
int newRed=255-(x*10); // subtract another incriment from new red value
analogWrite(REDPIN, newRed); //fade red out
oldRed=newRed; //reset old value to 0
}
*/
/*
if (g >oldGreen){ // new value greater than old (fade on)
int newGreen = x*(255/NUMPIX); // add another incriment to new green value
analogWrite(GREENPIN,newGreen);
oldGreen=newGreen; //reset old value to 255
}
if (g < oldGreen);
{ // new value less than old (fade off)
int newGreen=255-(x*(255/NUMPIX)); // subtract another incriment from new Green value
analogWrite(GREENPIN, newGreen); //fade Green out
oldGreen=newGreen; //reset old value to 0
}
if (b > oldBlue){ // new value greater than old (fade on)
int newBlue = x*(255/NUMPIX); // add another incriment to new Blue value
analogWrite(BLUEPIN,newBlue);
oldBlue=newBlue; //reset old value to 255
}
else (b < oldBlue);
{ // new value less than old (fade off)
int newBlue=255-(x*(255/NUMPIX)); // subtract another incriment from new Blue value
analogWrite(BLUEPIN, newBlue); //fade Blue out
oldBlue=newBlue; //reset old value to 0
}
*/
delay(100);
strip_a.show();
strip_b.show();
}
}