IF Statement Not Executing Fully (For loop inside it executes great)

I have double and 138x checked my brackets and they check out good.

So this has been going great, having so much fun learning from everyone in these forums.

My code is making each stair light up an adjacent HUE value which works great. It's setup to use motion sensors from the top and from the bottom of the stairs.

Problem is I cannot for the life of me (after researching for a few hours) figure out why my IF statement will not execute all of the code with its {}.

It executes the FOR loops perfectly, but disregards what is after the FOR loop still inside the IF statement.

I feel it is something stupid which is why I googled for a while before posting :frowning:

It's not executing my commented parts "//XXXXXXXXXXXX"

#include "FastLED.h"
#define DATA_PIN 7
#define NUM_LEDS 120 //how many leds on the strip
#define ledgroup 10 //how many leds per stair

int brightness = 0;
CRGB leds[NUM_LEDS];
int alarmPinTop = 10;        // PIR at the top of the stairs
int alarmPinBottom = 8;     // PIR at the bottom of the stairs
int alarmValueTop = LOW;    // Variable to hold the PIR status
int alarmValueBottom = LOW; // Variable to hold the PIR status

void setup()
{
  //Serial.begin(9600);
 // Serial.println("resetting");

  //int ledgroup=2;
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  LEDS.setBrightness(155);
}

void loop()
{

  alarmValueTop = digitalRead(alarmPinTop);    // Constantly poll the PIR at the top of the stairs
  //Serial.println(alarmPinTop);
  alarmValueBottom = digitalRead(alarmPinBottom);  // Constantly poll the PIR at the bottom of the stairs
  //Serial.println(alarmPinBottom);

  //Serial.println(alarmValueTop);

  static int hue = 0;
  if (alarmValueTop == HIGH)
  {
    for (int dot = 0 ; dot <= NUM_LEDS ; dot = (dot + ledgroup)) //this is how many steps in the staircase
    {
      hue = (25 / (NUM_LEDS / ledgroup)) * dot; //increase hue with each stair

      for (int i = 0; i < ledgroup; i++) //this lights up each LED on that specific stair
      {
        //leds[dot-i] = CRGB::Red;
        leds[dot + i] = CHSV(hue, 255, 255);
      }

      FastLED.show();
      delay(500);
    }
    delay(1000); //XXXXXXXXXXXX
    FastLED.clear();  //XXXXXXXXXXXX
    FastLED.show(); //XXXXXXXXXXXX
    delay(2000); //XXXXXXXXXXXX
  }

  if (alarmValueBottom == HIGH)
  {
    Serial.println("bottom pin detected");
    for (int dot = NUM_LEDS ; dot >= 0 ; dot = (dot - ledgroup)) //this is how many steps in the staircase
    {
      hue = NUM_LEDS / ledgroup * dot / 10 * 1.75; //decrease hue with each stair

      //Serial.println(hue);

      for (int i = 0; i < ledgroup; i++) //this lights up each LED on that specific stair
      {
        //leds[dot-i] = CRGB::Red;
        leds[dot - i] = CHSV(hue, 255, 255);
      }

      FastLED.show();
      delay(500);

    }

  }

  FastLED.clear();  //XXXXXXXXXXXX
  FastLED.show(); //XXXXXXXXXXXX


}

Thank you so much for any help, I'm having so much fun :slight_smile:

Travisbklein:
It's not executing my commented parts "//XXXXXXXXXXXX"

Are you sure the code you have indicated as not working actually works?

Starting at line 79, your code is as follows:

  FastLED.clear();  //XXXXXXXXXXXX
  FastLED.show(); //XXXXXXXXXXXX


}

Are you sure that FastLED.clear() & FastLED.show() do something? Have you tested that?

The last '}' of your program is the end of the loop, so those two statements are outside of the IF portion of your code, therefore they must execute, or your loop doesn't repeat. Is the loop repeating?

