Multi-led fading without delay ( with FastSPI2 library)

Hello,

I am stacked on a quite simple thing and I am little confused what is wrong with my code….
I am using FastSPI2 library and I am driving 100rgb leds with WS2801 chips.
Now I want to create some math function which will be creating few independent fading numbers used in fill_solid object.
In attached code there are just 2 fades.
The very strange thing is that this is working only when i put delay() there in the first fading part (Brightness fade A )… Without it it does not work.
Also the time 1 and time2 only works once - in the first condition , but after that the speed of fading is same…
My idea is to setup the speed of independent fading with the time1, time2 etc… values.

const unsigned long time1 = 50; 
const unsigned long time2 = 200;
unsigned long last1 = 0;
unsigned long last2 = 0;

// BRIGHTNESS FADE - A

 if  ( (millis() - last1) >= time1) {            
      if ( a < 255 && smer1 == 0) {
         a++; 
         delay(1);    
          }
         else { smer1 = 1;}  
      if ( a > 0 && smer1 == 1) {
          a--;  
          delay(1);  
         }
         else { smer1 = 0;}
        last1 = millis();
         }      
 // BRIGHTNESS FADE - B 
if ( (millis() - last2) >= time2) {
        
    if ( b < 255 && smer2 == 0) {
        b++;
        }
        else { smer2 = 1;} 
    if ( b > 0 && smer2 == 1) {
        b--;
        }
        else { smer2 = 0;} 
       last2 = millis(); 
       }
…. 
fill_solid( &(leds[0]), 7 , CHSV( 100, 255, a) );   // the a is for brightness
fill_solid( &(leds[7]), 2 , CHSV( 200, 255, b) );
LEDS.show();

Please do you have any suggestion what is wrong? Thanks a lot !!!

Where are you defining last1/last2? If they're inside your loop function, you need to make them static (e.g. static unsigned long last1, static unsigned long last2).

Also - when you say "without it, it does not work" - what doesn't work? The leds don't light up at all? or they light up wrong? or your fade doesn't happen?

dgarcia42: thanks for notes. I have moved quite far in solving my problem-but I have found some new… :sweat_smile:
My plan is to have at least 17 fading led blocks that will shine from time to time and they will stop at brightness peaks for a moment. Now I am just at testing phase with three RGB leds, WS2801, connected to Teensy2.0++
( final version 100rgbled with ws2801, Teensy 3.0 )
here is the complete test code:

#include <FastSPI_LED2.h>
#define NUM_LEDS 3  // will be 100
CRGB leds[NUM_LEDS];

// var for time operations
unsigned long lastTime[3] = {0,0,0}; 
unsigned long previousTime[3] = {0,0,0}; 
unsigned long stopTime[3] = {0,0,0};
unsigned long threshold[3] = {5, 30, 50};

// var for brightness
int jas0 = 0;
int jas1 = 0;
int jas2 = 0;

//tool var 
int smer1 = 0;
byte i1 = 0;
byte i2 = 0;

