Go Down

Topic: Need help with code (Read 253 times) previous topic - next topic

Goggakat

Hi

I would like to know if i can shorten the following code

I use the adafruit neo pixel lid for my WS2812b led strip
I have 32 leds in the strip

at the moment i set the color individually for each led and have 6 color settings

  pixels.setPixelColor(0, pixels.Color(127, 255, 0));                                     //medium brightness red
  pixels.setPixelColor(1, pixels.Color(0, 0, 127));                                     //medium brightness blue
  pixels.setPixelColor(2, pixels.Color(127, 255, 0));
  pixels.setPixelColor(3, pixels.Color(0, 0, 127));
  pixels.setPixelColor(4, pixels.Color(127, 255, 0));
  pixels.setPixelColor(5, pixels.Color(0, 0, 127));
  pixels.setPixelColor(6, pixels.Color(127, 255, 0));                                     
  pixels.setPixelColor(7, pixels.Color(0, 0, 127));                                     
  pixels.setPixelColor(8, pixels.Color(127, 255, 0));
  pixels.setPixelColor(9, pixels.Color(0, 0, 127));
  pixels.setPixelColor(10, pixels.Color(127, 255, 0));
  pixels.setPixelColor(11, pixels.Color(0, 0, 127));
  pixels.setPixelColor(12, pixels.Color(127, 255, 0));                                     
  pixels.setPixelColor(13, pixels.Color(0, 0, 127));                                   
  pixels.setPixelColor(14, pixels.Color(127, 255, 0));
  pixels.setPixelColor(15, pixels.Color(0, 0, 127));
  pixels.setPixelColor(16, pixels.Color(127, 255, 0));
  pixels.setPixelColor(17, pixels.Color(0, 0, 127));
  pixels.setPixelColor(18, pixels.Color(127, 255, 0));                                     
  pixels.setPixelColor(19, pixels.Color(0, 0, 127));                                     
  pixels.setPixelColor(20, pixels.Color(127, 255, 0));
  pixels.setPixelColor(21, pixels.Color(0, 0, 127));
  pixels.setPixelColor(22, pixels.Color(127, 255, 0));
  pixels.setPixelColor(23, pixels.Color(0, 0, 127));
  pixels.setPixelColor(24, pixels.Color(127, 255, 0));                                     
  pixels.setPixelColor(25, pixels.Color(0, 0, 127));                                     
  pixels.setPixelColor(26, pixels.Color(127, 255, 0));
  pixels.setPixelColor(27, pixels.Color(0, 0, 127));
  pixels.setPixelColor(28, pixels.Color(127, 255, 0));
  pixels.setPixelColor(29, pixels.Color(0, 0, 127));
  pixels.setPixelColor(30, pixels.Color(127, 255, 0));
  pixels.setPixelColor(31, pixels.Color(0, 0, 127));
  pixels.show();

Thanks

PaulRB

Quote
I would like to know if i can shorten the following code
Yes, that would be easy.
Quote
have 6 color settings
I see only 2. This makes me think that shortening the code you posted would be a waste of time, because that is only one example of many that you need to shorten. Can you post more examples that show the range of different patterns and colours that you need to use? Please use code tags for this. The forum guidelines in the sticky post will show you how.

Goggakat

I have 6 different settings for the leds that show different light settings at different times

Code: [Select]

