Reverse direction another way?!

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
perolalars
 
Posts: 11
Joined: Sat Jan 04, 2014 3:22 pm

Reverse direction another way?!

Post by perolalars »

Hi
Been trying to use the code (from thread viewtopic.php?f=47&t=49649&p=250399&hil ... trip+chase) for reversing direction of neopixel led strip color chase but cant get it to work. Total beginner at this so really need help!
I like to make the colorChase when it reaches the end of the led strip reverse and run "backwards" and then next color-option would kick in and then the next etc.
This is the code so far:

Code: Select all

    #include <Adafruit_NeoPixel.h>

    #define PIN 6

    // John Bryant Display
    // Parameter 1 = number of pixels in strip
    // Parameter 2 = pin number (most are valid)
    // Parameter 3 = pixel type flags, add together as needed:
    //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
    //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
    //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
    //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
    Adafruit_NeoPixel strip = Adafruit_NeoPixel(120, PIN, NEO_GRB + NEO_KHZ800);

    void setup() {
      strip.begin();
      strip.show(); // Initialize all pixels to 'off'
    }

    void loop() {
      // Some example procedures showing how to display to the pixels:
      
      colorChase(strip.Color(127,127,127), 100); // White
      reversecolorChase(strip.Color(127,127,127), 100); // White
      colorChase(strip.Color(128,0,0), 100); // Maroon
      colorChase(strip.Color(0, 255, 0), 20); // Green
      threeColorChase(strip.Color(255, 0, 0), strip.Color(255, 255, 255), strip.Color(0, 255, 0), 20); // Red, White, Green
    }

    // threeColorChase: three pixels down the full strip.
    void threeColorChase(uint32_t c1, uint32_t c2, uint32_t c3, uint8_t wait) {
      int i;
     
      // Start by turning all pixels off:
      for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);

      // This the speed for colorChase(blank w/one pixel running down strip):
      for(i=0; i<strip.numPixels(); i++) {
        strip.setPixelColor(i, c1); // Set new pixel 'on'
        strip.setPixelColor(i+1, c2); // Set new pixel 'on'
        strip.setPixelColor(i+2, c3); // Set new pixel 'on'
        strip.show();              // Refresh LED states
        strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
        delay(wait);
      }

      strip.show(); // Refresh to turn off last pixel
    }


    // colorChase: one pixel down the full strip.
    void colorChase(uint32_t c, uint8_t wait) {
      int i;
     
      // Start by turning all pixels off:
      for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);

      // This the speed for colorChase(blank w/one pixel running down strip):
      for(i=0; i<strip.numPixels(); i++) {
        strip.setPixelColor(i, c); // Set new pixel 'on'
        strip.show();              // Refresh LED states
        strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
        delay(30);
      }
      }void reversecolorChase(uint32_t c, uint8_t wait)
      {
      for(int16_t i=(strip.numPixels()-1); i>=0; i--)
      {

      strip.show(); // Refresh to turn off last pixel
    }
    }
  
There is something wrong since I cant get this to work...Its the reversecolorChase I have problem with
Any help much appreciated
Per

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

Re: Reverse direction another way?!

Post by adafruit_support_bill »

This tutorial includes working code for a reversible chase (among other things):
https://learn.adafruit.com/multi-taskin ... 3/overview

User avatar
perolalars
 
Posts: 11
Joined: Sat Jan 04, 2014 3:22 pm

Re: Reverse direction another way?!

Post by perolalars »

Thanks for reply!
But as a newbie I feel quite lost, tried to break out the portions of code that I could use but get some errors and feels just like fumbling around, and dont know what to do!
Is it possible with some more handy advise?

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

Re: Reverse direction another way?!

Post by adafruit_support_bill »

Just add the NeoPatterns class to the beginning of your file and use only the parts you need.
The following code will run a chase pattern for a 120 pixel strip on pin 6 that alternates between forward and reverse.

Code: Select all

// Define a NeoPatternfor the strip
// as well as some completion routines
NeoPatterns strip(120, 6, NEO_GRB + NEO_KHZ800, &StripComplete);

 
// Initialize everything and prepare to start
void setup()
{
	strip.begin();

	// Kick off a pattern
	strip.TheaterChase(strip.Color(255,255,0), strip.Color(0,0,50), 100);
}
 
