NeoPixel Code

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
crusaderabbit
 
Posts: 122
Joined: Wed Apr 13, 2016 1:55 pm

NeoPixel Code

Post by crusaderabbit »

Hi, I am trying to use the following code to make neo-pixels fade up from half-bright to full-bright, stay full-bright for 5 seconds, then fade back down to half-bright:

Code: Select all

   for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      strip_Flower.setPixelColor(w,red,green,blue);
      strip_Flower.setPixelColor(x,red,green,blue);
      
      previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <5);

      }

      strip_Flower.setPixelColor(v,255,255,255);
      strip_Flower.setPixelColor(w,255,255,255);
      strip_Flower.setPixelColor(x,255,255,255);

      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
    


    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
       for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      strip_Flower.setPixelColor(w,red,green,blue);
      strip_Flower.setPixelColor(x,red,green,blue);
      
       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <5);

Everything works great. The fade is good, except that the fade up repeats 4 times. The stay-bright-for-5-seconds does just that with no repeat. The the fade back down repeats 4 times again.
Originally, I had the strip.show() outside the do loop, but changing that had no effect. It does exactly the same thing. It is doing what I want it to do except for the repeat on the fade up and fade down, which happen to be in the for loop. Can anyone splain wass goin on? Thanks.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixel Code

Post by adafruit_support_bill »

It' is difficult to diagnose code when you only post a fragment of it. Please post the complete code.

User avatar
crusaderabbit
 
Posts: 122
Joined: Wed Apr 13, 2016 1:55 pm

Re: NeoPixel Code

Post by crusaderabbit »

Hi, Thanks for looking at my code. I originally sent only a portion of the code as I was hoping the problem was obvious and I could spare you the gory details. I should know better as the devil is in the details, isn't it.

The circuit is a simple string of 10 neo-pixels connected to pin 6 of a Flora. They go behind some flowers; hence, the flower names. The color of every neo-pixel is white. I want the entire string to be constantly lit to half brightness. Then I want to randomly choose no more than 5 neo-pixels to fade up to full brightness, remain there for 5 seconds, then fade back down to half brightness. Once this is done I want to randomly choose another group of no more than 5 neo-pixels to do the same as the first group. Fade from half to full brightness. Stay at full for 5 seconds. Fade back down to half. Then looping back to the beginning.

I have been learning my code from your tutorials, Multi-Tasking The Arduino-Part 1 https://learn.adafruit.com/multi-taskin ... 1?view=all , and Part 3 https://learn.adafruit.com/multi-taskin ... 3?view=all .
I have gone from using delay() commands to using timing with millis(). I have learned how to use a switch statement and how to create functions all in the past week. I, ultimately, intend to replace the fade algorithm with a fade function but that is not the case in this code.

You might wonder why I use (32,32,32) for my half-bright white r,g,b instead of (127,127,127). I refer you to the table in your tutorial on Led Tricks and Gamma correction https://learn.adafruit.com/led-tricks-g ... n?view=all .


So there are alot of random variables in the code. First, I randomly choose the number of neo-pixels to bring up to full brightness. Then I set up a switch stmnt related to the random number chosen, either switch(m) or switch(n) in my code. I randomly choose the number of each neo-pixel to be lit and use a do while loop to ensure that the same neo-pixel is not chosen twice with the group. Variables a-e are related with switch(m). Variables v-z are related to switch(n). I think this is the most complicated part of the program.

After I create the random variables, I use a switch statement, either switch(m) or switch(n), to determine which case to use based on the random number of neo-pixels chosen to be lit.

I use a for loop to accomplish the fade.

I use 1000 TotalSteps to go through the Fade cycle.

I nest another for loop inside the first to initialize all the neo-pixels in the strip to half-bright white.

I use the algorithm presented in the Multi-Tasking tutorial Part 3 to obtain the r,g,b values for the fade, half-bright (32,32,32) being Color1, and full-bright (255,255,255) being Color2. Then I use the random variable numbers generated for the a-e group( switch(m)), or the v-z group(switch(n)) to initialize the neo-pixels with the calculated r,g,b values.

I then light the neo-pixel strip using strip_Flower.show(). I do this inside a do while loop that counts 5 milliseconds before performing the next iteration of the fade cycle.