void morning() {
  pixels.setPixelColor(0, pixels.Color(127, 255, 0));                                     //medium brightness red
  pixels.setPixelColor(1, pixels.Color(0, 0, 127));                                     //medium brightness blue
  pixels.setPixelColor(2, pixels.Color(127, 255, 0));
  pixels.setPixelColor(3, pixels.Color(0, 0, 127));
  pixels.setPixelColor(4, pixels.Color(127, 255, 0));
  pixels.setPixelColor(5, pixels.Color(0, 0, 127));
  pixels.setPixelColor(6, pixels.Color(127, 255, 0));
  pixels.setPixelColor(7, pixels.Color(0, 0, 127));
  pixels.setPixelColor(8, pixels.Color(127, 255, 0));
  pixels.setPixelColor(9, pixels.Color(0, 0, 127));
  pixels.setPixelColor(10, pixels.Color(127, 255, 0));
  pixels.setPixelColor(11, pixels.Color(0, 0, 127));
  pixels.setPixelColor(12, pixels.Color(127, 255, 0));
  pixels.setPixelColor(13, pixels.Color(0, 0, 127));
  pixels.setPixelColor(14, pixels.Color(127, 255, 0));
  pixels.setPixelColor(15, pixels.Color(0, 0, 127));
  pixels.setPixelColor(16, pixels.Color(127, 255, 0));
  pixels.setPixelColor(17, pixels.Color(0, 0, 127));
  pixels.setPixelColor(18, pixels.Color(127, 255, 0));
  pixels.setPixelColor(19, pixels.Color(0, 0, 127));
  pixels.setPixelColor(20, pixels.Color(127, 255, 0));
  pixels.setPixelColor(21, pixels.Color(0, 0, 127));
  pixels.setPixelColor(22, pixels.Color(127, 255, 0));
  pixels.setPixelColor(23, pixels.Color(0, 0, 127));
  pixels.setPixelColor(24, pixels.Color(127, 255, 0));
  pixels.setPixelColor(25, pixels.Color(0, 0, 127));
  pixels.setPixelColor(26, pixels.Color(127, 255, 0));
  pixels.setPixelColor(27, pixels.Color(0, 0, 127));
  pixels.setPixelColor(28, pixels.Color(127, 255, 0));
  pixels.setPixelColor(29, pixels.Color(0, 0, 127));
  pixels.setPixelColor(30, pixels.Color(127, 255, 0));
  pixels.setPixelColor(31, pixels.Color(0, 0, 127));
  pixels.show();
}
void noon() {
  pixels.setPixelColor(0, pixels.Color(255, 255, 255));                                 //full brightness white
  pixels.setPixelColor(1, pixels.Color(255, 255, 255));                                      //medium brightness blue
  pixels.setPixelColor(2, pixels.Color(255, 255, 255));
  pixels.setPixelColor(3, pixels.Color(0, 0, 127));
  pixels.setPixelColor(4, pixels.Color(255, 255, 255));
  pixels.setPixelColor(5, pixels.Color(255, 255, 255));
  pixels.setPixelColor(6, pixels.Color(255, 255, 255));
  pixels.setPixelColor(7, pixels.Color(255, 255, 255));
  pixels.setPixelColor(8, pixels.Color(255, 255, 255));
  pixels.setPixelColor(9, pixels.Color(255, 255, 255));
  pixels.setPixelColor(10, pixels.Color(0, 0, 127));
  pixels.setPixelColor(11, pixels.Color(255, 255, 255));
  pixels.setPixelColor(12, pixels.Color(255, 255, 255));
  pixels.setPixelColor(13, pixels.Color(255, 255, 255));
  pixels.setPixelColor(14, pixels.Color(255, 255, 255));
  pixels.setPixelColor(15, pixels.Color(255, 255, 255));
  pixels.setPixelColor(16, pixels.Color(255, 255, 255));
  pixels.setPixelColor(17, pixels.Color(0, 0, 127));
  pixels.setPixelColor(18, pixels.Color(255, 255, 255));
  pixels.setPixelColor(19, pixels.Color(255, 255, 255));
  pixels.setPixelColor(20, pixels.Color(255, 255, 255));
  pixels.setPixelColor(21, pixels.Color(255, 255, 255));
  pixels.setPixelColor(22, pixels.Color(255, 255, 255));
  pixels.setPixelColor(23, pixels.Color(255, 255, 255));
  pixels.setPixelColor(24, pixels.Color(0, 0, 127));
  pixels.setPixelColor(25, pixels.Color(255, 255, 255));
  pixels.setPixelColor(26, pixels.Color(255, 255, 255));
  pixels.setPixelColor(27, pixels.Color(255, 255, 255));
  pixels.setPixelColor(28, pixels.Color(255, 255, 255));
  pixels.setPixelColor(29, pixels.Color(255, 255, 255));
  pixels.setPixelColor(30, pixels.Color(255, 255, 255));
  pixels.setPixelColor(31, pixels.Color(0, 0, 127));
  pixels.show();
}
void afterNoon() {
  pixels.setPixelColor(0, pixels.Color(0, 0, 255));                                     //full brightness blue
  pixels.setPixelColor(1, pixels.Color(0, 0, 255));
  pixels.setPixelColor(2, pixels.Color(0, 0, 255));
  pixels.setPixelColor(3, pixels.Color(0, 0, 255));
  pixels.setPixelColor(4, pixels.Color(0, 0, 255));
  pixels.setPixelColor(5, pixels.Color(0, 0, 255));
  pixels.setPixelColor(6, pixels.Color(0, 0, 255));
  pixels.setPixelColor(7, pixels.Color(0, 0, 255));
  pixels.setPixelColor(8, pixels.Color(0, 0, 255));
  pixels.setPixelColor(9, pixels.Color(0, 0, 255));
  pixels.setPixelColor(10, pixels.Color(0, 0, 255));
  pixels.setPixelColor(11, pixels.Color(0, 0, 255));
  pixels.setPixelColor(12, pixels.Color(0, 0, 255));
  pixels.setPixelColor(13, pixels.Color(0, 0, 255));
  pixels.setPixelColor(14, pixels.Color(0, 0, 255));
  pixels.setPixelColor(15, pixels.Color(0, 0, 255));
  pixels.setPixelColor(16, pixels.Color(0, 0, 255));
  pixels.setPixelColor(17, pixels.Color(0, 0, 255));
  pixels.setPixelColor(18, pixels.Color(0, 0, 255));
  pixels.setPixelColor(19, pixels.Color(0, 0, 255));
  pixels.setPixelColor(20, pixels.Color(0, 0, 255));
  pixels.setPixelColor(21, pixels.Color(0, 0, 255));
  pixels.setPixelColor(22, pixels.Color(0, 0, 255));
  pixels.setPixelColor(23, pixels.Color(0, 0, 255));
  pixels.setPixelColor(24, pixels.Color(0, 0, 255));
  pixels.setPixelColor(25, pixels.Color(0, 0, 255));
  pixels.setPixelColor(26, pixels.Color(0, 0, 255));
  pixels.setPixelColor(27, pixels.Color(0, 0, 255));
  pixels.setPixelColor(28, pixels.Color(0, 0, 255));
  pixels.setPixelColor(29, pixels.Color(0, 0, 255));
  pixels.setPixelColor(30, pixels.Color(0, 0, 255));
  pixels.setPixelColor(31, pixels.Color(0, 0, 255));
  pixels.show();
}

PaulRB

#3
Jul 10, 2018, 10:31 am Last Edit: Jul 10, 2018, 10:42 am by PaulRB
Ok, that is why I asked for more examples. Your other two above don't match the pattern of the first one. I guess the other 3 will be equally different?

Here is an idea. Use strings to represent the patterns. Use letters/digits/other characters to represent the colours. So for example, your examples could be represented as
Code: [Select]

char morning[33] = "rbrbrbrbrbrbrbrbrbrbrbrbrbrbrbrb";
char noon[33] = "WWWbWWWbWWbWWWWWWWWbWWWWWWbWWWWb";
char afternoon[33] = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";

You would then write a function to set the LEDs, which you would call like this:
Code: [Select]

setLeds(morning);

The setLeds() function would contain a for() loop which would set each led individually based on the character at the corresponding position in the string. You would also write a function which would turn a character back into a colour, possibly containing a switch statement.

Go Up