Alors ! En remplaçant les delay() par des millis(), ça marche beaucoup mieux. Je peux passer d'une animation à l'autre presque sans problème.
Ca bloque toujours un peu avec uniforme_flashs(), sans que je sache pourquoi (mais de toute façon je n'y suis pas très attaché). Ca bloque toujours complètement avec spirale(), et pour le coup j'aimerais vraiment la garder... Pourtant il n'y a plus de delay() !
Je précise quand même qu'il n'y a plus de "code erroné" : soit le bon code est reçu par le récepteur IR, soit il ne reçoit (en apparence) rien.
Si quelqu'un a une idée miraculeuse Merci !
#include <FastLED.h>
#include <IRremote.hpp>
//=========================
//Definition pour IRremote
//=========================
const int RECV_PIN = 12;
int currentFunction = 1; // start with function1()
//=========================
//Definition pour FastLED
//=========================
// On définit les PINS sur lesquels sont connectés les bandeaux
const int ledStrip1 = 2;
const int ledStrip2 = 4;
const int ledStrip3 = 6;
const int ledStrip4 = 8;
const int ledStrip5 = 10;
// Nombre de LEDs par bandeau
const int numLEDs = 29;
#define BRIGHTNESS 10
#define speed 12
#define speed_4 1000
// Define the starting values
int hue = 0;
int frame = 0;
// Define the arrays of LED colors for each strip
CRGB leds1[numLEDs];
CRGB leds2[numLEDs];
CRGB leds3[numLEDs];
CRGB leds4[numLEDs];
CRGB leds5[numLEDs];
bool gReverseDirection = true;
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
CRGBPalette16 gPal;
// Default 55, suggested range 20-100
#define COOLING 50
// Default 120, suggested range 50-200
#define SPARKING 120
//==========================
//==========SETUP===========
//==========================
void setup() {
delay(2000); // sanity delay
//Setup pour IRremote
//=====================
Serial.begin(9600);
IrReceiver.begin(RECV_PIN, ENABLE_LED_FEEDBACK);
//Setup pour FastLED
//=====================
FastLED.addLeds<WS2812B, ledStrip1, GRB>(leds1, numLEDs);
FastLED.addLeds<WS2812B, ledStrip2, GRB>(leds2, numLEDs);
FastLED.addLeds<WS2812B, ledStrip3, GRB>(leds3, numLEDs);
FastLED.addLeds<WS2812B, ledStrip4, GRB>(leds4, numLEDs);
FastLED.addLeds<WS2812B, ledStrip5, GRB>(leds5, numLEDs);
FastLED.setBrightness( BRIGHTNESS );
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
}
void loop()
{
if (IrReceiver.decode()){
Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
delay(50);
IrReceiver.resume();
switch (IrReceiver.decodedIRData.decodedRawData) {
case 0xF30CFF00:
currentFunction = 1;
break;
case 0xE718FF00:
currentFunction = 2;
break;
case 0xA15EFF00:
currentFunction = 3;
break;
case 0xF708FF00:
currentFunction = 4;
break;
case 0xE31CFF00:
currentFunction = 5;
break;
case 0xA55AFF00:
currentFunction = 6;
break;
case 0xBD42FF00:
currentFunction = 7;
break;
case 0xAD52FF00:
currentFunction = 8;
break;
case 0xB54AFF00:
currentFunction = 9;
break;
case 0xE916FF00:
currentFunction = 10;
break;
case 0xF807FF00:
currentFunction--;
if (currentFunction < 1) {
currentFunction = 10;
}
break;
case 0xF609FF00:
currentFunction++;
if (currentFunction > 10) {
currentFunction = 1;
}
break;
}
}
// call the current function
switch (currentFunction) {
case 1:
uniforme(0, 255, 255) ;
break;
case 2:
uniforme_flashs(255, 255, 255) ;
break;
case 3:
propulsion() ;
break;
case 4:
spirale(255, 0, 0, 255, 255, 255);
break;
case 5:
rainbow() ;
break;
case 6:
gradBlancOrangeAlt() ;
break;
case 7:
gradJauneOrangeRougeAlt() ;
break;
case 8:
bizarre() ;
break;
case 9:
HautBas() ;
break;
case 10:
flickering();
break;
}
// uniforme(0, 255, 255) ; //for pattern 1 , solid color , just give rgb values
// uniforme_flashs(255, 255, 255) ;
// propulsion() ;
// spirale(255, 0, 0, 255, 255, 255);
// rainbow() ;
// gradBlancOrangeAlt() ;
// gradJauneOrangeRougeAlt() ;
// bizarre() ;
// HautBas() ;
// flickering() ;
}
//=====================================================================
//=====================================================================
//==========================COULEUR UNIFORME===========================
//=====================================================================
//=====================================================================
void uniforme(int r, int g, int b) {
unsigned long startTime = millis(); // store the current time
for (int i = 0; i < numLEDs; i++) {
leds1[i] = CRGB(r, g, b);
leds2[i] = CRGB(r, g, b);
leds3[i] = CRGB(r, g, b);
leds4[i] = CRGB(r, g, b);
leds5[i] = CRGB(r, g, b);
if (IrReceiver.isIdle()) FastLED.show();
FastLED.setBrightness(10);
// wait for 100 milliseconds to pass
while (millis() - startTime < 10) {
// do nothing
}
}
}
//=====================================================================
//=====================================================================
//==========================UNIFORME + FLASHS==========================
//=====================================================================
//=====================================================================
void uniforme_flashs(int r, int g, int b) {
for (int i = 0; i < numLEDs; i++) {
if (random(2) == 1) {
leds1[i] = CRGB(r, g, b);
leds2[i] = CRGB(r, g, b);
leds3[i] = CRGB(r, g, b);
leds4[i] = CRGB(r, g, b);
leds5[i] = CRGB(r, g, b);
if (IrReceiver.isIdle()) FastLED.show();
} else {
unsigned long startTime = millis(); // store the current time
for (int i = 0; i < numLEDs; i++) {
leds1[i] = CRGB(255, 255, 0);
leds2[i] = CRGB(255, 255, 0);
leds3[i] = CRGB(255, 255, 0);
leds4[i] = CRGB(255, 255, 0);
leds5[i] = CRGB(255, 255, 0);
if (IrReceiver.isIdle()) FastLED.show();
FastLED.setBrightness(10);
// wait for 10 milliseconds to pass
while (millis() - startTime < 10) {
// do nothing
}
}
}
unsigned long startTime = millis(); // store the current time
// wait for a random amount of time between 0 and 100 milliseconds
unsigned long delayTime = random(100);
while (millis() - startTime < delayTime) {
// do nothing
}
}
}
//======================================================================
//======================================================================
//==============================PROPULSION==============================
//======================================================================
//======================================================================
void propulsion()
{
unsigned long startTime = millis(); // store the current time
// run simulation frame, using palette colors
gPal = CRGBPalette16( CRGB::Black, CRGB::CRGB(255, 255, 0), CRGB::Orange, CRGB::Grey);
Fire2012WithPalette();
if (IrReceiver.isIdle()) FastLED.show(); // display this frame
// wait for the appropriate amount of time to pass
while (millis() - startTime < 1000 / speed) {
// do nothing
}
}
//=====================================================================
//=====================================================================
//===============================SPIRALE===============================
//=====================================================================
//=====================================================================
/*void spirale(int r, int g, int b, int r2, int g2, int b2)
{
for (int i = numLEDs; i >= 0 ; i--)
{
leds1[i] = CRGB(r, g, b);
leds2[i] = CRGB(r, g, b);
leds3[i] = CRGB(r, g, b);
leds4[i] = CRGB(r, g, b);
leds5[i] = CRGB(r, g, b);
FastLED.setBrightness(10);
if (IrReceiver.isIdle()) FastLED.show();
}
while (1) {
for (int i = numLEDs; i >= 0 ; i--)
{
leds1[i] = CRGB(r, g, b);
leds2[i] = CRGB(r, g, b);
leds3[i] = CRGB(r, g, b);
leds4[i] = CRGB(r, g, b);
leds5[i] = CRGB(r, g, b);
if (IrReceiver.isIdle()) FastLED.show();
FastLED.setBrightness(10);
delay(100);
}
for (int i = 28; i >= 0; i -= 5)
{
leds1[i] = CRGB(r2, g2, b2);
delay(100);
if (IrReceiver.isIdle()) FastLED.show();
leds2[i - 1] = CRGB(r2, g2, b2);
delay(100);
if (IrReceiver.isIdle()) FastLED.show();
leds3[i - 2] = CRGB(r2, g2, b2);
delay(100);
if (IrReceiver.isIdle()) FastLED.show();
leds4[i - 3] = CRGB(r2, g2, b2);
delay(100);
if (IrReceiver.isIdle()) FastLED.show();
leds5[i - 4] = CRGB(r2, g2, b2);
delay(100);
if (IrReceiver.isIdle()) FastLED.show();
}
}
}
*/
void spirale(int r, int g, int b, int r2, int g2, int b2)
{
unsigned long startTime = millis(); // store the current time
// Set all LEDs to the specified color
for (int i = numLEDs; i >= 0 ; i--)
{
leds1[i] = CRGB(r, g, b);
leds2[i] = CRGB(r, g, b);
leds3[i] = CRGB(r, g, b);
leds4[i] = CRGB(r, g, b);
leds5[i] = CRGB(r, g, b);
FastLED.setBrightness(10);
if (IrReceiver.isIdle()) FastLED.show();
}
// Enter an infinite loop
while (1) {
// Set all LEDs to the specified color
for (int i = numLEDs; i >= 0 ; i--)
{
leds1[i] = CRGB(r, g, b);
leds2[i] = CRGB(r, g, b);
leds3[i] = CRGB(r, g, b);
leds4[i] = CRGB(r, g, b);
leds5[i] = CRGB(r, g, b);
if (IrReceiver.isIdle()) FastLED.show();
FastLED.setBrightness(10);
// wait for 100 milliseconds to pass
while (millis() - startTime < 100) {
// do nothing
}
startTime = millis(); // store the current time
}
// Set the LEDs to the second color in a pattern
for (int i = 28; i >= 0; i -= 5)
{
leds1[i] = CRGB(r2, g2, b2);
while (millis() - startTime < 100) {
}
startTime = millis(); // store the current time
if (IrReceiver.isIdle()) FastLED.show();
leds2[i - 1] = CRGB(r2, g2, b2);
while (millis() - startTime < 100) {
}
startTime = millis(); // store the current time
if (IrReceiver.isIdle()) FastLED.show();
leds3[i - 2] = CRGB(r2, g2, b2);
while (millis() - startTime < 100) {
}
startTime = millis(); // store the current time
if (IrReceiver.isIdle()) FastLED.show();
leds4[i - 3] = CRGB(r2, g2, b2);
while (millis() - startTime < 100) {
}
startTime = millis(); // store the current time
if (IrReceiver.isIdle()) FastLED.show();
leds5[i - 4] = CRGB(r2, g2, b2);
while (millis() - startTime < 100) {
}
startTime = millis(); // store the current time
}
}
}
void Fire2012WithPalette()
{
static uint8_t heat[numLEDs];
// Step 1. Cool down every cell a little
for( int i = 0; i < numLEDs; i++) {
heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / numLEDs) + 2));
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= numLEDs - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
}
// Step 3. Randomly ignite new 'sparks' of heat near the bottom
if( random8() < SPARKING ) {
int y = random8(7);
heat[y] = qadd8( heat[y], random8(160,255) );
}
// Step 4. Map from heat cells to LED colors
for( int j = 0; j < numLEDs; j++) {
// Scale the heat value from 0-255 down to 0-240
// for best results with color palettes.
uint8_t colorindex = scale8( heat[j], 240);
CRGB color = ColorFromPalette( gPal, colorindex);
int pixelnumber;
if( gReverseDirection ) {
pixelnumber = (numLEDs-1) - j;
} else {
pixelnumber = j;
}
leds1[pixelnumber] = color;
leds2[pixelnumber] = color;
leds3[pixelnumber] = color;
leds4[pixelnumber] = color;
leds5[pixelnumber] = color;
}
}
//=====================================================================
//=====================================================================
//===============================RAINBOW===============================
//=====================================================================
//=====================================================================
void rainbow()
{
unsigned long startTime = millis(); // store the current time
// Set all LEDs to a color based on their position and the current hue value
for (int i = 0; i < numLEDs; i++) {
//leds[numLEDs - 1 - i] = CHSV(0 + (i * 60 / numLEDs), 255, 255);
//leds[numLEDs - 1 - i] = CHSV((hue + 128 + (i * 256 / numLEDs)) % 256, 200, 200);
leds1[numLEDs - 1 - i] = CHSV(hue + (i * 256 / numLEDs), 255, 200);
leds2[numLEDs - 1 - i] = CHSV(hue + (i * 256 / numLEDs), 255, 200);
leds3[numLEDs - 1 - i] = CHSV(hue + (i * 256 / numLEDs), 255, 200);
leds4[numLEDs - 1 - i] = CHSV(hue + (i * 256 / numLEDs), 255, 200);
leds5[numLEDs - 1 - i] = CHSV(hue + (i * 256 / numLEDs), 255, 200);
}
FastLED.setBrightness(10);
// Update the LED strips
if (IrReceiver.isIdle()) FastLED.show();
// wait for 10 milliseconds to pass
while (millis() - startTime < 10) {
// do nothing
}
// Increase the hue value and wrap around if necessary
hue+=2;
if (hue > 255) {
hue = 0;
}
}
//=====================================================================
//=====================================================================
//================GRADIENT ALEATOIRE BLANC-ORANGE FONCE================
//=====================================================================
//=====================================================================
void gradBlancOrangeAlt()
{
unsigned long startTime = millis(); // store the current time
// Set all LEDs to a color based on their position
for (int i = 0; i < numLEDs; i++) {
// Calculate the hue, saturation, and value for the current LED
int hue = 15;
int saturation = (i * 255 / numLEDs);
int value = 255 - i * 155 / numLEDs + 0.2 * i * (rand() % (20 + 1 - (-0)) + (-0));
// Set the color of the current LED
leds1[i] = CHSV(hue, saturation, value);
leds2[i] = CHSV(hue, saturation, value);
leds3[i] = CHSV(hue, saturation, value);
leds4[i] = CHSV(hue, saturation, value);
leds5[i] = CHSV(hue, saturation, value);
}
FastLED.setBrightness(10);
// Update the LED strips
if (IrReceiver.isIdle()) FastLED.show();
// wait for 50 milliseconds to pass
while (millis() - startTime < 50) {
// do nothing
}
}
//=====================================================================
//=====================================================================
//================GRADIENT ALEATOIRE JAUNE-ORANGE-ROUGE================
//=====================================================================
//=====================================================================
void gradJauneOrangeRougeAlt()
{
unsigned long startTime = millis(); // store the current time
// Set the hue value to a new value within the desired range
//hue = (hue + 2) % 42;
// Set all LEDs to a color based on their position and the current hue value
for (int i = 0; i < numLEDs; i++) {
hue = (hue+20) % 42 ;
leds1[numLEDs - 1 - i] = CHSV(hue + (i * 42 / numLEDs), 255, 200);
leds2[numLEDs - 1 - i] = CHSV(hue + (i * 42 / numLEDs), 255, 200);
leds3[numLEDs - 1 - i] = CHSV(hue + (i * 42 / numLEDs), 255, 200);
leds4[numLEDs - 1 - i] = CHSV(hue + (i * 42 / numLEDs), 255, 200);
leds5[numLEDs - 1 - i] = CHSV(hue + (i * 42 / numLEDs), 255, 200);
}
FastLED.setBrightness(10);
// Update the LED strips
if (IrReceiver.isIdle()) FastLED.show();
// wait for 50 milliseconds to pass
while (millis() - startTime < 100) {
// do nothing
}
}
//=====================================================================
//=====================================================================
//=============================TRUC CHELOU=============================
//=====================================================================
//=====================================================================
void bizarre()
{
unsigned long startTime = millis(); // store the current time
for (int i = 0; i < numLEDs; i++) {
// Set the hue value based on the LED's position and the current hue value
int hue = (i * 32 / numLEDs) + (frame * 4);
// Set the saturation and value based on the hue value
int sat = 255;
int val = 255;
if (hue < 64) {
// White to yellow
val = 255;
} else if (hue < 128) {
// Yellow to orange
val = 255 - (hue - 64) * 4;
} else {
// Orange to red
sat = 255 - (hue - 128) * 4;
val = 128;
}
// Set the color of the LED
leds1[numLEDs - 1 - i] = CHSV(hue, sat, val);
leds2[numLEDs - 1 - i] = CHSV(hue, sat, val);
leds3[numLEDs - 1 - i] = CHSV(hue, sat, val);
leds4[numLEDs - 1 - i] = CHSV(hue, sat, val);
leds5[numLEDs - 1 - i] = CHSV(hue, sat, val);
}
FastLED.setBrightness(10);
// Update the LED strips
if (IrReceiver.isIdle()) FastLED.show();
// Increase the frame value and wrap around if necessary
frame++;
if (frame > 63) {
frame = 0;
}
// wait for 50 milliseconds to pass
while (millis() - startTime < 100) {
// do nothing
}
}
//====================================================================
//====================================================================
//==============APPARITION PAR HAUT, DISPARITION PAR BAS==============
//====================================================================
//====================================================================
void HautBas()
{
unsigned long startTime = millis(); // store the current time
for (int i = 0; i < numLEDs-1; i++) {
// Set the i'th led to red
leds1[i] = CHSV(hue, 255, 255);
leds2[i] = CHSV(hue, 255, 255);
leds3[i] = CHSV(hue, 255, 255);
leds4[i] = CHSV(hue, 255, 255);
leds5[i] = CHSV(hue, 255, 255);
// Show the leds
FastLED.setBrightness(10);
if (IrReceiver.isIdle()) FastLED.show();
// wait for 50 milliseconds to pass
while (millis() - startTime < 30) {
// do nothing
}
hue+=5;
if (hue > 255) {
hue = 0;
}
}
startTime = millis(); // update the current time
for (int i = numLEDs - 1; i >= 0; i--) {
// Set the i'th led to black (off)
leds1[i] = CRGB::Black;
leds2[i] = CRGB::Black;
leds3[i] = CRGB::Black;
leds4[i] = CRGB::Black;
leds5[i] = CRGB::Black;
// Show the leds
if (IrReceiver.isIdle()) FastLED.show();
// wait for 50 milliseconds to pass
while (millis() - startTime < 30) {
// do nothing
}
}
}
//=====================================================================
//=====================================================================
//==========================FLICKERING EFFECT==========================
//=====================================================================
//=====================================================================
void flickering()
{
unsigned long startTime = millis(); // store the current time
// Fade the colors of all the LEDs on each strip to black
for(int i = 0; i < numLEDs; i++) {
leds1[i].fadeToBlackBy(30);
leds2[i].fadeToBlackBy(30);
leds3[i].fadeToBlackBy(30);
leds4[i].fadeToBlackBy(30);
leds5[i].fadeToBlackBy(30);
}
// Create random "flickering" effect on each strip
int flicker_led1 = random(numLEDs);
int flicker_led2 = random(numLEDs);
int flicker_led3 = random(numLEDs);
int flicker_led4 = random(numLEDs);
int flicker_led5 = random(numLEDs);
leds1[flicker_led1] = CRGB::DarkOrange;
leds2[flicker_led2] = CRGB::Yellow;
leds3[flicker_led3] = CRGB::OrangeRed;
leds4[flicker_led4] = CRGB::Red;
leds5[flicker_led5] = CRGB::DarkRed;
FastLED.setBrightness(10);
if (IrReceiver.isIdle()) FastLED.show();
while (millis() - startTime < 30) {
// do nothing
}
}
/*
0 E916FF00
1 F30CFF00
2 E718FF00
3 A15EFF00
4 F708FF00
5 E31CFF00
6 A55AFF00
7 BD42FF00
8 AD52FF00
9 B54AFF00
EQ E619FF00
ST/REPT F20DFF00
↑ F609FF00
↓ F807FF00
PREV BB44FF00
NEXT BC43FF00
VOL- EA15FF00
VOL+ B946FF00
PL/PAUSE BF40FF00
FUNC/STOP B847FF00
POWER BA45FF00
*/