0

Effect neopixel
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Effect neopixel

by Andresled099 on Sun Jan 12, 2020 6:09 pm

Hello Adafruit community, I am currently making a mask with neopixel and I want to draw some figures on it. For this I wrote the following function that runs through an array leaving a tail behind it, of the same intensity, what I would like to be if the tail was faded, with my little programming knowledge I could not achieve this effect, someone could guide me ? how the code should be to achieve the faded tail. the effect that I would like to achieve is similar to that achieved in the code of this link. https://learn.adafruit.com/multi-taskin ... -3/scanner
However I could not replicate it in my code. I appreciate any help, thank you very much.

This is my code
Code: Select all | TOGGLE FULL SIZE
const byte spin2Hu[46] = {59, 81, 84, 86, 95, 96, 97, 98, 122, 127, 135, 76, 137, 125, 124, 98, 99, 100, 120, 130, 131, 71, 70, 46, 39, 38, 28, 25, 24, 23, 32, 33, 45, 51, 43, 35, 30, 23, 22, 21, 20, 141, 139, 56, 59};
const int numElements = sizeof(spin2Hu) / sizeof(spin2Hu[0]);

void recorridoHu(int r, int g, int b) {

  byte idx = (posicionH - 5 + numElements) % numElements;
  numeroPixeld = spin2Hu[idx];
  numeroPixel = spin2Hu[posicionH];
  tira.setPixelColor(numeroPixel, r, g, b);
  tira.show();
  tira.setPixelColor(numeroPixeld, 0, 0, 0);

  if (posicionH >= numElements) {
    posicionH = 0;
  }
}

void SOH() {
  tiempoTranscurrido = millis();
  if (tiempoTranscurrido - tiempoCambio > velocidad)
  {
    tiempoCambio = tiempoTranscurrido;
    posicionH++;
  }
}

Andresled099
 
Posts: 2
Joined: Sun Jan 12, 2020 5:53 pm

Re: Effect neopixel

by mikeysklar on Sun Jan 12, 2020 8:09 pm

Hello Andresled099,

In this scenario where you want the imitate the demo 'scanner' code which includes a DIM effect function I would recommend starting with our example code base and getting that going with your hardware. Then slowly start adding in your matrix of patterns and other code on top rather than the other way around.

mikeysklar
 
Posts: 611
Joined: Mon Aug 01, 2016 8:10 pm

Re: Effect neopixel

by Andresled099 on Mon Jan 13, 2020 2:04 pm

Hi, thanks for the advice, try to adapt my project to the Scanner code but I have not been able to do it satisfactorily. I extracted the part of the code I needed and added an array of a figure, then modified in this way the code "setPixelColor (antispinDu [i], Color1);" so that the route went through the array, but when it reaches the end of the array it does not return to 0. I tried to modify the conditions of the for cycle, of the if and else cycle, but I have not achieved the expected result. When replacing numPixels () with longArray = sizeof (antispinDu) / sizeof (antispinDu [0]) in the for condition, it appears that it returns to 0 once the entire array is complete but the blur effect is not achievedhat should. I do to make it work properly?



Code: Select all | TOGGLE FULL SIZE
 #include <Adafruit_NeoPixel.h>


int antispinDu[37]={59, 55, 54, 53, 63, 76, 135, 127, 122, 98, 124, 125, 137, 76,66, 49, 48, 47, 70, 72, 73, 74, 66, 51, 45, 33, 32, 23, 30, 35, 43, 51, 63, 78, 79, 80, 59};
// Pattern types supported:
enum  pattern { SCANNER };
// Patern directions supported:
enum  direction { FORWARD, REVERSE };

// NeoPattern Class - derived from the Adafruit_NeoPixel class
class NeoPatterns : public Adafruit_NeoPixel
{
    public:

    // Member Variables: 
    pattern  ActivePattern;  // which pattern is running
    direction Direction;     // direction to run the pattern
   
    unsigned long Interval;   // milliseconds between updates
    unsigned long lastUpdate; // last update of position
   