As I said earlier, everything works great. The neo-pixels are randomly chosen correctly. The switch statements execute the correct case. The lights fade up nicely, except that they do it 4 times. The neo-pixels stay lit at full-bright for the correct amount of time, 5 seconds, and they do it only once as they are supposed to. They then fade back down to half-bright, once again doing it 4 times.

Everything in the program seems to perform correctly except for the neo-pixels that are initialized by the fade algorithm, and there it repeats only 4 times. If it was a problem with the for loop I would expect it to repeat 1000 times. I hope you can figure it out. Thanks.

Code: Select all


#include <Adafruit_NeoPixel.h>


const int NEOPIN_Flower = 6;
const int colorInterval = 4;

int NUMNEOPIXEL_Flower = 10;

int TotalSteps = 1000;
int IndexFL = 0;

float red = 0;  //  I use floats to avoid truncating the variables
float green = 0;
float blue = 0;



unsigned long currentMillis_Color = 0;
unsigned long previousMillis_Color = 0;
unsigned long previousMillis_Flower = 0;
unsigned long currentMillis_Flower = 0;

Adafruit_NeoPixel strip_Flower = Adafruit_NeoPixel(NUMNEOPIXEL_Flower, NEOPIN_Flower, NEO_RGB + 
NEO_KHZ800);


void setup()
{   
  strip_Flower.begin();
}