void setup()
{ 
LEDS.addLeds<WS2801>(leds, NUM_LEDS);
}
void loop()
{
if (millis() - lastTime[0] > 3000)   
   {     
   if (millis() - previousTime[0] > threshold[0])  {  
         previousTime[0] = millis(); 
         if ( jas0 < 255 && smer1 == 0) {
         jas0++;         
         } 
         else { smer1 = 1;}
     if (jas0 == 255 && smer1 == 0) {   
         stopTime[0] = millis(); 
         }  
     if ( jas0 > 0 && smer1 == 1 && (millis() - stopTime[0]) > 5000) {
        jas0--;       
        }
        else { smer1 = 0;}
     if (jas0 == 0) {
        lastTime[0] = millis();
        } 
     }
   }    
         

// LED 1..array 1...............................................

if (millis() - lastTime[1] > 5000) 
   {     
    if (millis() - previousTime[1] > threshold[1])     //
       {       
       previousTime[1] = millis();      
       if ( i1 >= 0 && i1 < 250) {       
       i1++;  // auto wraps after 255 to 0 as it is a byte 
       jas1 = abs(-127+127*cos(4*PI/500*i1));   
       jas1 = constrain (jas1,0,255);

          }
         }
    if ( i1 >= 250)
       {
       lastTime[1] = millis();
       i1 = 0;
       }
    }
 // LED 2..array 2...............................................

if (millis() - lastTime[2] > 7000) 
   {     
    if (millis() - previousTime[2] > threshold[2])     //
       {       
       previousTime[2] = millis();      
       if ( i2 >= 0 && i2 < 250) {       
       i2++;  // auto wraps after 255 to 0 as it is a byte 
       jas2 = abs(-127+127*cos(4*PI/500*i2)); 
       jas2 = constrain (jas2,0,255);
          }
         }
    if ( i2 >= 250)
       {
       lastTime[2] = millis();
       i2 = 0;
       }
    }

fill_solid( &(leds[0]), 1 , CHSV( 100, 200, jas0) );   // i know it is silly to use with one led, but in final  ver. it will be much more
fill_solid( &(leds[1]), 1 , CHSV( 200, 255, jas1) ); 
fill_solid( &(leds[2]), 1 , CHSV( 255, 255, jas2) ); 
LEDS.show();  

Serial.print ( "jas0:");
Serial.println ( jas0);
Serial.print ( "jas1:");
Serial.println ( jas1);
Serial.print ( "jas2:");
Serial.println ( jas2);
Serial.print ( "millis:" );
Serial.println (  millis() );
 }

NOW : math functions for the fading is quite ok. I checked them in Serialmonitor and the numbers are runnig as I expect.
(There are two types of fading - first one stops at peak for a while and then fades down. Other is some sort of cosine function…)
What si really strange now is instability:

  1. Flickering - sometimes the fading is clear, sometimes it start flicker so much =(
  2. the whole code is working ONLY if I use Serial.print object with mills() data sending to monitor. If I disable them then no LEDs starts to shine. Just dark…
  3. sometimes when I see in Serial monitor correct data for brightness ( jas1, jas2, jas3) the the LEDs does not shine as these data…

I have checked the hardware and it is working well with some other codes (not with FastSPI2).
I will check if this will work with Teensy3.0…
Should this be caused by combination of FastSPI2 and Teensy2.0 ?
It is quite strange behavior. Please do you have any idea what is wrong? Thank you very much!!!

I

After I pushed the FastLED 2 release, I found out that some WS2801 chips have a longer reset time required - otherwise, if you send data too quickly to them, they don't get to latch in their data and basically just do nothing. (It was originally set at 24µs from the first data sheets I saw, but then I started seeing data sheets saying that it needed 500µs, and I had a user manage to get WS2801's that was closer to 900µs).

I haven't put up a new release that fixes this (yet) - but if you want to fix it without having a delay(1) in place you can go into the file chipsets.h in the FastLED folder, around line 96, and change it from "CMinWait<500> mWaitDelay;" to "CMinWait< 1000 > mWaitDelay;"

https://github.com/FastLED/FastLED/blob/master/chipsets.h#L96

shows the fixed change.

GREAT!!!

Thanks a lot Mr.Garcia! Now its working well. Actually I had there even 24… So now it looks much more stable with 1000.
I will check it deeply in more complicated situations, but first impression is WOW XD
Thanks again!!!

Hello again,
...after few weeks of testing the system ( teensy 3.0, 100 RGB led with ws2801, FastSPI2 library) I have noticed one problem :
sometimes unfortunately happens that shines only first 50leds of the strip and the rest is dark. After few minutes of this unwanted behavior the rest 50leds starts also shining and then the system is mostly working well. It is quit unpredictable.
Sometimes happens also some little flickering at the "dark part". After flickering is gone than the system is stable and work hours and hours without problem.
But this starting period is very strange, and sometimes it takes even 20minutes to get the system working well.

I think that this is probably not caused by hardware issue, because after this dark/flickering period the system is working stable.

This behavior happens even with the simpliest code where I just set all 100 leds to full brightness.

Please Mr.Garcia, can you help me what could be improved to solve this?
Thanks a lot!!!