I am using a switch case to call a loop (FX1(), FX2(),...) to change the color effect of an led strip. I am using the neopixel library and defining colors to be used for that effect. I want to define all the colors in in a separate loop and call that loop to use the defined colors (void colors();). How can I make a list of colors and call that list for each case? instead of defining the same color for each case.
I'm not exactly clear what you are asking/describing. Sounds like you may be using some programming terminology where it may not apply.
I think the answer is you make an array. Then you can access the array by using an index.
const uint32_t myColours[] = {
strip.colour(123, 234, 34),
strip.colour(56, 67, 78),
...
};
My terminology is probably not correct. here is an example of what im trying to do.
void loop(){
switch (state){
case 1:
FX1();
strip.setBrightness(pot);
break;
case 2:
FX2();
strip.setBrightness(pot);
break;
}
void colors(){
uint32_t red = strip.Color(255,0,0);
uint32_t blue = strip.Color(0,0,255);
}
void FX1(){
colors();
strip.fill(red);
strip.show();
}
void FX2(){
colors();
strip.fill(blue);
strip.show();
}
just define them globally, not in a function and initialise them either in setup directly or through a function but don't repeat uint32_t(read about scope)
uint32_t redColor;
uint32_t blueColor;
•••
void setup() {
redColor = strip.Color(0,0,255);
blueColor = strip.Color(255,0,0);
•••
}
void FX1(){
strip.fill(red); // <== will be known because it's a global variable
strip.show();
}
void FX2(){
strip.fill(blue);
strip.show();
}
That being said, I agree with @PaulRB, I'm not too sure exactly what you want to achieve (various color palettes and you can switch dynamically?)
Here is the entire code. I defined the colors in the setup but they still show as undeclared in the other loops.
#include <Adafruit_LiquidCrystal.h>
#include <Adafruit_NeoPixel.h>
#define PIN 13
#define leds 24
Adafruit_NeoPixel strip = Adafruit_NeoPixel(leds, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_LiquidCrystal lcd_0(0);
int pb_pin = 3;
int state = 1;
bool pb;
bool pressed = false;
int pot_pin = A1;
int pot;
void setup()
{
pinMode(pb_pin, INPUT_PULLUP);
strip.begin();
Serial.begin(9600);
lcd_0.begin(20,4);
strip.clear();
uint32_t red = strip.Color(255, 0, 0); //Define the color red
uint32_t green = strip.Color(0, 255, 0); //Define the color green
uint32_t blue = strip.Color(0, 0, 255); //Define the color blue
uint32_t white = strip.Color(255, 255, 255); //Define the color white
}
void loop()
{
pb = digitalRead(pb_pin);
pot = analogRead(pot_pin);
pot = map(pot, 0, 1023, 255, 0);
if(pb == pressed){
state = state + 1;
Serial.println(state);
while(digitalRead(pb_pin) == pressed){delay(100);}
}
switch (state){
case 1:
FX1();
Serial.print("pot: ");
Serial.println(pot);
strip.setBrightness(pot);
break;
case 2:
FX2();
strip.setBrightness(pot);
break;
case 3:
FX3();
strip.setBrightness(pot);
break;
default:
strip.clear();
strip.show();
state = 0;
break;
}
}
void FX1(){
strip.clear();
strip.fill(red, 0, leds/3);
strip.fill(white, leds/3, leds/3);
strip.fill(blue, (leds/3)*2, leds/3);
strip.show();
}
void FX2(){
strip.clear();
strip.fill(green, 0, leds/4);
strip.fill(white, leds/4, leds/4);
strip.fill(green, (leds/4)*2, leds/4);
strip.fill(white, (leds/4)*3, leds/4);
strip.show();
}
void FX3(){
strip.clear();
strip.rainbow(0,1,255,255,true);
strip.show();
}
My mistake. I see where I messed up. thank you
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.