// Main loop
void loop()
{
	// Update the strip.
	strip.Update();
}
 
//------------------------------------------------------------
//Completion Routines - get called on completion of a pattern
//------------------------------------------------------------
 
// Strip Completion Callback
void StripComplete()
{
	strip.Reverse();
}

User avatar
perolalars
 
Posts: 11
Joined: Sat Jan 04, 2014 3:22 pm

Re: Reverse direction another way?!

Post by perolalars »

Hello Bill?!
I am so sorry but my head have stopped working (its been a long week..) And I would be very grateful if you could show even more in detail...
My code so far is:

Code: Select all

        #include <Adafruit_NeoPixel.h>

        #define PIN 6

        // John Bryant Display
        // Parameter 1 = number of pixels in strip
        // Parameter 2 = pin number (most are valid)
        // Parameter 3 = pixel type flags, add together as needed:
        //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
        //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
        //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
        //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
        Adafruit_NeoPixel strip = Adafruit_NeoPixel(120, PIN, NEO_GRB + NEO_KHZ800);

        void setup() {
          strip.begin();
          strip.show(); // Initialize all pixels to 'off'
        }

        void loop() {
          // Some example procedures showing how to display to the pixels:
         
          colorChase(strip.Color(127,127,127), 100); // White
          colorChase(strip.Color(128,0,0), 100); // Maroon
          colorChase(strip.Color(0, 255, 0), 20); // Green
          threeColorChase(strip.Color(255, 0, 0), strip.Color(255, 255, 255), strip.Color(0, 255, 0), 20); // Red, White, Green
        }

        // threeColorChase: three pixels down the full strip.
        void threeColorChase(uint32_t c1, uint32_t c2, uint32_t c3, uint8_t wait) {
          int i;
         
          // Start by turning all pixels off:
          for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);

          // This the speed for colorChase(blank w/one pixel running down strip):
          for(i=0; i<strip.numPixels(); i++) {
            strip.setPixelColor(i, c1); // Set new pixel 'on'
            strip.setPixelColor(i+1, c2); // Set new pixel 'on'
            strip.setPixelColor(i+2, c3); // Set new pixel 'on'
            strip.show();              // Refresh LED states
            strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
            delay(wait);
          }

          strip.show(); // Refresh to turn off last pixel
        }


        // colorChase: one pixel down the full strip.
        void colorChase(uint32_t c, uint8_t wait) {
          int i;
         
          // Start by turning all pixels off:
          for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);

          // This the speed for colorChase(blank w/one pixel running down strip):
          for(i=0; i<strip.numPixels(); i++) {
            strip.setPixelColor(i, c); // Set new pixel 'on'
            strip.show();              // Refresh LED states
            strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
            delay(30);
          }
          {

          strip.show(); // Refresh to turn off last pixel
        }
        }
      
And I cant for my life understand where to define the Neopattern class and how to get it to reverse.
I am sorry for not see the obvious!
Per

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

Re: Reverse direction another way?!

Post by adafruit_support_bill »

The Neopattern class will not work with the code you have. Start with the simple example I wrote for you and adapt it to your needs. You will need to copy the NeoPattern class to the beginning of the file.

User avatar
perolalars
 
Posts: 11
Joined: Sat Jan 04, 2014 3:22 pm

Re: Reverse direction another way?!

Post by perolalars »

Could you take a look? And its a total mess, lot of errors... but I cant figure right from wrong....

Code: Select all

  #include <Adafruit_NeoPixel.h>

