0

Multiple Strips with Pushbutton Support
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Multiple Strips with Pushbutton Support

by joshuakane on Thu Aug 10, 2017 9:52 pm

Hi There,

I went and attempted to add push button support for my sketch. set up a case select statement that on button push should, and does cycle me through the three cases I have defined. The behavior I am seeing is that for

Case 1 - I am not seeing the delay interval that I build into the code. I wanted them to light up each pixel with a delay, but they are all illuminating at once.

Case 2 - the "Scanner" is stuck on the first pixel. It is not moving back and forth, it just illuminates pixel "0"

Case 3 - This seems to work, but again all the lights refresh at once so all the lights turn off at once.

Once Case 3 is complete, it seems to take a few prolonged button pushes to get it to cycle back to "Case 1"

I have added debugging so I know it is running thought the functions I have created such as "SaberOn" and the ones I have borrowed such as "Scanner"

If you could let me know what I am doing wrong, and not just point me to the Multi-tasking guide for Neo-Pixels it would be great.

The code is below.

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



//---------------------------------BEGIN Yondu LIGHTING SECTION--------------------------------------------------------//
// Pattern types
enum  pattern { NONE, RAINBOW_CYCLE, COLOR_WIPE, SCANNER, SABEROFF, SABERON, COUNT_UPDOWN};
// Patern directions


enum  direction { FORWARD, REVERSE };


class YonduPatterns : 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

    bool red, green, blue;

    unsigned long interval = 200;
    unsigned long previousMillis = 0;        // will store last time LED Strip was updated

    uint32_t currentColor;// current Color in case we need it
    uint16_t currentPixel = 0;// what pixel are we operating on

    // check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();
    bool UpDown = true;
    int i = 0;



    // Constructor
    YonduPatterns(uint16_t pixels, uint8_t pin, uint8_t type)
      : Adafruit_NeoPixel(pixels, pin, type)
    {

    }

    // Update the pattern
    void Update()
    {
      if ((millis() - lastUpdate) > Interval) // time to update
      {
        lastUpdate = millis();
        switch (ActivePattern)
        {
          case RAINBOW_CYCLE:
            RainbowCycleUpdate();
            break;
          case SCANNER:
            ScannerUpdate();
            break;
          case SABEROFF:
            SaberOffUpdate();
          case SABERON:
            SaberOnUpdate();
            break;
          case COLOR_WIPE:
            ColorWipeUpdate();
          case COUNT_UPDOWN:
            CountUpDownUpdate();
            break;
          default:
            break;
        }
      }
    }

    // Increment the Index and reset at the end
    void Increment()
    {
      if (Direction == FORWARD)
      {
        Index++;
        if (Index >= TotalSteps)
        {
          Index = 0;

        }
      }
      else // Direction == REVERSE
      {
        --Index;
        if (Index <= 0)
        {
          Index = TotalSteps - 1;

        }
      }
    }



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


    // Update the CountUpDown Pattern
    // Initialize for a Count Up/Down
    void SaberOff(uint32_t color1, uint8_t interval, direction dir = FORWARD)
    {
      ActivePattern = SABEROFF;
      Interval = interval;
      TotalSteps = numPixels();
      Color1 = color1;
      Index = 0;
      Direction = dir;
    }

    void SaberOffUpdate()
    {
      Serial.println("Entering Saber Off Pattern");
      for (int i = (numPixels() - 1); i >= 0; i--)
      {
        Serial.print("Saber On Increment # ");
        Serial.println(i);
        setPixelColor(i, Color1);
      }
      show();
      Increment();
    }

    // Update the CountUpDown Pattern
    // Initialize for a Count Up/Down
    void SaberOn(uint32_t color1, uint8_t interval, direction dir = FORWARD)
    {
      ActivePattern = SABERON;
      Interval = interval;
      TotalSteps = numPixels();
      Color1 = color1;
      Index = 0;
      Direction = dir;
    }

    void SaberOnUpdate()
    {
        Serial.println("Entering Saber On Pattern");
       for (int i = 0; i < numPixels(); i++)
       {
        Serial.print("Saber On Increment # ");
        Serial.println(i);
        setPixelColor(i, Color1);
      }
      show();
      Increment();
    }


// Update the CountUpDown Pattern
// Initialize for a Count Up/Down
void CountUpDown(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD)
{
  ActivePattern = COUNT_UPDOWN;
  Interval = interval;
  TotalSteps = numPixels();
  Color1 = color1;
  Color2 = color2;
  Index = 0;
  Direction = dir;
}