I see you have several Serial.print() statements in your code that are commented out. So you have used those statements to check for data values, right?

So use the same strategy to check that the code is executing. In the above example, I would put a print statement after the FastLED.clear() statement. Something simple like -

Serial.println("FastLED.clear - executed")

Placed after the FastLED.clear() statement would tell you in the serial console if the code is executing.

If the print statement works, then the FastLED.clear() executed but it had no effect. So now you would know the code is working, it's just not having any effect. Now you have narrowed down the problem.

If what I just said works, the print statements and all, (and I'm 99.5% sure you will see the output in the serial monitor), next up would be to test the statements that don't work. Maybe, if you haven't already, write the code to just cycle the lights and make sure that works, regardless of input.

Hope this helps,
Randy

I see nothing wrong with the code. You have to be a bit mode specific in describing your problem.

The " } " on line 77 closes the if block.

#include "FastLED.h"
#define DATA_PIN 7
#define NUM_LEDS 120 //how many leds on the strip
#define ledgroup 10 //how many leds per stair

int brightness = 0;
CRGB leds[NUM_LEDS];
int alarmPinTop = 10;        // PIR at the top of the stairs
int alarmPinBottom = 8;     // PIR at the bottom of the stairs
int alarmValueTop = LOW;    // Variable to hold the PIR status
int alarmValueBottom = LOW; // Variable to hold the PIR status

void setup()
{
  //Serial.begin(9600);
 // Serial.println("resetting");

  //int ledgroup=2;
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  LEDS.setBrightness(155);
}

void loop()
{

  alarmValueTop = digitalRead(alarmPinTop);    // Constantly poll the PIR at the top of the stairs
  //Serial.println(alarmPinTop);
  alarmValueBottom = digitalRead(alarmPinBottom);  // Constantly poll the PIR at the bottom of the stairs
  //Serial.println(alarmPinBottom);

  //Serial.println(alarmValueTop);

  static int hue = 0;
  if (alarmValueTop == HIGH)
  {
    for (int dot = 0 ; dot <= NUM_LEDS ; dot = (dot + ledgroup)) //this is how many steps in the staircase
    {
      hue = (25 / (NUM_LEDS / ledgroup)) * dot; //increase hue with each stair

      for (int i = 0; i < ledgroup; i++) //this lights up each LED on that specific stair
      {
        //leds[dot-i] = CRGB::Red;
        leds[dot + i] = CHSV(hue, 255, 255);
      } // for

      FastLED.show();
      delay(500);
    } // for
    delay(1000); //XXXXXXXXXXXX
    FastLED.clear();  //XXXXXXXXXXXX
    FastLED.show(); //XXXXXXXXXXXX
    delay(2000); //XXXXXXXXXXXX
  } // if

  if (alarmValueBottom == HIGH)
  {
    Serial.println("bottom pin detected");
    for (int dot = NUM_LEDS ; dot >= 0 ; dot = (dot - ledgroup)) //this is how many steps in the staircase
    {
      hue = NUM_LEDS / ledgroup * dot / 10 * 1.75; //decrease hue with each stair

      //Serial.println(hue);

      for (int i = 0; i < ledgroup; i++) //this lights up each LED on that specific stair
      {
        //leds[dot-i] = CRGB::Red;
        leds[dot - i] = CHSV(hue, 255, 255);
      }

      FastLED.show();
      delay(500);

    } // for

  } // if <<<<<<<<<<<<<<<<<<<<<<<<<<<

  FastLED.clear();  //XXXXXXXXXXXX
  FastLED.show(); //XXXXXXXXXXXX


} // loop

The lines you indicated are not inside an if block, they should execute on every loop.

JCA34F:
The lines you indicated are not inside an if block, they should execute on every loop.

Yep that's the problem, anything outside of the IF statement is not executed, and I can't figure out why

I need to test this tomorrow I will post back. It's really weird I was baffled by it happening.