// Pattern types supported:
enum  pattern { THEATER_CHASE };
// Pattern 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 THEATER_CHASE:
                    TheaterChaseUpdate();
                    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 completion callback
                }
            }
        }
        else // Direction == REVERSE
        {
            --Index;
            if (Index <= 0)
            {
                Index = TotalSteps-1;
                if (OnComplete != NULL)
                {
                    OnComplete(); // call the completion callback
                }
            }
        }
    }
    
    // Reverse pattern direction
    void Reverse()
    {
        if (Direction == FORWARD)
        {
            Direction = REVERSE;
            Index = TotalSteps-1;
        }
        else
        {
            Direction = FORWARD;
            Index = 0;
        }
    }

    // Initialize for a Theater Chase
    void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = THEATER_CHASE;
        Interval = interval;
        TotalSteps = numPixels();
        Color1 = color1;
        Color2 = color2;
        Index = 0;
        Direction = dir;
   }
    
    // Update the Theater Chase Pattern
    void TheaterChaseUpdate()
    {
        for(int i=0; i< numPixels(); i++)
        {
            if ((i + Index) % 3 == 0)
            {
                setPixelColor(i, Color1);
            }
            else
            {
                setPixelColor(i, Color2);
            }
        }
        show();
        Increment();
    }

    // Define a NeoPatternfor the strip
    // as well as some completion routines
    NeoPatterns strip(120, 6, NEO_GRB + NEO_KHZ800, &StripComplete);

     
    // Initialize everything and prepare to start
    void setup()
    {
       strip.begin();

       // Kick off a pattern
       strip.TheaterChase(strip.Color(255,255,0), strip.Color(0,0,50), 100);
    }
     
    // Main loop
    void loop()
    {
       // Update the strip.
       strip.Update();
    }
     
    //------------------------------------------------------------
    //Completion Routines - get called on completion of a pattern
    //------------------------------------------------------------
     
    // Strip Completion Callback
    void StripComplete()
    {
       strip.Reverse();
    }

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

Re: Reverse direction another way?!

Post by adafruit_support_bill »

Include the complete NeoPattern class - unchanged.

User avatar
perolalars
 
Posts: 11
Joined: Sat Jan 04, 2014 3:22 pm

Re: Reverse direction another way?!

Post by perolalars »

I get errors anyhow....

Code: Select all

#include <Adafruit_NeoPixel.h>

// Pattern types supported:
enum  pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE };
// 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 RAINBOW_CYCLE:
                    RainbowCycleUpdate();
                    break;
                case THEATER_CHASE:
                    TheaterChaseUpdate();
                    break;
                case COLOR_WIPE:
                    ColorWipeUpdate();
                    break;
                case SCANNER:
                    ScannerUpdate();
                    break;
                case FADE:
                    FadeUpdate();
                    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;
        }
    }
    
    // Initialize for a RainbowCycle
    void RainbowCycle(uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = RAINBOW_CYCLE;
        Interval = interval;
        TotalSteps = 255;
        Index = 0;
        Direction = dir;
    }
    
    // Update the Rainbow Cycle Pattern
    void RainbowCycleUpdate()
    {
        for(int i=0; i< numPixels(); i++)
        {
            setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
        }
        show();
        Increment();
    }

    // Initialize for a Theater Chase
    void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = THEATER_CHASE;
        Interval = interval;
        TotalSteps = numPixels();
        Color1 = color1;
        Color2 = color2;
        Index = 0;
        Direction = dir;
   }
    
    // Update the Theater Chase Pattern
    void TheaterChaseUpdate()
    {
        for(int i=0; i< numPixels(); i++)
        {
            if ((i + Index) % 3 == 0)
            {
                setPixelColor(i, Color1);
            }
            else
            {
                setPixelColor(i, Color2);
            }
        }
        show();
        Increment();
    }

    // Initialize for a ColorWipe
    void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = COLOR_WIPE;
        Interval = interval;
        TotalSteps = numPixels();
        Color1 = color;
        Index = 0;
        Direction = dir;
    }
    
    // Update the Color Wipe Pattern
    void ColorWipeUpdate()
    {
        setPixelColor(Index, Color1);
        show();
        Increment();
    }
    
    // Initialize for a SCANNNER
    void Scanner(uint32_t color1, uint8_t interval)
    {
        ActivePattern = SCANNER;
        Interval = interval;
        TotalSteps = (numPixels() - 1) * 2;
        Color1 = color1;
        Index = 0;
    }

    // Update the Scanner Pattern
    void ScannerUpdate()
    { 
        for (int i = 0; i < numPixels(); i++)
        {
            if (i == Index)  // Scan Pixel to the right
            {
                 setPixelColor(i, Color1);
            }
            else if (i == TotalSteps - Index) // Scan Pixel to the left
            {
                 setPixelColor(i, Color1);
            }
            else // Fading tail
            {
                 setPixelColor(i, DimColor(getPixelColor(i)));
            }
        }
        show();
        Increment();
    }
    
    // Initialize for a Fade
    void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = FADE;
        Interval = interval;
        TotalSteps = steps;
        Color1 = color1;
        Color2 = color2;
        Index = 0;
        Direction = dir;
    }
    
    // Update the Fade Pattern
    void FadeUpdate()
    {
        // Calculate linear interpolation between Color1 and Color2
        // Optimise order of operations to minimize truncation error
        uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
        uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
        uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
        
        ColorSet(Color(red, green, blue));
        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;
    }

    // Set all pixels to a color (synchronously)
    void ColorSet(uint32_t color)
    {
        for (int i = 0; i < numPixels(); i++)
        {
            setPixelColor(i, color);
        }
        show();
    }

    // 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;
    }
    
    // Input a value 0 to 255 to get a color value.
    // The colours are a transition r - g - b - back to r.
    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);
        }
    }
};