void CountUpDownUpdate()
{
  if ((unsigned long)(millis() - previousMillis) >= interval) {
    previousMillis = millis();
    if ((UpDown == true))
    {
      setPixelColor(i, Color1);
      Serial.print ("the vlaue of i is ");
      Serial.println (i);
      i++;
      show();
      Increment();
      if (i == 15) {
        UpDown = false;
      }
    }
    if ((UpDown == false))
    {
      setPixelColor(i, Color2);
      i--;
      show();
      Increment();
      if (i == 0) {
        UpDown = true;
      }
    }

  }
}
// 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()
{
  Serial.println("Entering ColorWipe Pattern");
  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()
{
  Serial.println("Entering Scanner Pattern");
  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, 255, 0, 0);
    }
  }
  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) >> 2, Green(color) >> 2, Blue(color) >> 2);
  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);
  }
}
};


//---------------------------------- END Yondu Lighting SECTION ------------------------------------

#define UP 0
#define DOWN 1
#define BUTTONPIN 2             // the pin that the pushbutton is attached to


// Define some YonduPatterns for the Radar, Jolo Projector, Side lights and PSI
YonduPatterns Radar(11, 5, NEO_GRB + NEO_KHZ800); //radar has 7 LEDs in it and will be controlled on pin 5
YonduPatterns Side(10, 9, NEO_GRB + NEO_KHZ800);  //Side sections  have 15 LEDs in it and will be controlled on pin 9

//
// RF Pins
//
int         buttonState      = LOW;   // current state of the button
int         lastButtonState  = HIGH;   // previous state of the button
int         pixelState       = 0;
int         debounce_count   = 2;     // number of millis/samples to consider before declaring a debounced input


// Initialize everything and prepare to start
void setup()
{

  Serial.begin(9600);    // initialize serial communication
  Serial.print("Degugging Enabled");

  pinMode(BUTTONPIN, INPUT_PULLUP);

  //initialize all our LED sections
  Radar.begin();
  Side.begin();
}

void BumpState()
{
  pixelState = (pixelState + 1) % 4;  //can have values of 0, 1, or 2
}

// Main loop
void loop()
{

  // read the state of the switch into a local variable:
  buttonState = debounceRead(BUTTONPIN);
  if ((HIGH == buttonState) && (buttonState != lastButtonState))
  {
    BumpState();
  }
  lastButtonState = buttonState;
  switch (pixelState)
  {
    case 1:
        Serial.println("Case 1 Fired");
        Radar.SaberOn(Radar.Color(255, 0, 0), 2000, FORWARD); //Turn the Radar on and make it solid Red
        Radar.show();
        Side.show();
        Side.SaberOn(Side.Color(255, 0, 0), 2000,  FORWARD); //Count up and down in red
        break;
       
    case 2:
     
        Serial.println("Case 2 Fired");
        Radar.Scanner(Radar.Color(255, 255, 0), 50); // Radar scans Cylon style red with 50ms interval;
        Side.SaberOn(Side.Color(255, 0, 0), 0 , FORWARD); //Count up and down in red
        break;
    case 3:
        Serial.println("Case 3 Fired");
        Radar.ColorWipe(Radar.Color(0, 0, 0), 0 , FORWARD); //Turn Solid Red
        Radar.SaberOff(Radar.Color(0, 0, 0), 50 , FORWARD); //Count down in red
        Side.SaberOff(Side.Color(0, 0, 0), 50 , FORWARD); //Count down in red
      break;
    default:
      break; 
     }
    Serial.println("Updating Radar");
  Radar.Update();
  //Serial.println("Showing Radar");
  //Radar.show();
  Serial.println("Updating Side");
  Side.Update();
  //Serial.println("Showing Radar");
  //Side.show(); */
}

// End Loop

uint8_t debounceRead(int pin)
{
  uint8_t pinState = digitalRead(pin);
  uint32_t timeout = millis();
  while (millis() < timeout + 2)
  {
    if (digitalRead(pin) != pinState)
    {
      pinState = digitalRead(pin);
      timeout = millis();
    }
  }

  return pinState;
}

joshuakane
 
Posts: 266
Joined: Sat Apr 13, 2013 4:40 pm

Re: Multiple Strips with Pushbutton Support

by adafruit_support_bill on Fri Aug 11, 2017 5:52 am

Your switch statement will get executed on every pass through the loop, so you will be re-initializing the pixel state machines on every pass. You want to execute that statement only once per button press.

adafruit_support_bill
 
Posts: 63375
Joined: Sat Feb 07, 2009 10:11 am

Re: Multiple Strips with Pushbutton Support

by joshuakane on Sat Aug 12, 2017 8:02 am

Hi Bill,

I have been trying to do what you mentioned. I am trying to fire the case just once and then Exit out.

What I have noticed is that I have my modulo Set to 7, and in this example I have 6 Test Cases Setup
Each Case fires the event in an infinite loop, and never gets past the first iteration.

