PIR: For loop inside an if statement

My for loop to turn 7 pins on and off in sequence works great but when I put that inside and if statement so I can control these pins using a small pir sensor, it doesn't work.
The actual code I am using is as follow; Could you, please, provide some suggestions. Thanks

int DL= 20;
int val = 0;
int pirPin = 8;
void setup() {
  for(int pinled =2; pinled <=7; pinled=pinled + 1)
    pinMode(pinled, OUTPUT);
  pinMode(pirPin, INPUT); 
}
void loop()
{
val = digitalRead(pirPin);

if(HIGH == val){
    for(int pinled=2; pinled<=7; pinled ++)
    {
      digitalWrite(pinled, HIGH);
      delay(DL);
      digitalWrite(pinled,LOW);
    }
    for(int pinled=6; pinled>=2; pinled --)
    {
      digitalWrite(pinled, HIGH);
      delay(DL);
      digitalWrite(pinled,LOW);
    }
  }
  else
  {
    for(int pinled=2; pinled<=7; pinled ++)
    {
      digitalWrite(pinled, LOW);
      delay(DL);
      digitalWrite(pinled,LOW);
    }

    for(int pinled=6; pinled>=2; pinled --)
    {
      digitalWrite(pinled, LOW);
      delay(DL);
      digitalWrite(pinled,LOW);
    }
 }
}

You want to say
val = digitalRead(...
not
val == digitalRead(...

I am sorry, that was just a typo recently. Yeah, I know that's an assignment. You are right but that doesn't solve the issue.

Maybe you could tell us what doesn't work, so we don't have to guess. How many other "typos" are there so we don't have to go down too many rabbit holes?

In what way does it not work? What results do you expect, and what results do you get?

We don't have crystal balls (at least, mine aren't), so we don't know what it is you want the sketch to achieve.

Is this intentional:

      digitalWrite(pinled, LOW);
      delay(DL);
      digitalWrite(pinled,LOW);

Because it seems kind of pointless if it is. Perhaps one of them should be 'HIGH'? :slight_smile:

When i tested the for loop by itself, it worked fine. It turns the first LED on then off and moves to next one that 's why i have that delay in my code.
but when I tried putting the for loop inside the if statement because I am using a pir sensor to trigger the LEDs when motion is sensed, it wouldn't work.
I wanted to say: when the output pin of the pir is high do the for loop which just turns the LEDs on and off in sequence as I explained else turn them off.
I hope I clarified a little bit.
Thank you

Do you know if the PIR sensor is working right? Why not add some Serial.println() calls in there to debug your sketch and see what is happening and when...

Let's look at the code anyway...

My first comment:

  else
  {
    for(int pinled=2; pinled<=7; pinled ++)
    {
      digitalWrite(pinled, LOW);
      delay(DL);
      digitalWrite(pinled,LOW);
    }

    for(int pinled=6; pinled>=2; pinled --)
    {
      digitalWrite(pinled, LOW);
      delay(DL);
      digitalWrite(pinled,LOW);
    }
 }

You're looping through the pins, and for each one you turn it off, then you delay, then you turn it off. Then you go through them all again in a different order and turn them all off... twice...

Why?

That whole section is pointless, as the section of code when the sensor is HIGH would have left all the LEDs off anyway. You don't need anything from the "else" to the end of the if construct.

if your 'if' statement isn't going True/high in this code, first check if the digitalRead at 8 is even going high or not when u need it . just turn on a single led may be , other than that the thing looks fine.

Yes i tested the pir with a working code and it works very good.

I added that code to make sure the LEDs stay off.
I guess I don't need it.

ferkheusa:
I added that code to make sure the LEDs stay off.
I guess I don't need it.

That's like switching off the light in your room by first flicking the switch to "off", then taking the bulb out and finally ripping the socket out of the ceiling.

Once an output is off it is off - you don't need to keep reminding it.

HAHAHAH.
I like how you explained it.