// Define some NeoPatterns for the two rings and the stick
//  as well as some completion routines
NeoPatterns Ring1(24, 5, NEO_GRB + NEO_KHZ800, &Ring1Complete);
NeoPatterns Ring2(16, 6, NEO_GRB + NEO_KHZ800, &Ring2Complete);
NeoPatterns Stick(16, 7, NEO_GRB + NEO_KHZ800, &StickComplete);

// Initialize everything and prepare to start
void setup()
{
  Serial.begin(115200);

   pinMode(8, INPUT_PULLUP);
   pinMode(9, INPUT_PULLUP);
    
    // Initialize all the pixelStrips
    Ring1.begin();
    Ring2.begin();
    Stick.begin();
    
    // Kick off a pattern
    Ring1.TheaterChase(Ring1.Color(255,255,0), Ring1.Color(0,0,50), 100);
    Ring2.RainbowCycle(3);
    Ring2.Color1 = Ring1.Color1;
    Stick.Scanner(Ring1.Color(255,0,0), 55);
}

// Main loop
void loop()
{
    // Update the rings.
    Ring1.Update();
    Ring2.Update();    
    
    // Switch patterns on a button press:
    if (digitalRead(8) == LOW) // Button #1 pressed
    {
        // Switch Ring1 to FASE pattern
        Ring1.ActivePattern = FADE;
        Ring1.Interval = 20;
        // Speed up the rainbow on Ring2
        Ring2.Interval = 0;
        // Set stick to all red
        Stick.ColorSet(Stick.Color(255, 0, 0));
    }
    else if (digitalRead(9) == LOW) // Button #2 pressed
    {
        // Switch to alternating color wipes on Rings1 and 2
        Ring1.ActivePattern = COLOR_WIPE;
        Ring2.ActivePattern = COLOR_WIPE;
        Ring2.TotalSteps = Ring2.numPixels();
        // And update tbe stick
        Stick.Update();
    }
    else // Back to normal operation
    {
        // Restore all pattern parameters to normal values
        Ring1.ActivePattern = THEATER_CHASE;
        Ring1.Interval = 100;
        Ring2.ActivePattern = RAINBOW_CYCLE;
        Ring2.TotalSteps = 255;
        Ring2.Interval = min(10, Ring2.Interval);
        // And update tbe stick
        Stick.Update();
    }    
}

//------------------------------------------------------------
//Completion Routines - get called on completion of a pattern
//------------------------------------------------------------

// Ring1 Completion Callback
void Ring1Complete()
{
    if (digitalRead(9) == LOW)  // Button #2 pressed
    {
        // Alternate color-wipe patterns with Ring2
        Ring2.Interval = 40;
        Ring1.Color1 = Ring1.Wheel(random(255));
        Ring1.Interval = 20000;
    }
    else  // Retrn to normal
    {
      Ring1.Reverse();
    }
}