When I get to case 6, it does the same thing, but if I hit the button again, it does not fire any case, and the last pattern (Rainbow) runs fine. How to I make each case do that? I have tried If Statements and bools, but they are not helping.

Code: Select all | TOGGLE FULL SIZE
//Yondu lights and sound version 0.2 alpha
//Author: Derick Hess
// Based on NeoPatterns by Bill Earl found at https://learn.adafruit.com/multi-tasking-the-arduino-part-3/using-neopatterns

//Feel free to use and modify this as you see fit.
//Please give credit to both myself and Bill Earl if you redistrubute the code


//lighting only works soudn to be added later.
//This code uses NeoPixel style RGB LEDs you can find them on amazon or adafruit as individual LEDs or in strips
// I used these: http://www.amazon.com/gp/product/B00P2GQUYC?psc=1&redirect=true&ref_=oh_aui_detailpage_o04_s01
//you could also use the predone bars or possibly even rolls, as long as they are the RGB individually addressable LEDS


#include <Adafruit_NeoPixel.h>

#include <SoftwareSerial.h>




//---------------------------------BEGIN Yondu LIGHTING SECTION--------------------------------------------------------//
// Pattern types
enum  pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, SCANNER, FADE, SOLID_COLOR_RADAR, RANDOM, COLOR_WIPE };
// Patern directions


enum  direction { FORWARD, REVERSE };


class YonduPatterns : 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

    bool red, green, blue;
   
    unsigned long jIinterval = 200;
    unsigned long previousMillis = 0;        // will store last time LED Strip was updated

    // check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();
   
    // Constructor
    YonduPatterns(uint16_t pixels, uint8_t pin, uint8_t type)
    :Adafruit_NeoPixel(pixels, pin, type)
    {
       
    }
   
    // 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 SCANNER:
                    ScannerUpdate();
                    break;
                case FADE:
                    FadeUpdate();
                    break;
                case SOLID_COLOR_RADAR:
                    SolidColorRadarUpdate();
                    break;
                case RANDOM:
                    RandomUpdate();
                    break;
                case COLOR_WIPE:
                    ColorWipeUpdate();
                    break;
                default:
                    break;
            }
        }
    }
 
    // Increment the Index and reset at the end
    void Increment()
    {
        if (Direction == FORWARD)
        {
           Index++;
           if (Index >= TotalSteps)
            {
                Index = 0;
             
            }
        }
        else // Direction == REVERSE
        {
            --Index;
            if (Index <= 0)
            {
                Index = TotalSteps-1;
               
            }
        }
    }


 
    // 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 Random
    void Random(uint8_t interval, bool redOnOff,bool greenOnOff, bool blueOnOff )
    {
        ActivePattern = RANDOM;
        Interval = interval;
        TotalSteps = numPixels();
        Index = 0;
        red=redOnOff;
        green=greenOnOff;
        blue=blueOnOff;

       
    }
   
    // Update the random Pattern
    void RandomUpdate()
    {
      int randomOnOff=0;
      int randomGreen=0;
      int randomBlue=0;

      int randomRed=0;
      for(int i=0; i< numPixels(); i++)
        {
            randomSeed(analogRead(0));
            randomOnOff=random(0,100); //get a random number for led on or off
           
            randomSeed(analogRead(0));
            if (green == 1){
            randomGreen=random(0,50);  //random green
            }
            else{
               randomGreen=0;  //random green
            }
           
            randomSeed(analogRead(0));
             if (blue == 1){
            randomBlue=random(0,50);  //random green
            }
            else{
               randomBlue=0;  //random green
            }

            randomSeed(analogRead(0));
             if (red == 1){
            randomRed=random(0,255);  //random green
            }
            else{
               randomRed=0;  //random green
            }
           
           
            if (randomOnOff > 20)  // if greater than 50 led is on and random color
            {
           
            setPixelColor(i, randomRed,randomGreen,randomBlue); //random value for the blue and green of the LED
            }
            else  //otherwise it's off
            {
             
              setPixelColor(i, 0,0,0);  //LED off
            }
           
        }
        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 Solid Color
    void SolidColorRadar(uint32_t color, uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = SOLID_COLOR_RADAR;
        Interval = interval;
        TotalSteps = 23;
        //TotalSteps = numPixels();
        Color1 = color;
        Index = 0;
        Direction = dir;
       
    }
   
    // Update the Color Wipe Pattern 
    void SolidColorRadarUpdate(){
        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) >> 2, Green(color) >> 2, Blue(color) >> 2);
        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);
        }
    }

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

};



char val;         // variable to receive data from the serial port
//---------------------------------- END Yondu Lighting SECTION ------------------------------------

#define BUTTONPIN 2             // the pin that the pushbutton is attached to