void loop()
{
  int i=0;

  int m=0;

  int a=9;   // I originally envisioned a situation where if all the variables were zero it might create an endless loop in my code, but I'm not sure.
  int b=8;
  int c=7;
  int d=6;
  int e=5;


  int n=0;
  
  int v=4;
  int w=3;
  int x=2;
  int y=1;
  int z=0;

  int IndexFL = 0;
  m =random(0,5);

  switch(m)
  {
     case 0:

         a =random(0,10);

      
      for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(a,red,green,blue);
      

       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
      }
     

      strip_Flower.setPixelColor(a,255,255,255);

      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
   


    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
       for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(a,red,green,blue);
    

       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
    }   
     
      
      break;

     case 1:

         a =random(0,10);

            do
             { b = random(0,10);
             }
            while (b==a);

      
      for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(a,red,green,blue);
      strip_Flower.setPixelColor(b,red,green,blue);
      
   
       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
      }
     

      strip_Flower.setPixelColor(a,255,255,255);
      strip_Flower.setPixelColor(b,255,255,255);

      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
    

    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
      for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(a,red,green,blue);
      strip_Flower.setPixelColor(b,red,green,blue);
    
      
       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
    }
      
      
      break;

     case 2:

     a =random(0,10);

            do
             { b = random(0,10);
             }
            while (b==a);

            do
             {c = random(0,10);
             }
            while (c==a||c==b);
      
      for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(a,red,green,blue);
      strip_Flower.setPixelColor(b,red,green,blue);
      strip_Flower.setPixelColor(c,red,green,blue);
      

       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
      }
      

      strip_Flower.setPixelColor(a,255,255,255);
      strip_Flower.setPixelColor(b,255,255,255);
      strip_Flower.setPixelColor(c,255,255,255);

      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
    


    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
       for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }
       
      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(a,red,green,blue);
      strip_Flower.setPixelColor(b,red,green,blue);
      strip_Flower.setPixelColor(c,red,green,blue);
   

       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
    }
      
      break;

       case 3:

         a =random(0,10);

            do
             { b = random(0,10);
             }
            while (b==a);

            do
             {c = random(0,10);
             }
            while (c==a||c==b);

            do
             {d = random(0,10);
             }
            while (d==a||d==b||d==c);

      
      for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(a,red,green,blue);
      strip_Flower.setPixelColor(b,red,green,blue);
      strip_Flower.setPixelColor(c,red,green,blue);
      strip_Flower.setPixelColor(d,red,green,blue);
      
      
       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
      }

      strip_Flower.setPixelColor(a,255,255,255);
      strip_Flower.setPixelColor(b,255,255,255);
      strip_Flower.setPixelColor(c,255,255,255);
      strip_Flower.setPixelColor(d,255,255,255);
      
      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
    


    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
       for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(a,red,green,blue);
      strip_Flower.setPixelColor(b,red,green,blue);
      strip_Flower.setPixelColor(c,red,green,blue);
      strip_Flower.setPixelColor(d,red,green,blue);
    
      
       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
    }
      
      
      break;


      case 4:

         a =random(0,10);

            do
             { b = random(0,10);
             }
            while (b==a);

            do
             {c = random(0,10);
             }
            while (c==a||c==b);

            do
             {d = random(0,10);
             }
            while (d==a||d==b||d==c);

            do
             {e = random(0,10);
             }
            while (e==a||e==b||e==c||e==d);

      
      for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(a,red,green,blue);
      strip_Flower.setPixelColor(b,red,green,blue);
      strip_Flower.setPixelColor(c,red,green,blue);
      strip_Flower.setPixelColor(d,red,green,blue);
      strip_Flower.setPixelColor(e,red,green,blue);
      

      previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
      }
      

      strip_Flower.setPixelColor(a,255,255,255);
      strip_Flower.setPixelColor(b,255,255,255);
      strip_Flower.setPixelColor(c,255,255,255);
      strip_Flower.setPixelColor(d,255,255,255);
      strip_Flower.setPixelColor(e,255,255,255);

      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
    


    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
       for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(a,red,green,blue);
      strip_Flower.setPixelColor(b,red,green,blue);
      strip_Flower.setPixelColor(c,red,green,blue);
      strip_Flower.setPixelColor(d,red,green,blue);
      strip_Flower.setPixelColor(e,red,green,blue);
    

       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
    }
      
    
      break;
  } 


  n = random(0,5);

  switch(n)
  {
    case 0:

         v =random(0,10);
      
      for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      
     
       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
      }

      

      strip_Flower.setPixelColor(v,255,255,255);
     

      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
   


    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
       for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
    
      
      previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
    }
      
      
      break;

      case 1:

         v =random(0,10);

            do
             { w = random(0,10);
             }
            while (w==v);

           
      
      for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      strip_Flower.setPixelColor(w,red,green,blue);
      
      
      previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
      }

      

      strip_Flower.setPixelColor(v,255,255,255);
      strip_Flower.setPixelColor(w,255,255,255);

      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
    


    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
       for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }
      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      strip_Flower.setPixelColor(w,red,green,blue);
    
      
       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
    }
      
      
      break;

    case 2:

         v =random(0,10);

            do
             { w = random(0,10);
             }
            while (w==v);

            do
             {x = random(0,10);
             }
            while (x==v||x==w);

      
      for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      strip_Flower.setPixelColor(w,red,green,blue);
      strip_Flower.setPixelColor(x,red,green,blue);
      
      
      previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
      }

      

      strip_Flower.setPixelColor(v,255,255,255);
      strip_Flower.setPixelColor(w,255,255,255);
      strip_Flower.setPixelColor(x,255,255,255);

      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
    


    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
       for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      strip_Flower.setPixelColor(w,red,green,blue);
      strip_Flower.setPixelColor(x,red,green,blue);
    
      
       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
    }
      
      
      break;    

    case 3:

         v =random(0,10);

            do
             { w = random(0,10);
             }
            while (w==v);

            do
             {x = random(0,10);
             }
            while (x==v||x==w);

            do
             {y = random(0,10);
             }
            while (y==v||y==w||y==x);

      
      for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      strip_Flower.setPixelColor(w,red,green,blue);
      strip_Flower.setPixelColor(x,red,green,blue);
      strip_Flower.setPixelColor(y,red,green,blue);
      
     
       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
      }
      

      strip_Flower.setPixelColor(v,255,255,255);
      strip_Flower.setPixelColor(w,255,255,255);
      strip_Flower.setPixelColor(x,255,255,255);
      strip_Flower.setPixelColor(y,255,255,255);
      

      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
    


    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
      for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }
       
      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      strip_Flower.setPixelColor(w,red,green,blue);
      strip_Flower.setPixelColor(x,red,green,blue);
      strip_Flower.setPixelColor(y,red,green,blue);
    
     
       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
    }  
      
      break;
    
    case 4:

         v =random(0,10);

            do
             { w = random(0,10);
             }
            while (w==v);

            do
             {x = random(0,10);
             }
            while (x==v||x==w);

            do
             {y = random(0,10);
             }
            while (y==v||y==w||y==x);

            do
             {z = random(0,10);
             }
            while (z==v||z==w||z==x||z==y);

      
      for(IndexFL = 0; IndexFL<TotalSteps; IndexFL ++)
      {
          for(i=0; i< NUMNEOPIXEL_Flower; i++)
          {
             strip_Flower.setPixelColor(i,32,32,32);
          }

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      strip_Flower.setPixelColor(w,red,green,blue);
      strip_Flower.setPixelColor(x,red,green,blue);
      strip_Flower.setPixelColor(y,red,green,blue);
      strip_Flower.setPixelColor(z,red,green,blue);
      

       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
      }

      strip_Flower.setPixelColor(v,255,255,255);
      strip_Flower.setPixelColor(w,255,255,255);
      strip_Flower.setPixelColor(x,255,255,255);
      strip_Flower.setPixelColor(y,255,255,255);
      strip_Flower.setPixelColor(z,255,255,255);

      strip_Flower.show();
     
            currentMillis_Flower = 0;
            previousMillis_Flower = millis();
  
            do
              {
                
                currentMillis_Flower = (millis() - previousMillis_Flower);
              }
            while (currentMillis_Flower <5000);
    


    for(IndexFL=999; IndexFL >= 0; IndexFL --)
    {
       for(i=0; i< NUMNEOPIXEL_Flower; i++)
       {
         strip_Flower.setPixelColor(i,32,32,32);
       }
    

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      strip_Flower.setPixelColor(v,red,green,blue);
      strip_Flower.setPixelColor(w,red,green,blue);
      strip_Flower.setPixelColor(x,red,green,blue);
      strip_Flower.setPixelColor(y,red,green,blue);
      strip_Flower.setPixelColor(z,red,green,blue);
    

       previousMillis_Flower = millis();
      do
        {
          strip_Flower.show();
        }
       while ((millis() - previousMillis_Flower) <10);
    }
      
      
      break;
  }

}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixel Code