// Ring 2 Completion Callback
void Ring2Complete()
{
    if (digitalRead(9) == LOW)  // Button #2 pressed
    {
        // Alternate color-wipe patterns with Ring1
        Ring1.Interval = 20;
        Ring2.Color1 = Ring2.Wheel(random(255));
        Ring2.Interval = 20000;
    }
    else  // Retrn to normal
    {
        Ring2.RainbowCycle(random(0,10));
    }
}

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


    // Define a NeoPatternfor the strip
    // as well as some completion routines
    NeoPatterns strip(120, 6, NEO_GRB + NEO_KHZ800, &StripComplete);

     
    // Initialize everything and prepare to start
    void setup()
    {
       strip.begin();

       // Kick off a pattern
       strip.TheaterChase(strip.Color(255,255,0), strip.Color(0,0,50), 100);
    }
     
    // Main loop
    void loop()
    {
       // Update the strip.
       strip.Update();
    }
     
    //------------------------------------------------------------
    //Completion Routines - get called on completion of a pattern
    //------------------------------------------------------------
     
    // Strip Completion Callback
    void StripComplete()
    {
       strip.Reverse();
    }

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

Re: Reverse direction another way?!

Post by adafruit_support_bill »

You included the setup and loop too. You just need the class definition, plus the setup and loop I posted:

Code: Select all

#include <Adafruit_NeoPixel.h>

// Pattern types supported:
enum  pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE };
// 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 RAINBOW_CYCLE:
                    RainbowCycleUpdate();
                    break;
                case THEATER_CHASE:
                    TheaterChaseUpdate();
                    break;
                case COLOR_WIPE:
                    ColorWipeUpdate();
                    break;
                case SCANNER:
                    ScannerUpdate();
                    break;
                case FADE:
                    FadeUpdate();
                    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;
        }
    }
    
    // Initialize for a RainbowCycle
    void RainbowCycle(uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = RAINBOW_CYCLE;
        Interval = interval;
        TotalSteps = 255;
        Index = 0;
        Direction = dir;
    }
    
    // Update the Rainbow Cycle Pattern
    void RainbowCycleUpdate()
    {
        for(int i=0; i< numPixels(); i++)
        {
            setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
        }
        show();
        Increment();
    }

    // Initialize for a Theater Chase
    void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = THEATER_CHASE;
        Interval = interval;
        TotalSteps = numPixels();
        Color1 = color1;
        Color2 = color2;
        Index = 0;
        Direction = dir;
   }
    
    // Update the Theater Chase Pattern
    void TheaterChaseUpdate()
    {
        for(int i=0; i< numPixels(); i++)
        {
            if ((i + Index) % 3 == 0)
            {
                setPixelColor(i, Color1);
            }
            else
            {
                setPixelColor(i, Color2);
            }
        }
        show();
        Increment();
    }

    // Initialize for a ColorWipe
    void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = COLOR_WIPE;
        Interval = interval;
        TotalSteps = numPixels();
        Color1 = color;
        Index = 0;
        Direction = dir;
    }
    
    // Update the Color Wipe Pattern
    void ColorWipeUpdate()
    {
        setPixelColor(Index, Color1);
        show();
        Increment();
    }
    
    // Initialize for a SCANNNER
    void Scanner(uint32_t color1, uint8_t interval)
    {
        ActivePattern = SCANNER;
        Interval = interval;
        TotalSteps = (numPixels() - 1) * 2;
        Color1 = color1;
        Index = 0;
    }

    // Update the Scanner Pattern
    void ScannerUpdate()
    { 
        for (int i = 0; i < numPixels(); i++)
        {
            if (i == Index)  // Scan Pixel to the right
            {
                 setPixelColor(i, Color1);
            }
            else if (i == TotalSteps - Index) // Scan Pixel to the left
            {
                 setPixelColor(i, Color1);
            }
            else // Fading tail
            {
                 setPixelColor(i, DimColor(getPixelColor(i)));
            }
        }
        show();
        Increment();
    }
    
    // Initialize for a Fade
    void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = FADE;
        Interval = interval;
        TotalSteps = steps;
        Color1 = color1;
        Color2 = color2;
        Index = 0;
        Direction = dir;
    }
    
    // Update the Fade Pattern
    void FadeUpdate()
    {
        // Calculate linear interpolation between Color1 and Color2
        // Optimise order of operations to minimize truncation error
        uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
        uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
        uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
        
        ColorSet(Color(red, green, blue));
        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;
    }

    // Set all pixels to a color (synchronously)
    void ColorSet(uint32_t color)
    {
        for (int i = 0; i < numPixels(); i++)
        {
            setPixelColor(i, color);
        }
        show();
    }

    // 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;
    }
    
    // Input a value 0 to 255 to get a color value.
    // The colours are a transition r - g - b - back to r.
    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);
        }
    }
};


    // Define a NeoPattern for the strip
    // as well as some completion routines
    NeoPatterns strip(120, 6, NEO_GRB + NEO_KHZ800, &StripComplete);

     
    // Initialize everything and prepare to start
    void setup()
    {
       strip.begin();

       // Kick off a pattern
       strip.TheaterChase(strip.Color(255,255,0), strip.Color(0,0,50), 100);
    }
     
    // Main loop
    void loop()
    {
       // Update the strip.
       strip.Update();
    }
     
    //------------------------------------------------------------
    //Completion Routines - get called on completion of a pattern
    //------------------------------------------------------------
     
    // Strip Completion Callback
    void StripComplete()
    {
       strip.Reverse();
    }

