Need advice and help integrating feature to toggle between light cycles buttons

Good day,

I have been working on the below sketch and did some great progress but realized that maybe I took the wrong approach and I am unable to see how I would go about integrating the final requirements in the sketch.

the sketch represent the remote control for my mini tank robot. the control has a thumb joystick and 2 push buttons.

When pressing the thumb joystick I can switch from MODE1 to MODE2 and OFF state.

When in Mode1 I use button 2 to switch from front light, back light, left lights, right lights and all lights but can only go one way in the sequence. this only trigger the normal light for when it is dark.

Mode2 is for showcasing led patterns and putting on a show for the kids and I can switch between 21 templates using button2 to go forward and button 3 to go backward through the templates.

a short video of the test done so far
DEMO

The problem: In mode1 I would like to integrate the same behavior like in mode2 where I can leverage button2 and 3 to toggle back and forth between the available light patterns.

I am having a hard time understanding how to accomplish because of the way I developed the sketch.

Can anyone help shed lights if I can still be able to do this without having to rethink the entire sketch? the hurdle I am having is that button 3 is already setup to cycle through 21 sketches and not like in mode 1 where there is only 5 sketches.

any help is appreciated
Thank You

/*
  this sketch uses as well ALA library eliminating Delay()
  https://github.com/bportaluri/ALA/archive/master.zip

   Tutorial page for EZButton: https://arduinogetstarted.com/tutorials/arduino-button-library

   This example:
     + uses debounce for multiple buttons.
     + reads state of multiple buttons
     + detects the pressed and released events of multiple buttons
*/
//define libraries
#include <AlaLedRgb.h>
#include <ezButton.h>
#include <FastLED.h>

//define pins for LED strip and number of LEDS on strip
#define LED_PIN     3
#define NUM_LEDS    30


//create object representing led strip to work with ALA library
AlaLedRgb rgbStrip;

//initial value of animation out of 21
int animation = 0;


//array containing all templates from the ALA library
int animList[21] = {
  ALA_ON,
  ALA_SPARKLE,
  ALA_SPARKLE2,
  ALA_PIXELSHIFTRIGHT,
  ALA_PIXELSMOOTHSHIFTRIGHT,
  ALA_PIXELSMOOTHSHIFTLEFT,
  ALA_MOVINGBARS,
  ALA_COMET,
  ALA_COMETCOL,
  ALA_GLOW,
  ALA_CYCLECOLORS,
  ALA_FADECOLORS,
  ALA_FIRE,
  ALA_BOUNCINGBALLS,
  ALA_BUBBLES,
  ALA_STROBO,
  ALA_GLOW,
  ALA_FADECOLORSLOOP,
  ALA_FADEIN,
  ALA_FADEOUT,
  ALA_BLINKALT
};


//Predefined group of LEDS for MODE1 this represent the lights for the robot Front lights, back lights, left lights, right lights
int allLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int frontLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int leftLEDS[] = {9, 10, 11, 12, 13, 14, 15, 16};
int rightLEDS[] = {17, 18, 19, 20, 21, 22, 23, 24};
int backLEDS[] = {25, 26, 27, 28, 29, 30};

//array of lEDS to work with the FASTLed library
CRGB leds[NUM_LEDS];


//buttons on the joystick, button one is the joystick push button
ezButton button1(A5);  // create ezButton object that attach to pin 7;
ezButton button2(A4);  // create ezButton object that attach to pin 8;
ezButton button3(A3);  // create ezButton object that attach to pin 8;

//function to lit all lights
void lightsOnAll() {
  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to turn all lights off
void lightsOffAll() {
  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::Black;
  }
  FastLED.show();
}

