Lighting LED Strip from End to Start

Hi Guys,

New to the forum and Arduino units themselves. I’ve got a stair light project I’m working on and need a little bit of help if possible.

I’ve got the WS2801 Led strip to light up one by one from LED 0 to LED 159.

The code for this is

for(int COLOURLed = 0; COLOURLed < NUM_LEDS; COLOURLed = COLOURLed + 1) {
      // Turn our current led on to COLOUR, then show the leds
      leds[COLOURLed] = CRGB::COLOUR1;

      // Show the leds (only one of which is set to blue, from above)
      FastLED.show();

I assume it’s something in this line I need to change, but what I want to do is turn them on from the last LED so LED 159 to LED 0. I’ve tried changing the 0 to 159 but it doesn’t seem to work, I’ve also tried changing the +1 to -1 but couldn’t seem to get this to work either

for(int COLOURLed = 0; COLOURLed < NUM_LEDS; COLOURLed = COLOURLed + 1) {

Thanks,
Mark

I might be missing something, but this seems as easy as reversing your for statement.

for(int COLOURLed = NUM_LEDS; COLOURLed > 0; COLOURLed = COLOURLed - 1) {
      // Turn our current led on to COLOUR, then show the leds
      leds[COLOURLed] = CRGB::COLOUR1;

      // Show the leds (only one of which is set to blue, from above)
      FastLED.show();

for(int COLOURLed = NUM_LEDS; COLOURLed > 0; COLOURLed = COLOURLed - 1) { is not the reverse of

for(int COLOURLed = 0; COLOURLed < NUM_LEDS; COLOURLed = COLOURLed + 1) {

It should read

for(int COLOURLed = NUM_LEDS-1; COLOURLed >= 0; COLOURLed = COLOURLed - 1) {

Mark

Thanks, I'm really new to programming languages and just couldn't work out how I would get it to work the other way round, as in where I would change this to make it work backwards.

Will give it a try now!

Worked perfectly! Thanks so much been racking my head for the last hour.

Rather than starting a new thread I thought it may be best to use this one.

The setup is as follows

1 PIR Sensor Top of Stairs
1 PIR Sensor Bottom of Stairs
5m WS2801 LED Strip

When the bottom sensor goes HIGH it should light the LED strip from bottom to top. If the sensor goes low then turn them off from top to bottom. The top sensor should do the opposite.

With 1 PIR sensor this works perfectly, but I think it's something to do with my coding that it doesn't work with both. I've tried using 2 IF statements for if the PIR goes HIGH. I've also tried using IF and ELSE IF.

When I have both sensors connected, one PIR works fine but the other seems to either not work or when it does work it just turns them on and doesn't seem to go to LOW to turn them off. Either that or my code is completely wrong.

I have a couple of versions of the code, I've tried to make it neat and tidy but you may think otherwise! This is my first time coding with the Arduino. I've taken some code from the PIR example script on this site and some for the LEDs from FASTLED.IO

Shall I just post both scripts here or is there somewhere I could upload them to that may look better on the screen to try and read?

Thanks
Mark

You can attach the code to your message. The forum seems to accept *.ino files.