User avatar
perolalars
 
Posts: 11
Joined: Sat Jan 04, 2014 3:22 pm

Re: Reverse direction another way?!

Post by perolalars »

It was not the TheaterChase I was after, I think it calls ColorWipe as pattern. One lit pixel runs along the whole length of strip and then reverse direction..
Tried this :

Code: Select all

    #include <Adafruit_NeoPixel.h>

    // Pattern types supported:
    enum  pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE };
    // 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 RAINBOW_CYCLE:
                        RainbowCycleUpdate();
                        break;
                    case THEATER_CHASE:
                        TheaterChaseUpdate();
                        break;
                    case COLOR_WIPE:
                        ColorWipeUpdate();
                        break;
                    case SCANNER:
                        ScannerUpdate();
                        break;
                    case FADE:
                        FadeUpdate();
                        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;
            }
        }
       
        // Initialize for a RainbowCycle
        void RainbowCycle(uint8_t interval, direction dir = FORWARD)
        {
            ActivePattern = RAINBOW_CYCLE;
            Interval = interval;
            TotalSteps = 255;
            Index = 0;
            Direction = dir;
        }
       
        // Update the Rainbow Cycle Pattern
        void RainbowCycleUpdate()
        {
            for(int i=0; i< numPixels(); i++)
            {
                setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
            }
            show();
            Increment();
        }

        // Initialize for a Theater Chase
        void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD)
        {
            ActivePattern = THEATER_CHASE;
            Interval = interval;
            TotalSteps = numPixels();
            Color1 = color1;
            Color2 = color2;
            Index = 0;
            Direction = dir;
       }
       
        // Update the Theater Chase Pattern
        void TheaterChaseUpdate()
        {
            for(int i=0; i< numPixels(); i++)
            {
                if ((i + Index) % 3 == 0)
                {
                    setPixelColor(i, Color1);
                }
                else
                {
                    setPixelColor(i, Color2);
                }
            }
            show();
            Increment();
        }

        // Initialize for a ColorWipe
        void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD)
        {
            ActivePattern = COLOR_WIPE;
            Interval = interval;
            TotalSteps = numPixels();
            Color1 = color;
            Index = 0;
            Direction = dir;
        }
       
        // Update the Color Wipe Pattern
        void ColorWipeUpdate()
        {
            setPixelColor(Index, Color1);
            show();
            Increment();
        }
       
        // Initialize for a SCANNNER
        void Scanner(uint32_t color1, uint8_t interval)
        {
            ActivePattern = SCANNER;
            Interval = interval;
            TotalSteps = (numPixels() - 1) * 2;
            Color1 = color1;
            Index = 0;
        }

        // Update the Scanner Pattern
        void ScannerUpdate()
        {
            for (int i = 0; i < numPixels(); i++)
            {
                if (i == Index)  // Scan Pixel to the right
                {
                     setPixelColor(i, Color1);
                }
                else if (i == TotalSteps - Index) // Scan Pixel to the left
                {
                     setPixelColor(i, Color1);
                }
                else // Fading tail
                {
                     setPixelColor(i, DimColor(getPixelColor(i)));
                }
            }
            show();
            Increment();
        }
       
        // Initialize for a Fade
        void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD)
        {
            ActivePattern = FADE;
            Interval = interval;
            TotalSteps = steps;
            Color1 = color1;
            Color2 = color2;
            Index = 0;
            Direction = dir;
        }
       
        // Update the Fade Pattern
        void FadeUpdate()
        {
            // Calculate linear interpolation between Color1 and Color2
            // Optimise order of operations to minimize truncation error
            uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
            uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
            uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
           
            ColorSet(Color(red, green, blue));
            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;
        }

        // Set all pixels to a color (synchronously)
        void ColorSet(uint32_t color)
        {
            for (int i = 0; i < numPixels(); i++)
            {
                setPixelColor(i, color);
            }
            show();
        }

        // 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;
        }
       
        // Input a value 0 to 255 to get a color value.
        // The colours are a transition r - g - b - back to r.
        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);
            }
        }
    };


        // Define a NeoPattern for the strip
        // as well as some completion routines
        NeoPatterns strip(120, 6, NEO_GRB + NEO_KHZ800, &StripComplete);

         
        // Initialize everything and prepare to start
        void setup()
        {
           strip.begin();

           // Kick off a pattern
           strip.ColorWipe(strip.Color(255,255,255), 50);
        }
         
        // Main loop
        void loop()
        {
           // Update the strip.
           strip.Update();
        }
         
        //------------------------------------------------------------
        //Completion Routines - get called on completion of a pattern
        //------------------------------------------------------------
         
        // Strip Completion Callback
        void StripComplete()
        {
           strip.Reverse();
        }