    uint32_t Color1, Color2;  // What colors are in use
    uint16_t TotalSteps;  // total number of steps in the pattern
    uint16_t Index;  // current step within the pattern
   
    void (*OnComplete)();  // Callback on completion of pattern
   
    // Constructor - calls base-class constructor to initialize strip
    NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)())
    :Adafruit_NeoPixel(pixels, pin, type)
    {
        OnComplete = callback;
    }

   
    // Update the pattern
    void Update()
    {
        if((millis() - lastUpdate) > Interval) // time to update
        {
            lastUpdate = millis();
            switch(ActivePattern)
            {
               
                case SCANNER:
                    ScannerUpdate();
                    break;
               
                default:
                    break;
            }
        }
    }
 
    // Increment the Index and reset at the end
    void Increment()
    {
        if (Direction == FORWARD)
        {
           Index++;
           if (Index >= TotalSteps)
            {
                Index = 0;
                if (OnComplete != NULL)
                {
                    OnComplete(); // call the comlpetion callback
                }
            }
        }
        else // Direction == REVERSE
        {
            --Index;
            if (Index <= 0)
            {
                Index = TotalSteps-1;
                if (OnComplete != NULL)
                {
                    OnComplete(); // call the comlpetion callback
                }
            }
        }
    }
   
    // Reverse pattern direction
    void Reverse()
    {
        if (Direction == FORWARD)
        {
            Direction = REVERSE;
            Index = TotalSteps-1;
        }
        else
        {
            Direction = FORWARD;
            Index = 0;
        }
    }
 
  int longArray = sizeof(antispinDu) / sizeof(antispinDu[0]);
 
  void Scanner(uint32_t color1, uint8_t interval)
    {
        ActivePattern = SCANNER;
        Interval = interval;
        TotalSteps = (numPixels() - 1) * 2;   // ??
        Color1 = color1;
        Index = 0;
    }

 
 
 void ScannerUpdate()
    {
        for (int i = 0; i < numPixels(); i++) //??
        {
            if (i == Index)  // Scan Pixel to the right
            {
                 setPixelColor(antispinDu[i], Color1);
         
            }
            else if (i == TotalSteps - Index)
            {
                 Index = 0;
            }
            else // Fading tail
            {
                 setPixelColor(i, DimColor(getPixelColor(i)));
                 
            }
        }
        show();
        Increment();
    }


     // 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;
    }

     // Returns the Red component of a 32-bit color
    uint8_t Red(uint32_t color)
    {
        return (color >> 16) & 0xFF;
    }

    // Returns the Green component of a 32-bit color
    uint8_t Green(uint32_t color)
    {
        return (color >> 8) & 0xFF;
    }

    // Returns the Blue component of a 32-bit color
    uint8_t Blue(uint32_t color)
    {
        return color & 0xFF;
    }
   
      uint32_t Wheel(byte WheelPos)
    {
        WheelPos = 255 - WheelPos;
        if(WheelPos < 85)
        {
            return Color(255 - WheelPos * 3, 0, WheelPos * 3);
        }
        else if(WheelPos < 170)
        {
            WheelPos -= 85;
            return Color(0, WheelPos * 3, 255 - WheelPos * 3);
        }
        else
        {
            WheelPos -= 170;
            return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
        }
    }
};

void StickComplete();
NeoPatterns Stick(147, 3, NEO_GRB + NEO_KHZ800, &StickComplete);


    void setup()
{
   Stick.begin();
   Serial.begin(9600);
   Stick.Scanner(Stick.Color(250, 0, 250), 100);
}
void loop()
{

  Stick.Update();
}

void StickComplete()
{
    // Random color change for next scan
    Stick.Color1 = Stick.Wheel(random(255));
}


I attach the code for better understanding

Andresled099
 
Posts: 2
Joined: Sun Jan 12, 2020 5:53 pm

Re: Effect neopixel

by mikeysklar on Mon Jan 13, 2020 4:17 pm

Andresled099,

I would sprinkle in some print statements so you can watch the code execute and try to see what is happening that it is not returning to zero.

mikeysklar
 
Posts: 611
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.