Simplifying code for fading LED strips

Good day I am quite new to coding. I have spent much time searching for a general answer on how to accomplish what I am trying to do. I am looking to have 10 sections of WS2812 LED strip lights attached to my stairs. I want each stair to "fade" on one at a time stay on for a set time and then fade off.

I plan on the data line linked between each strip so that I can have one code for a single output pin.

I have written code that will fade each strip on and I can write it in reverse to turn each step off. Currently I only have 1 step worth of code written for turning on only.

I am hoping before I continue on I am hoping I might get some advise on simplifying this code. I am having a difficult time figuring out Brightness to increase the leds from 0 to 255. I am just looking for white light.

I know there has to be either "getbrightness" + X loop I can run of some variation of the setColor code where is just keeps adding X to what it is currently set to.

Any help is greatly appreciated, I am certainly not looking to have it written for me. But a string of code with explanation of what each sections does will allow me to adapt it to my needs and grow my abilities.

Bellow is my planned setup and current code. Thank you in advance.

#include <Adafruit_NeoPixel.h>
#define Pin 6
#define numLed 80
#define dval 39
#define dval2 (dval / 2)
Adafruit_NeoPixel led = Adafruit_NeoPixel(numLed, Pin);

void setup(){
  led.begin();
}

void loop(){
  Fade10();
    delay(dval);
  Fade20();
    delay(dval);
  Fade30();
    delay(dval);
  Fade40();
    delay(dval);
  Fade50();
    delay(dval);
  Fade60();
    delay(dval);
  Fade70();
    delay(dval);
  Fade80();
    delay(dval);
  Fade90();
    delay(dval);
  Fade100();
    delay(dval);
  Fade110();
    delay(dval);
  Fade120();
    delay(dval);
  Fade130();
    delay(dval);
  Fade140();
    delay(dval);
  Fade150();
    delay(dval);
  Fade160();
    delay(dval);
  Fade170();
    delay(dval);
  Fade180();
    delay(dval);
  Fade190();
    delay(dval);
  Fade200();
    delay(dval);
  Fade210();
    delay(dval);
  Fade220();
    delay(dval);
  Fade230();
    delay(dval);
  Fade240();
    delay(dval);
  Fade250();
    delay(dval2);
  Fade255();
}

void Fade10(){setColor(10,10,10);}
void Fade20(){setColor(20,20,20);}
void Fade30(){setColor(30,30,30);}
void Fade40(){setColor(40,40,40);}
void Fade50(){setColor(50,50,50);}
void Fade60(){setColor(60,60,60);}
void Fade70(){setColor(70,70,70);}
void Fade80(){setColor(80,80,80);}
void Fade90(){setColor(90,90,90);}
void Fade100(){setColor(100,100,100);}
void Fade110(){setColor(110,110,110);}
void Fade120(){setColor(120,120,120);}
void Fade130(){setColor(130,130,130);}
void Fade140(){setColor(140,140,140);}
void Fade150(){setColor(150,150,150);}
void Fade160(){setColor(160,160,160);}
void Fade170(){setColor(170,170,170);}
void Fade180(){setColor(180,180,180);}
void Fade190(){setColor(190,190,190);}
void Fade200(){setColor(200,200,200);}
void Fade210(){setColor(210,210,210);}
void Fade220(){setColor(220,220,220);}
void Fade230(){setColor(230,230,230);}
void Fade240(){setColor(240,240,240);}
void Fade250(){setColor(250,250,250);}
void Fade255(){setColor(255,255,255);}

void setColor(int red, int green, int blue){
  for (uint8_t i = 0; i < 8; i++){
    led.setPixelColor(i, led.Color(red ,green, blue));
  }
  led.show();
}

can be replaced with

void Fade(int x)
{
setColor(x,x,x);
}

Then loop like

for (int l = 10;l <=250;l += 10)
{
Fade(l);
delay(dval);
}
Fade(255);
1 Like

First thank you very much, I have been trying to figure how and what order to write this code for this very purpose. I do have some questions though.

first the line "for (int l" it did not seem to work with the 1 but changing it to a letter seems to fix it (changing it to the same letter in Fade(X) as well.

second the last line "Fade(255)" doesn't seem to be needed. what is or was its intended purpose? I see no difference with or without it, so I would love to understand if there is a need... does it stop it from calculating over 255? I assumed that is where "<=255" comes in.

Thank you again as that is a much more simplified.

Now is there a way to add to this to get it to do each set or stair's leds or am I looking to simplify it too much, and would be better off just repeating this code to for each step? if it is possible a simple yes is enough and I will try and figure it out.

Much appreciated.

The 'l' is a lower case L (I use it for shorthand for LOOP)

To get it to move up the LED string you would need another loop, stepping up by 8 each time, adding this index to your setColor with a bit of maths.

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