And it just lights up the first led (in white light) and stops...
Really happy for your patience with me!

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

Re: Reverse direction another way?!

Post by adafruit_support_bill »

One lit pixel runs along the whole length of strip and then reverse direction..
That sounds more like the "Scanner" pattern.

Code: Select all

            // Initialize everything and prepare to start
            void setup()
            {
               strip.begin();

               // Kick off a pattern
               strip.Scanner(strip.Color(255,255,255), 50);
            }
The scanner pattern automatically reverses, so you can comment out the reverse in the callback:

Code: Select all

            // Strip Completion Callback
            void StripComplete()
            {
               // strip.Reverse();
            }

User avatar
perolalars
 
Posts: 11
Joined: Sat Jan 04, 2014 3:22 pm

Re: Reverse direction another way?!

Post by perolalars »

After a lot of headache I got it to work (there was some hardware problems to) but the code

Code: Select all

    #include <Adafruit_NeoPixel.h>

    // Pattern types supported:
    enum  pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE };
    // 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 RAINBOW_CYCLE:
                        RainbowCycleUpdate();
                        break;
                    case THEATER_CHASE:
                        TheaterChaseUpdate();
                        break;
                    case COLOR_WIPE:
                        ColorWipeUpdate();
                        break;
                    case SCANNER:
                        ScannerUpdate();
                        break;
                    case FADE:
                        FadeUpdate();
                        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;
            }
        }
        
        // Update the Rainbow Cycle Pattern
        void RainbowCycleUpdate()
        {
            for(int i=0; i< numPixels(); i++)
            {
                setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
            }
            show();
            Increment();
        }

        // Initialize for a Theater Chase
        void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD)
        {
            ActivePattern = THEATER_CHASE;
            Interval = interval;
            TotalSteps = numPixels();
            Color1 = color1;
            Color2 = color2;
            Index = 0;
            Direction = dir;
       }
       
        // Update the Theater Chase Pattern
        void TheaterChaseUpdate()
        {
            for(int i=0; i< numPixels(); i++)
            {
                if ((i + Index) % 3 == 0)
                {
                    setPixelColor(i, Color1);
                }
                else
                {
                    setPixelColor(i, Color2);
                }
            }
            show();
            Increment();
        }

        // Initialize for a ColorWipe
        void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD)
        {
            ActivePattern = COLOR_WIPE;
            Interval = interval;
            TotalSteps = numPixels();
            Color1 = color;
            Index = 0;
            Direction = dir;
        }
       
        // Update the Color Wipe Pattern
        void ColorWipeUpdate()
        {
            setPixelColor(Index, Color1);
            show();
            Increment();
        }
       
        // Initialize for a SCANNNER
        void Scanner(uint32_t color1, uint8_t interval)
        {
            ActivePattern = SCANNER;
            Interval = interval;
            TotalSteps = (numPixels() - 1) * 2;
            Color1 = color1;
            Index = 0;
        }

        // Update the Scanner Pattern
        void ScannerUpdate()
        {
            for (int i = 0; i < numPixels(); i++)
            {
                if (i == Index)  // Scan Pixel to the right
                {
                     setPixelColor(i, Color1);
                }
                else if (i == TotalSteps - Index) // Scan Pixel to the left
                {
                     setPixelColor(i, Color1);
                }
                else // Fading tail
                {
                     setPixelColor(i, DimColor(getPixelColor(i)));
                }
            }
            show();
            Increment();
        }
       
        // Initialize for a Fade
        void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD)
        {
            ActivePattern = FADE;
            Interval = interval;
            TotalSteps = steps;
            Color1 = color1;
            Color2 = color2;
            Index = 0;
            Direction = dir;
        }
       
        // Update the Fade Pattern
        void FadeUpdate()
        {
            // Calculate linear interpolation between Color1 and Color2
            // Optimise order of operations to minimize truncation error
            uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
            uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
            uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
           
            ColorSet(Color(red, green, blue));
            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;
        }

        // Set all pixels to a color (synchronously)
        void ColorSet(uint32_t color)
        {
            for (int i = 0; i < numPixels(); i++)
            {
                setPixelColor(i, color);
            }
            show();
        }

        // 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;
        }
       
        // Input a value 0 to 255 to get a color value.
        // The colours are a transition r - g - b - back to r.
        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);
            }
        }
    };


        // Define a NeoPattern for the strip
        // as well as some completion routines
        NeoPatterns strip(120, 6, NEO_GRB + NEO_KHZ800, &StripComplete);

         
        // Initialize everything and prepare to start
        void setup()
        {
           strip.begin();

           // Kick off a pattern
           strip.Scanner(strip.Color(255,255,255), 20);
        }
         
        // Main loop
        void loop()
        {
           // Update the strip.
           strip.Update();
        }
         
        //------------------------------------------------------------
        //Completion Routines - get called on completion of a pattern
        //------------------------------------------------------------
         
        // Strip Completion Callback
        void StripComplete()
        {
           //strip.Reverse();
        }
Now works with Scanner. Thanks a million!
But with the chance of being a pain in the b-tt, how do I do to run one of the other patterns after the Scanner pattern and then return again to Scanner pattern in a loop?

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

Re: Reverse direction another way?!

Post by adafruit_support_bill »

You can do that in the completion callback. Something like this will cycle through several patterns.

Code: Select all

        // Strip Completion Callback
        void StripComplete()
        {
           switch(strip.ActivePattern)
           {
                case SCANNER:  // done with a scan 
                   strip.ActivePattern = RAINBOW_CYCLE;  // switch to a rainbow
                   break;
                case RAINBOW_CYCLE:  // done with a rainbow
                   strip.ActivePattern = THEATER_CHASE;  // switch to a chase
                   break;
                case THEATER_CHASE:  // done with a chase
                   strip.ActivePattern = SCANNER;  // switch back to scanner
                   break;
                default:
                   strip.ActivePattern = SCANNER;
                   break;
           }
        }

User avatar
perolalars
 
Posts: 11
Joined: Sat Jan 04, 2014 3:22 pm

Re: Reverse direction another way?!

Post by perolalars »

Thank you so much for all help!
Now I am a little bit more able. (A little...)

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

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