//function to lit front lights
void lightsforwardOnly() {
  for (int i = 0; i < 9; i++)
  {
    leds[frontLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit back lights
void lightsbackOnly() {
  for (int i = 0; i < 6; i++)
  {
    leds[backLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit right lights
void lightsrightOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[rightLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit left lights
void lightsleftOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[leftLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//fucntion representing mode 2 to start the showcase of lights
void mode2A() {
  rgbStrip.runAnimation();
}


void setup() {
  Serial.begin(9600);

  rgbStrip.initWS2812(30, 3);


  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(3, 500);
  FastLED.setBrightness(255);

  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  button3.setDebounceTime(50); // set debounce time to 50 milliseconds


  //clear everything on LED strip and start clean
  FastLED.clear();  // clear all pixel data
  FastLED.show();

}

void loop() {

  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop(); // MUST call the loop() function first


  unsigned long button1count = button1.getCount();
  unsigned long button2count = button2.getCount();
  unsigned long button3count = button3.getCount();

  // Serial.println(button1count);
  // Serial.println(button3count);


  //button counters for the 3 buttons
  if (button1count >= 3)
    button1.resetCount();

  if (button2count >= 5)
    button2.resetCount();

  if (button3count >= 21)
    button3.resetCount();

  //if button 1 pressed counter will increment then clear LED strip and start the animation , take note the animation start on second click of the joystick
  if (button1.isPressed()) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    //  Serial.println("The button is pressed");
    updateAnimation();
  }

  //if button 2 pressed counter will increment for either mode1 or mode 2 state then clear LED strip and start the animation .
  if (button2.isPressed()) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation >= 21) {
      animation = 0;
    }
    animation++;
    updateAnimation();
  }

  //if button 3 pressed counter will decrement for mode 2 only it can cycle 21 led templates.
  if (button3.isPressed()) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation <= 0) {
      animation = 21;
    }
    animation--;
    updateAnimation();
  }


  //the above statements are to control Mode1 and Mode2, mode 1 is to control white leds front, back, left, right, all. Mode 2 is to enable showcase of different leds templates
  if (button1count == 1 && button2count == 0) {
    lightsforwardOnly();
    Serial.println("Mode1A forward lights On");
  }

  else  if (button1count == 1 && button2count == 1) {
    lightsbackOnly();
    Serial.println("Mode1B back lights On");
  }

  else  if (button1count == 1 && button2count == 2) {
    lightsleftOnly();
    Serial.println("Mode1C left lights On");
  }

  else  if (button1count == 1 && button2count == 3) {
    lightsrightOnly();
    Serial.println("Mode1D right lights On");
  }

  else  if (button1count == 1 && button2count == 4) {
    lightsOnAll();
    Serial.println("Mode1E all lights On");
  }


  if (button1count == 2) {
    // lightsOnAll();
    Serial.println("Mode2A On");
    mode2A();
  }

}
//fucntion to run the 21 animations/templates.
void updateAnimation()
{
  rgbStrip.setAnimation(animList[animation % 21], 3000, alaPalRgb);
}

In principle, the joy stick button, Button1, cycles through MODE1 --> MODE2 --> OFF --> MODE1 . . . .etc.

in MODE2, Button2 has the function: cycle forward through the led patterns. Button 3 is cycle backwards through the led patterns.

in MODE1, button 2 cycles forward through a white light display.

All you want to do is to add the following function in MODE1 that button 3 cycles backwards through the white light display.

Is that it ?

I'd restructure it to make the behaviour between mode 1 and mode 2 more consistent. This is a basic outline. Most of your code stays exactly the same:

// light display 
if ( mode == 1 ) {  // button1count == 1 ?
    // coloured lights
    if ( button2pressed ) {
       // go forwards through animation
    }
    else if (button3pressed ) {
       // go backwards through animation
    }
}
else if ( mode == 2 ) {
    // white lights
    if ( button2pressed ) {
       // go forwards through white light sequence
    }
    else if (button3pressed ) {
       // go backwards through white light sequence
    }
}
else if ( mode == 0 ) {
    // all off
}

Thank you,

Yes this is it, bassically wish to have same behavior for mode 1 and mode 2 where I can use both buttons to cycle through available lights options.

I am revising your feedback and looking into this.

Quick question: is it possible to create an array containing existing arrays?

example:

In my sketch I built 5 arrays to group LEDS and was thinking of adding them to another array to create a list and from there work on the IF statement and be able to toggle back and forth in the array.

my first attempt failed:

//Predefined group of LEDS for MODE1 this represent the lights for the robot Front lights, back lights, left lights, right lights
int allLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int frontLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int leftLEDS[] = {9, 10, 11, 12, 13, 14, 15, 16};
int rightLEDS[] = {17, 18, 19, 20, 21, 22, 23, 24};
int backLEDS[] = {25, 26, 27, 28, 29, 30};


int myArray[5] = {
allLEDS[],
frontLEDS[],
leftLEDS[],
rightLEDS[],
backLEDS[]
};

I tried this as well and compiling failed

Google, "array of arrays C++"

Good day,

I have made progress based on the feedback given and I want to thank you.

The sketch does behave as it should now when cycling back and forth between led patterns but there is 2 small problems which I cant figure out.

Problem#1 when I press button2 when in mode1 I observe the counter for mode2 jump to 255 which is mind bugling.

Problem#2 when I cycle through the patterns in mode1 I observe that when incrementing using button1 the counter goes from 2 to 5 but should be from 1 to 5 and this make me not be able to see pattern number 1. The same happens when pushing button2 in mode1 when decrementing the counter for animation2 goes only 1 to 4 but should be 1 to 5.

I try to demonstrate using video for better understanding.

sketch:

//define libraries
#include <AlaLedRgb.h>
#include <ezButton.h>
#include <FastLED.h>

//define pins for LED strip and number of LEDS on strip
#define LED_PIN     3
#define NUM_LEDS    30


//create object representing led strip to work with ALA library
AlaLedRgb rgbStrip;

//initial value of animation1 and 2 //animation1 is to control counter to cycle throughtMode1(frontlight,backlight,left,rightlight) 
//animation2 is a counter to cycle through the ALA predefined templates 
int animation1 = 1;
int animation2 = 0;

//array containing all templates from the ALA library for Mode 2
int animList[] = {
  ALA_ON,
  ALA_SPARKLE,
  ALA_SPARKLE2,
  ALA_PIXELSHIFTRIGHT,
  ALA_PIXELSMOOTHSHIFTRIGHT,
  ALA_PIXELSMOOTHSHIFTLEFT,
  ALA_MOVINGBARS,
  ALA_COMET,
  ALA_COMETCOL,
  ALA_GLOW,
  ALA_CYCLECOLORS,
  ALA_FADECOLORS,
  ALA_FIRE,
  ALA_BOUNCINGBALLS,
  ALA_BUBBLES,
  ALA_STROBO,
  ALA_GLOW,
  ALA_FADECOLORSLOOP,
  ALA_FADEIN,
  ALA_FADEOUT,
  ALA_BLINKALT
};


//I Predefined groups of LEDS for MODE1 this represent the lights for the robot Front lights, back lights, left lights, right lights
int allLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int frontLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int leftLEDS[] = {9, 10, 11, 12, 13, 14, 15, 16};
int rightLEDS[] = {17, 18, 19, 20, 21, 22, 23, 24};
int backLEDS[] = {25, 26, 27, 28, 29, 30};


//array of lEDS to work with the FASTLed library
CRGB leds[NUM_LEDS];


//button one is the joystick push button and is used to switch from OFF/Mode1/Mode2 button 2 and 3 are push buttons they are used to move between templates in either Mode 1 or Mode 2 (button2 is forward and button3 is backward)
ezButton button1(A5);  // create ezButton object that attach to pin 7;
ezButton button2(A4);  // create ezButton object that attach to pin 8;
ezButton button3(A3);  // create ezButton object that attach to pin 8;



//I created 5 functions that represent the 5 differents template in Mode1, lights for the front, lights for the back, left,right and all light bright white.
//function to lit all lights
void lightsOnAll() {
  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to turn all lights off
void lightsOffAll() {
  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::Black;
  }
  FastLED.show();
}

//function to lit front lights
void lightsforwardOnly() {
  for (int i = 0; i < 9; i++)
  {
    leds[frontLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit back lights
void lightsbackOnly() {
  for (int i = 0; i < 6; i++)
  {
    leds[backLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit right lights
void lightsrightOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[rightLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit left lights
void lightsleftOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[leftLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function representing mode 2 to start the showcase of lights
void mode2A() {
  rgbStrip.runAnimation();
}


void setup() {
  Serial.begin(9600);

  //initialize LED strip
  rgbStrip.initWS2812(30, 3);

  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(3, 500);
  FastLED.setBrightness(255);

  //The below I use for debounce issue using the EZbutton library
  button1.setDebounceTime(70); // set debounce time to 50 milliseconds
  button2.setDebounceTime(70); // set debounce time to 50 milliseconds
  button3.setDebounceTime(70); // set debounce time to 50 milliseconds


  //clear everything on LED strip and start clean
  FastLED.clear();  // clear all pixel data
  FastLED.show();

}

void loop() {

  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop(); // MUST call the loop() function first

  //from EZbutton library to keep count of button1 and simply reset once it reaches 3
  unsigned long button1count = button1.getCount();


//the below works but there are 2 observation made. When pressing button 1 initially I set to mode 1 then when pushing button1 it increments but 
//I observe the animation2 counter jump to number 255
//another observation is that when pressing button 1 in mode1 it cycle forward throught the patterns but it reset back to 2 when it reaches 5 but it should reset to 1,
//this cause issue not being able to display one parttern from mode 1 since it goes from 2 to 5 instead of 1 to 5. 
//If I use button 3 in mode1 it decrement the animation1 counter but observe going from 1 to 4 only but should be 1 to 5. 


  //if button 1 is pressed counter1 will increment then look to see if over 3 and reset if needed then clear LED strip.
  if (button1.isPressed()) {
    animation1 = 1;
    animation2 = 0;
    if (button1count >= 3)
      button1.resetCount();
    FastLED.clear();  // clear all pixel data
    FastLED.show();

  }

  //if button 2 is pressed and button1counter will increment for either mode1 or mode 2 state then clear LED strip and start the animation .
  if (button2.isPressed() && button1count == 1) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation1 >= 5) {
      animation1 = 1;
    }
    animation1++;
  }

  if (button2.isPressed() && button1count == 2) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation2 >= 21) {
      animation2 = 0;
    }
    animation2++;
    updateAnimation2();

  }




  //if button 3 pressed counter will decrement for mode 2 only it can cycle 21 led templates.
  if (button3.isPressed() && button1count == 1) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation1 <= 1) {
      animation1 = 5;
    }
    animation1--;
  }

  if (button3.isPressed() && button1count == 2) {
    if (animation2 <= 0) {
      animation2 = 21;
    }
    animation2--;
    updateAnimation2();

  }


  //the above statements are to control Mode1 and Mode2, mode 1 is to control white leds front, back, left, right, all. Mode 2 is to enable showcase of different leds templates
  if (button1count == 1)  {

    if (animation1 == 1) {
      lightsforwardOnly();
    }
    else if
    (animation1 == 2) {
      lightsbackOnly();
    }
    else if
    (animation1 == 3) {
      lightsleftOnly();
    }
    else if
    (animation1 == 4) {
      lightsrightOnly();
    }
    else if
    (animation1 == 5) {
      lightsOnAll();
    }
  }


  if (button1count == 2) {

    if (animation2 >= 21) {
      animation2 = 0;
    }

    updateAnimation2();
    mode2A();

  }

   //Serial.println(button1count);
  Serial.println(animation1);
  Serial.println(animation2);

}
//fucntion to run the 21 animations/templates.
void updateAnimation2()
{
  rgbStrip.setAnimation(animList[animation2 % 21], 3000, alaPalRgb);
}

You may have a problem here as an example:

  //if button 2 is pressed and button1counter will increment for either mode1 or mode 2 state then clear LED strip and start the animation .
  if (button2.isPressed() && button1count == 1) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation1 >= 5) {
      animation1 = 1;
    }
    animation1++;
  }

If the animation1 counter has the value 5 (or over), it sets the counter back to 1 but then immediately increments it again to 2.

try this:

    if (animation1 >= 5) {
        animation1 = 1;
    }
    else animation1++;  // else

Thank you for your reply, I did make the addition of adding ELSE and it made a difference and now can cycle between 1-5. I can see how things need to be in order in my code to have correct behavior.

thank you very much.

The sketch is now working as it should but I still observe 255 value showing in animation2 when I press button2 to cycle through the patterns of mode1. It doesn't affect the sketch as I reset values each time button1 is pressed but I for some reason need to know why this is occuring.

I see 255 being the value for highest led, I am unsure why this behavior is occuring.

I will keep looking and figure out, if you have any input I will gladly listen.

thank you

//define libraries
#include <AlaLedRgb.h>
#include <ezButton.h>
#include <FastLED.h>

//define pins for LED strip and number of LEDS on strip
#define LED_PIN     3
#define NUM_LEDS    30


//create object representing led strip to work with ALA library
AlaLedRgb rgbStrip;

//initial value of animation1 and 2 //animation1 is to control counter to cycle throughtMode1(frontlight,backlight,left,rightlight)
//animation2 is a counter to cycle through the ALA predefined templates
int animation1 = 1;
int animation2 = 0;

//array containing all templates from the ALA library for Mode 2
int animList[] = {
  ALA_ON,
  ALA_SPARKLE,
  ALA_SPARKLE2,
  ALA_PIXELSHIFTRIGHT,
  ALA_PIXELSMOOTHSHIFTRIGHT,
  ALA_PIXELSMOOTHSHIFTLEFT,
  ALA_MOVINGBARS,
  ALA_COMET,
  ALA_COMETCOL,
  ALA_GLOW,
  ALA_CYCLECOLORS,
  ALA_FADECOLORS,
  ALA_FIRE,
  ALA_BOUNCINGBALLS,
  ALA_BUBBLES,
  ALA_STROBO,
  ALA_GLOW,
  ALA_FADECOLORSLOOP,
  ALA_FADEIN,
  ALA_FADEOUT,
  ALA_BLINKALT
};


//I Predefined groups of LEDS for MODE1 this represent the lights for the robot Front lights, back lights, left lights, right lights
int allLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int frontLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int leftLEDS[] = {9, 10, 11, 12, 13, 14, 15, 16};
int rightLEDS[] = {17, 18, 19, 20, 21, 22, 23, 24};
int backLEDS[] = {25, 26, 27, 28, 29, 30};


//array of lEDS to work with the FASTLed library
CRGB leds[NUM_LEDS];


//button one is the joystick push button and is used to switch from OFF/Mode1/Mode2 button 2 and 3 are push buttons they are used to move between templates in either Mode 1 or Mode 2 (button2 is forward and button3 is backward)
ezButton button1(A5);  // create ezButton object that attach to pin 7;
ezButton button2(A4);  // create ezButton object that attach to pin 8;
ezButton button3(A3);  // create ezButton object that attach to pin 8;



//I created 5 functions that represent the 5 differents template in Mode1, lights for the front, lights for the back, left,right and all light bright white.
//function to lit all lights
void lightsOnAll() {
  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to turn all lights off
void lightsOffAll() {
  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::Black;
  }
  FastLED.show();
}

//function to lit front lights
void lightsforwardOnly() {
  for (int i = 0; i < 9; i++)
  {
    leds[frontLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit back lights
void lightsbackOnly() {
  for (int i = 0; i < 6; i++)
  {
    leds[backLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit right lights
void lightsrightOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[rightLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit left lights
void lightsleftOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[leftLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function representing mode 2 to start the showcase of lights
void mode2A() {
  rgbStrip.runAnimation();
}


void setup() {
  Serial.begin(9600);

  //initialize LED strip
  rgbStrip.initWS2812(30, 3);

  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(3, 500);
  FastLED.setBrightness(255);

  //The below I use for debounce issue using the EZbutton library
  button1.setDebounceTime(70); // set debounce time to 50 milliseconds
  button2.setDebounceTime(70); // set debounce time to 50 milliseconds
  button3.setDebounceTime(70); // set debounce time to 50 milliseconds


  //clear everything on LED strip and start clean
  FastLED.clear();  // clear all pixel data
  FastLED.show();

}

void loop() {

  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop(); // MUST call the loop() function first

  //from EZbutton library to keep count of button1 and simply reset once it reaches 3
  unsigned long button1count = button1.getCount();


  //the below works but there are 2 observation made. When pressing button 1 initially I set to mode 1 then when pushing button1 it increments but
  //I observe the animation2 counter jump to number 255
  //another observation is that when pressing button 1 in mode1 it cycle forward throught the patterns but it reset back to 2 when it reaches 5 but it should reset to 1,
  //this cause issue not being able to display one parttern from mode 1 since it goes from 2 to 5 instead of 1 to 5.
  //If I use button 3 in mode1 it decrement the animation1 counter but observe going from 1 to 4 only but should be 1 to 5.


  //if button 1 is pressed counter1 will increment then look to see if over 3 and reset if needed then clear LED strip.
  if (button1.isPressed()) {
    animation1 = 1;
    animation2 = 0;
    if (button1count >= 3)
      button1.resetCount();
    FastLED.clear();  // clear all pixel data
    FastLED.show();

  }

  //if button 2 is pressed and button1counter will increment for either mode1 or mode 2 state then clear LED strip and start the animation .
  if (button2.isPressed() && button1count == 1) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation1 >= 5) {
      animation1 = 1;
     }

    else animation1++;
  }



  if (button2.isPressed() && button1count == 2) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation2 >= 21) {
      animation2 = 0;
    }
    animation2++;


  }


  //if button 3 pressed counter will decrement for mode 2 only it can cycle 21 led templates.
  if (button3.isPressed() && button1count == 1) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation1 <= 1) {
      animation1 = 5;
    }
   else animation1--;
  }


  if (button3.isPressed() && button1count == 2) {
    if (animation2 <= 0) {
      animation2 = 21;
    }
    animation2--;


  }


  //the above statements are to control Mode1 and Mode2, mode 1 is to control white leds front, back, left, right, all. Mode 2 is to enable showcase of different leds templates
  if (button1count == 1)  {

    if (animation1 == 1) {
      lightsforwardOnly();
    }
    else if
    (animation1 == 2) {
      lightsbackOnly();
    }
    else if
    (animation1 == 3) {
      lightsleftOnly();
    }
    else if
    (animation1 == 4) {
      lightsrightOnly();
    }
    else if
    (animation1 == 5) {
      lightsOnAll();
    }
  }


  if (button1count == 2) {

    if (animation2 >= 21) {
      animation2 = 0;
    }

    updateAnimation2();
    mode2A();

  }

  //Serial.println(button1count);
  Serial.println(animation1);
  Serial.println(animation2);

}
//fucntion to run the 21 animations/templates.
void updateAnimation2()
{
  rgbStrip.setAnimation(animList[animation2 % 21], 3000, alaPalRgb);
}

again, it looks like you have a problem here:

  if (button2.isPressed() && button1count == 2) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation2 >= 21) {
      animation2 = 0;
    }
    animation2++;
  }

The array animList[] has 21 elements in it, so the index can be any number between 0 and 20. Your code allows this to overflow by allowing animation2 to have the value 21. The solution is the same as before.

Thank you,

I revisted mode2 and made the change to correct the counter for animation 2 so it respect indexing of the array list.

I did tidy up the lines of code so I can better troubleshoot.

255 still show up for some odd reason.

thank you

//define libraries
#include <AlaLedRgb.h>
#include <ezButton.h>
#include <FastLED.h>

//define pins for LED strip and number of LEDS on strip
#define LED_PIN     3
#define NUM_LEDS    30


//create object representing led strip to work with ALA library
AlaLedRgb rgbStrip;

//initial value of animation1 and 2 //animation1 is to control counter to cycle throughtMode1(frontlight,backlight,left,rightlight)
//animation2 is a counter to cycle through the ALA predefined templates
int animation1 = 1;
int animation2 = 0;

//array containing all templates from the ALA library for Mode 2
int animList[] = {
  ALA_ON,
  ALA_SPARKLE,
  ALA_SPARKLE2,
  ALA_PIXELSHIFTRIGHT,
  ALA_PIXELSMOOTHSHIFTRIGHT,
  ALA_PIXELSMOOTHSHIFTLEFT,
  ALA_MOVINGBARS,
  ALA_COMET,
  ALA_COMETCOL,
  ALA_GLOW,
  ALA_CYCLECOLORS,
  ALA_FADECOLORS,
  ALA_FIRE,
  ALA_BOUNCINGBALLS,
  ALA_BUBBLES,
  ALA_STROBO,
  ALA_GLOW,
  ALA_FADECOLORSLOOP,
  ALA_FADEIN,
  ALA_FADEOUT,
  ALA_BLINKALT
};


//I Predefined groups of LEDS for MODE1 this represent the lights for the robot Front lights, back lights, left lights, right lights
int allLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int frontLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int leftLEDS[] = {9, 10, 11, 12, 13, 14, 15, 16};
int rightLEDS[] = {17, 18, 19, 20, 21, 22, 23, 24};
int backLEDS[] = {25, 26, 27, 28, 29, 30};


//array of lEDS to work with the FASTLed library
CRGB leds[NUM_LEDS];


//button one is the joystick push button and is used to switch from OFF/Mode1/Mode2 button 2 and 3 are push buttons they are used to move between templates in either Mode 1 or Mode 2 (button2 is forward and button3 is backward)
ezButton button1(A5);  // create ezButton object that attach to pin 7;
ezButton button2(A4);  // create ezButton object that attach to pin 8;
ezButton button3(A3);  // create ezButton object that attach to pin 8;



//I created 5 functions that represent the 5 differents template in Mode1, lights for the front, lights for the back, left,right and all light bright white.
//function to lit all lights
void lightsOnAll() {
  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to turn all lights off
void lightsOffAll() {
  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::Black;
  }
  FastLED.show();
}

//function to lit front lights
void lightsforwardOnly() {
  for (int i = 0; i < 9; i++)
  {
    leds[frontLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit back lights
void lightsbackOnly() {
  for (int i = 0; i < 6; i++)
  {
    leds[backLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit right lights
void lightsrightOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[rightLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit left lights
void lightsleftOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[leftLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function representing mode 2 to start the showcase of lights
void mode2A() {
  rgbStrip.runAnimation();
}


void setup() {
  Serial.begin(9600);

  //initialize LED strip
  rgbStrip.initWS2812(30, 3);

  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(3, 500);
  FastLED.setBrightness(255);

  //The below I use for debounce issue using the EZbutton library
  button1.setDebounceTime(70); // set debounce time to 50 milliseconds
  button2.setDebounceTime(70); // set debounce time to 50 milliseconds
  button3.setDebounceTime(70); // set debounce time to 50 milliseconds


  //clear everything on LED strip and start clean
  FastLED.clear();  // clear all pixel data
  FastLED.show();

}

void loop() {

  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop(); // MUST call the loop() function first

  //from EZbutton library to keep count of button1 and simply reset once it reaches 3
  unsigned long button1count = button1.getCount();


  //the below works but there are 2 observation made. When pressing button 1 initially I set to mode 1 then when pushing button1 it increments but
  //I observe the animation2 counter jump to number 255
  //another observation is that when pressing button 1 in mode1 it cycle forward throught the patterns but it reset back to 2 when it reaches 5 but it should reset to 1,
  //this cause issue not being able to display one parttern from mode 1 since it goes from 2 to 5 instead of 1 to 5.
  //If I use button 3 in mode1 it decrement the animation1 counter but observe going from 1 to 4 only but should be 1 to 5.


  //if button 1 is pressed counter1 will increment then look to see if over 3 and reset if needed then clear LED strip.
  if (button1.isPressed()) {
    animation1 = 1;
    animation2 = 0;
    if (button1count >= 3)
      button1.resetCount();
    FastLED.clear();  // clear all pixel data
    FastLED.show();

  }

  //if button 2 is pressed and button1counter will increment for either mode1 or mode 2 state then clear LED strip and start the animation .
  if (button2.isPressed() && button1count == 1) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation1 >= 5) {
      animation1 = 1;
    }

    else animation1++;
  }



  if (button2.isPressed() && button1count == 2) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation2 >= 20) {
      animation2 = 0;
    }
    else animation2++;
  }


  //if button 3 pressed counter will decrement for mode 2 only it can cycle 21 led templates.
  if (button3.isPressed() && button1count == 1) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();

    if (animation1 <= 1) {
      animation1 = 5;
    }
    else  animation1--;
  }


  if (button3.isPressed() && button1count == 2) {
    FastLED.clear();  // clear all pixel data
    FastLED.show();
    if (animation2 <= 0) {
      animation2 = 20;
    }
    else animation2--;
  }


  //the above statements are to control Mode1 and Mode2, mode 1 is to control white leds front, back, left, right, all. Mode 2 is to enable showcase of different leds templates
  if (button1count == 1)  {

    if (animation1 == 1) {
      lightsforwardOnly();
    }
    else if
    (animation1 == 2) {
      lightsbackOnly();
    }
    else if
    (animation1 == 3) {
      lightsleftOnly();
    }
    else if
    (animation1 == 4) {
      lightsrightOnly();
    }
    else if
    (animation1 == 5) {
      lightsOnAll();
    }
  }


  if (button1count == 2) {
    updateAnimation2();
    mode2A();
  }

  //Serial.println(button1count);
  Serial.println(animation1);
  Serial.println(animation2);

}
//fucntion to run the 21 animations/templates.
void updateAnimation2()
{
  rgbStrip.setAnimation(animList[animation2 % 21], 3000, alaPalRgb);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.