Hola a todos, como están?
Le cuento que tengo 2 ring leds HW-159 de 7 leds cada uno. El primero quiero que este con una animación por ejemplo rainbow con una velocidad de 500ms y el segundo al mismo tiempo con (por ejemplo) theaterChaseRainbow en 900ms y no logro que funcionen bien.
Este es mi diagrama:
El código es de este mismo foro de un usuario que pidió ayuda para no usar delay();. Lo fui modificando y luego de muchas pruebas esta desprolijo, pido disculpas de antemano pero prefiero subir lo que tengo antes que nada.
#define PINforControl 3 // pin connected to the small NeoPixels strip
#define NUMPIXELS1 7 // number of LEDs on second strip
#define PINforControl2 5
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS1, PINforControl, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(NUMPIXELS1, PINforControl2, NEO_GRB + NEO_KHZ800);
unsigned long patternInterval = 20 ; // time between steps in the pattern
unsigned long lastUpdate = 0 ; // for millis() when last update occoured
unsigned long lastUpdate2 = 0 ;
unsigned long intervals [] = { 20, 20, 50, 100 } ; // speed for each pattern
const byte button = 2; // pin to connect button switch to between pin and ground
void setup() {
strip.begin(); // This initializes the NeoPixel library.
strip2.begin();
wipe(); // wipes the LED buffers
pinMode(button, INPUT_PULLUP); // change pattern button
}
void loop() {
static int pattern = 0, lastReading;
int reading = digitalRead(button);
if(lastReading == HIGH && reading == LOW){
pattern++ ; // change pattern number
if(pattern > 3) pattern = 0; // wrap round if too big
patternInterval = intervals[pattern]; // set speed for this pattern
wipe(); // clear out the buffer
delay(50); // debounce delay
}
lastReading = reading; // save for next time
if(millis() - lastUpdate > patternInterval) updatePattern(pattern);
if(millis() - lastUpdate2 > patternInterval2) updatePattern(pattern2);
}
void updatePattern(int pat){ // call the pattern currently being created
switch(pat) {
case 0:
rainbow();
rainbow2();
//colorWipe2(strip2.Color(255, 0, 0)); // red
break;
case 1:
rainbowCycle();
colorWipe2(strip2.Color(0, 200, 0)); // red
break;
case 2:
theaterChaseRainbow();
colorWipe2(strip2.Color(0, 0, 200)); // red
break;
case 3:
colorWipe(strip.Color(255, 0, 0)); // red
colorWipe2(strip2.Color(110, 110, 110)); // red
break;
}
}
void rainbow() { // modified from Adafruit example to make it a state machine
static uint16_t j=0;
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
j++;
if(j >= 256) j=0;
lastUpdate = millis(); // time for next change to the display
}
//
void rainbow2() { // modified from Adafruit example to make it a state machine
static uint16_t j=0;
for(int i=0; i<strip2.numPixels(); i++) {
strip2.setPixelColor(i, Wheel((i+j) & 255));
}
strip2.show();
j++;
if(j >= 256) j=0;
lastUpdate2 = millis(); // time for next change to the display
}
//
void rainbowCycle() { // modified from Adafruit example to make it a state machine
static uint16_t j=0;
for(int i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
j++;
if(j >= 256*5) j=0;
lastUpdate = millis(); // time for next change to the display
}
void theaterChaseRainbow() { // modified from Adafruit example to make it a state machine
static int j=0, q = 0;
static boolean on = true;
// for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
// for (int q=0; q < 3; q++) {
if(on){
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
}
}
else {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
on = !on; // toggel pixelse on or off for next time
strip.show(); // display
q++; // update the q variable
if(q >=3 ){ // if it overflows reset it and update the J variable
q=0;
j++;
if(j >= 256) j = 0;
}
lastUpdate = millis(); // time for next change to the display
}
void colorWipe(uint32_t c) { // modified from Adafruit example to make it a state machine
static int i =0;
strip.setPixelColor(i, c);
strip.show();
i++;
if(i >= strip.numPixels()){
i = 0;
wipe(); // blank out strip
}
lastUpdate = millis(); // time for next change to the display
}
void colorWipe2(uint32_t c) { // modified from Adafruit example to make it a state machine
static int i =0;
strip2.setPixelColor(i, c);
strip2.show();
i++;
if(i >= strip2.numPixels()){
i = 0;
//wipe2(); // blank out strip
}
//lastUpdate2 = millis(); // time for next change to the display
}
void wipe(){ // clear all LEDs
for(int i=0;i<strip.numPixels();i++){
strip.setPixelColor(i, strip.Color(0,0,0));
}
}
void wipe2(){ // clear all LEDs
for(int i=0;i<strip2.numPixels();i++){
strip2.setPixelColor(i, strip2.Color(0,0,0));
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
uint32_t Wheel2(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip2.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip2.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip2.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
Toda recomendación será agradecida!
Si alguien tiene un código mejor, también es bienvenido.
Gracias!