Post by adafruit_support_bill »

The missing bit of information from your first post was the value of TotalSteps (1000).

You are doing all 16-bit integer math in the fade calculation. With such a large value for TotalSteps, you are probably running into integer overflow problems.

Code: Select all

      red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      green = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;

      blue = ((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;
In reality, there is not much point in having that many steps. The RGB resolution of the Neopixels is only 0-255. And you are starting at 32.

User avatar
crusaderabbit
 
Posts: 122
Joined: Wed Apr 13, 2016 1:55 pm

Re: NeoPixel Code

Post by crusaderabbit »

Yes. Thank you. I changed TotalSteps and it worked immediately.
I knew 1000 steps was an exaggeration, but I thought it might make for a nice, smooth fade, which it did...4 times.
I first changed TotalSteps to 223, 255 -32, even that had a slight residual repeat. I finally settled at 100 steps and it looks beautiful.
Now, the question is:do I need all that 16 bit math? It would save space in memory to use integer arithmetic I'm pretty sure. I think I would need to change the algorithm, though, to reduce the RGB equally so as not to effect the color. Anyway, thanks very much.

User avatar
kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: NeoPixel Code

Post by kcl1s »

First let me say I am just a hobbyist also. I have just been learning a while longer than you.

It is great you are trying out all functions and learning what you can do with them. The next step in your journey will be learning where you need fancy code and when just plain and simple works just as well. You talk about multitasking and millis() but your code just does not need it as you are really just doing one thing at a time.

I took the description of your project and coded what I think you want. There is no right or wrong way to code but you should try for efficiency. No need for any math other than ++ or -- on the loops for your project. I introduced an array to hold flag data for each pixel that is updated with the random set each time through loop. The 5 or less random gets handled automatically by the fact that each pixel can be flagged more than one time on each pass through the loop.

Anyway take a look at the following. Maybe you can get some value out of it. Good luck with this project and future ones.

Code: Select all

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 10
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int pixChoice[NUMPIXELS];   //an array to hold a flag to see
                            //if a pixel gets brightend for a loop
void setup()  {
  pixels.begin();
  pixels.show();
  //set all pixels to 32 brightness
  for (int i = 0; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, 32, 32, 32);
  }
  pixels.show();
}

void loop() {
  //set all pixel flags to 0
  for (int i = 0; i < NUMPIXELS; i++) {   
    pixChoice[i] = 0;
  }

  //set up to 5 pixel flags to 1
  for (int x = 0; x < 5; x++) {           
    pixChoice[random(NUMPIXELS)] = 1;
  }
  //code to brighten flagged pixels to full brightness
  //wait 5 sec and bring them back to 32 brightness
  for (int y = 33; y < 255; y++) {
    for (int z = 0; z < NUMPIXELS; z++) {
      if (pixChoice[z] == 1) {
        pixels.setPixelColor(z, y, y, y);
      }
    }
    pixels.show();
    delay(20);
  }
  delay(5000);
  for (int y = 255; y > 31; y--) {
    for (int z = 0; z < NUMPIXELS; z++) {
      if (pixChoice[z] == 1) {
        pixels.setPixelColor(z, y, y, y);
      }
    }
    pixels.show();
    delay(20);
  }
}
Keith
Last edited by kcl1s on Wed Jun 20, 2018 6:50 am, edited 1 time in total.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixel Code

Post by adafruit_support_bill »

Now, the question is:do I need all that 16 bit math? It would save space in memory to use integer arithmetic I'm pretty sure.
16 bit math is integer arithmetic - and it is quite efficient. The Arduino, supports 3 sizes of integer: 16 bits is the default size. There are also 8 bit and 32 bit integers. All 3 types come in signed and unsigned varieties.

For a wider range of values (or rather approximations of values) you can use floating point. Although the language supports 'single' and 'double' precision, on most Arduinos, they are really all single precision (32 bits). Floating point is quite a bit less efficient than integer math. I only use it when absolutely necessary.

In this application, the end result (RGB values) are all unsigned 8 bit values (0-255). For intermediate calculations, you need a bit of headroom, so 16 bit unsigned integers are a good choice. You did experience some overflow using a very large number of steps. That could be avoided by using 32-bit math for the calculations - at some cost in efficiency. But you still would not get more than 255 effective steps at the output - because that is the limit of the output resolution of the neopixels.

User avatar
crusaderabbit
 
Posts: 122
Joined: Wed Apr 13, 2016 1:55 pm

Re: NeoPixel Code

Post by crusaderabbit »

I really appreciate your explaining this to me.

I am a little confused, though. I don't know if you noticed in my code that I declared red, green, and blue as float variables.

When you said that I had all that 16 bit math causing an overflow, I thought it was because I had 1000 steps and was using float variables. I used float because even though TotalSteps and IndexFL are integer values, the division in the algorithm can create a decimal number. Now after you said that integer math was 16 bit math I am confused.

Firstly, each time the program goes through the for loop that sets the RGB values for the fade, does it not write over the value previously stored in those variables? I don't understand where the backlog occurs.

Also, as I am essentially using a fade algorithm to accomplish a dim, can you speak to the scanner algorithm in the Multi-Tasking The Arduino Part3,

Code: Select all

// Calculate 50% dimmed version of a color (used by ScannerUpdate)
    uint32_t DimColor(uint32_t color)
    {
        // Shift R, G and B components one bit to the right
        uint32_t dimColor = Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
        return dimColor;
    }

I think that the >>1 command is shifting the color 1 byte to the right and thus lowering the brightness, but I'm not sure and I don't really understand what that means.

And lastly, about color, you may have noticed that I put the actual RGB values into my fade algorithm. Originally I tried using uint-32t Color1( 32, 32, 32); , but when the program ran my neo-pixels lit blue not half-white. So I went with setting the RGB manually.

As I mentioned earlier, a week ago I was using delay loops, so this is all very eye opening for me. It is a way to use the arduino for my projects in the way it was intended. I want to be able to code fades and dims and multi-task. I feel dumb and like a pain in the ass. Any of this that you would comment on would be appreciated. Thanks.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixel Code

Post by adafruit_support_bill »

I am a little confused, though. I don't know if you noticed in my code that I declared red, green, and blue as float variables.
That is true. But everything on the right side of the assignment operator is an integer, so by default all the computation will be performed using 16 bit integer math. The 16 bit integer result of that is converted to a float for the assignment.

Code: Select all

red =((32 * (TotalSteps-IndexFL)) + (255*IndexFL)) / TotalSteps;
That floating point value is then converted (and truncated) back into an unsigned 8 bit value for the call to setPixelColor.
can you speak to the scanner algorithm in the Multi-Tasking The Arduino Part3,
I think that the >>1 command is shifting the color 1 byte to the right and thus lowering the brightness, but I'm not sure and I don't really understand what that means.
In binary, a 1-bit shift to the right is equivalent to a divide by 2. The scanner algorithm dims the pixels exponentially relative to the distance from the center of the 'eye'.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”