correct use of curly brackets - sketch review

Hi everyone,
I would like to submit my code for review.
The sketch compiles with no errors, but that doesn’t mean everything is ok.

There are 2 motion detectors as inputs( left and right), and 2 outputs to drive 2 lights (left and right) when motion is detected.
They stay on for a fixed time, and then they fade out.

There’s also an LDR used to read the ambient light and prevent the lights from turning on during daylight hours.

I’m away from home,no board to play with, so at the moment it’s just me and the IDE.

I did my best with the curly brackets, in particular with the last 2 sections (the activation of the lights, timer,and fading out) but I’m not sure I got it right.

Could you please review it and let me know if there’s anything wrong?

thanks

//
int SENSR_SX = 8; //
int SENSR_DX = 9; //
int OUT_SX = 10; //
int OUT_DX = 11; //
int LDR = A0;    //
int LDR_K = A1; //

//
int PWM_SX = 255;  //
int PWM_DX = 255;  //
int light = 0;     //
int light_set = 0; //
bool light_OK = 0; //
bool PIR_SX = 0;   //
bool PIR_DX = 0;   //

//
unsigned long startMillis;  //
unsigned long currentMillis_SX; //
unsigned long currentMillis_DX; //
const unsigned long timer = 25000;  //
int i = 0; //

void setup() {
  
  pinMode(SENSR_SX, INPUT);//
  pinMode(SENSR_DX, INPUT);//
  pinMode(OUT_SX, OUTPUT);
  pinMode(OUT_DX, OUTPUT);
  startMillis = millis();  //
}

void loop() {
  
  light = analogRead(LDR);       //
  light_set = analogRead(LDR_K); //
  if (light > light_set) {       //
    digitalWrite(light_OK, LOW); //
  }
  else if (light < light_set) {   //
    digitalWrite(light_OK, HIGH); //
  }

  PIR_SX = digitalRead(SENSR_SX);//
  if (PIR_SX == HIGH && light_OK == HIGH) { //
    analogWrite(OUT_SX, PWM_SX); //
    currentMillis_SX = millis();  //
    if (currentMillis_SX - startMillis >= timer)  //
      for (int i = PWM_SX; i > 0; i--) //
        analogWrite(OUT_SX, i); //
    delay(5);  //

  }

  PIR_DX = digitalRead(SENSR_DX);//
  if (PIR_DX == HIGH && light_OK == HIGH) { //
    analogWrite(OUT_DX, PWM_DX); //
    currentMillis_DX = millis();  //
    if (currentMillis_DX - startMillis >= timer)  //
      for (int i = PWM_DX; i > 0; i--) //
        analogWrite(OUT_DX, i); //
    delay(5);  //

  }
}
     for (int i = PWM_DX; i > 0; i--) //
        analogWrite(OUT_DX, i); //
    delay(5);  //

If you mean for there to be a 5 millisecond delay() between each value of i then you need to put the required code in curly brackets

      for (int i = PWM_DX; i > 0; i--) //
        {
          analogWrite(OUT_DX, i); //
          delay(5);
        }  //

I would go as far as to advise that you always put the code block associated with a for loop or a while in curly brackets even if there is only one code statement. I also favour each { and } going on its own line so that the code blocks are more easily visible

Styles regarding that may differ, though i think in this case at least the closing curly brace should be indented to the same as 'for' (i tend to put the opening curly braces on the same line as the condition, and if i leave the curly braces out because it is a single command, i also put them on that same line. But Bob's advice is pretty good, mainly be consistent !
So i write

  for (int i = PWM_DX; i > 0; i--)   {
    analogWrite(OUT_DX, i); //
    delay(5);
  }  //

to write what Bob wrote,
and

  for (int i = PWM_DX; i > 0; i--) analogWrite(OUT_DX, i); //
  delay(5);

to write what you wrote.

Thank you guys for the replies,

my understanding is that the curly are used to "force" the execution of the code within.

I can see why Bob is suggesting to add curly on the delay loop, since it will have to be performed several times, until the output is 0.

For the same reason, in my original code, I had enclosed between curly all the code that was switching on the PWM to the output, start the timer, wait until the end of the timer, and start ramping down the PWM until 0, since I wanted to be sure of the execution of the full set of instructions.

I can pretty much understand the use of curly but I don't feel really 100% confident.

Is there any general set of rules (for dummies :slight_smile: ) that you guys can share, in order to help me fully understand how and when to use them?

thanks

Simply put, curly brackets are used to group together a number of program statements that must be executed as a group.

Example include, but are not limited to, the code in a function, the dependent code of an if statement or while loop, the code of a switch statement and the dependant code of a for loop. They are also used to encompass the values assigned to an array when it is declared

A side effect of the use of a pair of curly brackets is to limit the scope of variables declared within them to the program statements in that block