How to loop a custom function

Im trying to make a custom controller for my LED strip and i need help with looping the custom pallets

located in "void loop"

#include <ezButton.h>
#include <FastLED.h>

#define NUM_LEDS 100
#define DATA_PIN 3
#define BRIGHTNESS 100

ezButton Pallet_1 (8); 
ezButton Pallet_2 (9);
ezButton Pallet_3 (10);

CRGB leds[NUM_LEDS];



void setup() {
  Serial.begin(9600);
  Pallet_1.setDebounceTime(50);
  Pallet_2.setDebounceTime(50);
  Pallet_3.setDebounceTime(50);
  
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);  // GRB ordering is typical
  FastLED.setBrightness(BRIGHTNESS);
    leds[0] = CRGB::Black;
  FastLED.show();
}

void loop() {
 Pallet_1.loop();
 Pallet_2.loop();
 Pallet_3.loop();

 int Pallet_1State = Pallet_1.getState();
 int Pallet_2State = Pallet_2.getState();
 int Pallet_3State = Pallet_3.getState();
 
 if(Pallet_1.isPressed())Pallet1();
 if(Pallet_2.isPressed())Pallet2();
 if(Pallet_3.isPressed())Pallet3();

}
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }

void Pallet1() {
 static uint8_t hue = 0;
  for(int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV(hue++, 255, 255);
    FastLED.show(); 
    fadeall();
    delay(10);
  } 
  for(int i = (NUM_LEDS)-1; i >= 0; i--) {
    leds[i] = CHSV(hue++, 255, 255);
    FastLED.show();
    fadeall();
    delay(10);
  }
}
void Pallet2() {
  leds[0] = CRGB::Green;
  FastLED.show();
}

void Pallet3() {
  leds[0] = CRGB::Blue;
  FastLED.show();
}
1 Like

What do You want us to help You with?
Well done using autoformat and code tags!
That code looks okey to me.

Could you restate what it is that you want? Could you describe what the code currently does? Could you describe what you'd like the code to do?

+1 for using code tags.

ok so located in the main loop you have

 if(Pallet_1.isPressed())Pallet1();
 if(Pallet_2.isPressed())Pallet2();
 if(Pallet_3.isPressed())Pallet3();

which detects when the button is pressed and plays the pallet's ive set up but it only plays it once
i want it to loop till you press another button selecting another pallet which will then loop again till you press another pallet

hope that makes sense

pondering... if you want it to be in states that are independent of the current button state, you need a variable to remember that. Then you set the variable with the buttons, act on the variable independently. The variable would indicate one of 4 states, pallet_1, pallet_2, pallet_3, or none.

so i did what u said and did

 int Pallet_1State = Pallet_1.getState();
 int Pallet_2State = Pallet_2.getState();
 int Pallet_3State = Pallet_3.getState();
 int Pallet = 0;
 
 if(Pallet_1.isPressed())int Pallet = 1;
 if(Pallet_2.isPressed())int Pallet = 2;
 if(Pallet_3.isPressed())int Pallet = 3;

 if (Pallet = 1;)Pallet1();
 if (Pallet = 2;)Pallet2();
 if (Pallet = 3;)Pallet3(); <<<<<<<<<<<

but i keep getting the error
"expected primary-expression before ')' token"
(marked the line with) <<

perhaps you can spot the extra ;?

In this code you should remove all of the int.

 if(Pallet_1.isPressed())int Pallet = 1;
 if(Pallet_2.isPressed())int Pallet = 2;
 if(Pallet_3.isPressed())int Pallet = 3;

Pallet = 1 is an assignment NOT a test. Pallet == 1 is a test.

 if (Pallet = 1;)Pallet1();
 if (Pallet = 2;)Pallet2();
 if (Pallet = 3;)Pallet3();

I think what you mean is this:

 if (Pallet == 1) Pallet1();
 if (Pallet == 2) Pallet2();
 if (Pallet == 3) Pallet3();

is different from

Pallet == 1

Instead of:

 if (Pallet == 1) Pallet1();
 if (Pallet == 2) Pallet2();
 if (Pallet == 3) Pallet3();

you could write:

switch(Pallet)
{
  case 1:
    Pallet1();
    break;

  case 2:
    Pallet2();
    break;

  case 3:
    Pallet3();
    break;
}

Hello

Maybe try something like this

void loop()
{
  ...
  static ezButton * pallet = nullptr;

  if(Pallet_1.isPressed()) pallet = &Pallet_1;
  if(Pallet_2.isPressed()) pallet = &Pallet_2;
  if(Pallet_3.isPressed()) pallet = &Pallet_3;
    
  Pallet( pallet );
  ...
}

void Pallet( ezButton * pallet )
{
  if ( pallet == &Pallet_1 )
  {
    static uint8_t hue = 0;
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CHSV(hue++, 255, 255);
      FastLED.show(); 
      fadeall();
      delay(10);
    } 
    for(int i = (NUM_LEDS)-1; i >= 0; i--) {
      leds[i] = CHSV(hue++, 255, 255);
      FastLED.show();
      fadeall();
      delay(10);
    }
  }

  else if ( pallet == &Pallet_2 )
  {
    leds[0] = CRGB::Green;
    FastLED.show();
  }

  else if ( pallet == &Pallet_3 )
  {
    leds[0] = CRGB::Blue;
    FastLED.show();
  }
}

yeah sorry i just noticed it :grinning_face_with_smiling_eyes:

@ guix

Unnecessary high level to use for a member not being highly experienced.