// Define some YonduPatterns for the Radar, & Side
YonduPatterns Radar(10, 5, NEO_GRB + NEO_KHZ800); //radar has 4 LEDs in it and will be controlled on pin 5
YonduPatterns Side(10, 9, NEO_GRB + NEO_KHZ800);  //Side sections  have 6 LEDs in it and will be controlled on pin 7


//
// RF Pins
//
int         buttonState      = LOW;   // current state of the button
int         lastButtonState  = HIGH;   // previous state of the button
int         pixelState       = 0;
int         debounce_count   = 2;     // number of millis/samples to consider before declaring a debounced input


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

  pinMode(BUTTONPIN, INPUT_PULLUP);
 
    //initialize all our LED sections
    Radar.begin();
     Side.begin();
      Radar.SolidColorRadar(Radar.Color(255,0,0),100, FORWARD);
      Side.SolidColorRadar(Side.Color(255,0,0), 100, FORWARD);   
}

void BumpState()
{
  pixelState = (pixelState + 1) % 7;  //can have values of 0 - 6
}


// Main loop
void loop()
{
  // read the state of the switch into a local variable:
  buttonState = debounceRead(BUTTONPIN);
  if ((HIGH == buttonState) && (buttonState != lastButtonState))
  {
    BumpState();
  }
  lastButtonState = buttonState;
  switch (pixelState)
  {
      case 1:
      Serial.println("Yondu Case 1 Fired");
      Radar.SolidColorRadar(Radar.Color(255,0,0),100, FORWARD);
      Side.SolidColorRadar(Side.Color(255,0,0), 100, FORWARD);
      break;

    case 2:
      Serial.println("Yondu Case 2 Fired");
      Radar.Scanner(Radar.Color(255, 255, 0), 50); // Radar scans Cylon style red with 50ms interval;
      Side.SolidColorRadar(Side.Color(255,0,0),0, FORWARD);
      break;

    case 3:
      Serial.println("Yondu Case 3 Fired");
      Radar.SolidColorRadar(Radar.Color(0,0,0), 100, REVERSE);
      Side.SolidColorRadar(Side.Color(0,0,0), 100, REVERSE);
      break;

    case 4:
      Serial.println("Yondu Case 4 Fired");
      Radar.Scanner(Radar.Color(0, 100, 00), 100);
      Side.Fade(Side.Color(0, 0, 100), Side.Color(0, 0, 05), 100, 40, REVERSE);
      break;

    case 5:
      Serial.println("Yondu Case 5 Fired");
      Radar.SolidColorRadar(Radar.Color(255, 0, 0), 0, FORWARD);
      Side.Fade(Side.Color(0, 0, 100), Side.Color(0, 0, 05), 100, 40, REVERSE);
      break;

    case 6:
      Serial.println("Yondu Case 6 Fired");
      Side.RainbowCycle(10);
      Radar.SolidColorRadar(Side.Color(255,0,0), 100, REVERSE);
        if (HIGH == buttonState &&  lastButtonState == LOW)
            {
              BumpState();
            }
      break;
      }
//at the end of the loop we upfate all the patterns 
Radar.Update();
Side.Update();
  }

uint8_t debounceRead(int pin)
{
  uint8_t pinState = digitalRead(pin);
  uint32_t timeout = millis();
  while (millis() < timeout + 2)
  {
    if (digitalRead(pin) != pinState)
    {
      pinState = digitalRead(pin);
      timeout = millis();
    }
  }

  return pinState;
}


joshuakane
 
Posts: 266
Joined: Sat Apr 13, 2013 4:40 pm

Re: Multiple Strips with Pushbutton Support

by adafruit_support_bill on Sat Aug 12, 2017 8:14 am

You are still calling the switch statement in every pass through the loop, so every pass will re-initialize whatever the current state is.

If you move the switch statement into your BumpState() function, it will only initialize on a state change.

adafruit_support_bill
 
Posts: 63375
Joined: Sat Feb 07, 2009 10:11 am

Re: Multiple Strips with Pushbutton Support

by joshuakane on Sat Aug 12, 2017 9:40 am

Thank you very much Bill. That did the trick. This is going into a "Yondo" costume from Guardians of the Galaxy. It is being debueted over Labor Day weekend at Dragon*Con in Atlanta. There will also be a Sakar Ogord using adafruit lights and processors. If you like I can send pictures and hardware used for your cosplay folks.

joshuakane
 
Posts: 266
Joined: Sat Apr 13, 2013 4:40 pm

Re: Multiple Strips with Pushbutton Support

by adafruit_support_bill on Sat Aug 12, 2017 10:30 am

Good to hear it is working. If you have some good photos, post them here and I'll see if we can get them up on the blog.

adafruit_support_bill
 
Posts: 63375
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.