Code doesn't do it's job when I recycle power

I hope someone could help with this issue. when I upload the code to arduino, it works perfect: Turning 6 LEDs on and off when a pir is triggered but the issue is: once I unplug the USB and put it back again the circuit doesn’t do anything so I need to upload the code again for it to work.
What could be the problem: Is it the code or the arduino resetting itself or what?
I had this issue before, but with other project it worked fine.
Here is my code:

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);
    }
  }
}

How do you recycle power?!?

The code looks OK to me. Maybe there is a hardware issue? How is the circuit hooked up?

What Arduino are you using?

The code looks fine so I'd suspect some kind of hardware problem.

The most obvious reason for it not to do anything is that a digitalRead of pirPin is not returning HIGH.

The Serial port is invaluable as a tool to help debug problems like this. If you put Serial.begin(9600) you can then use Serial.println() elsewhere in your code to tell you what code is executing and what values your variables have. Look at the example sketches with serial output to see how this can be used.

PIRs often have open collector or open drain outputs. Try enabling the internal pullup:

pinMode(pirPin, INPUT); digitalWrite (pirPin, HIGH); // enable pullup resistor

I am using an Arduino Uno. When I upload the code the LEDs work as they are supposed to (when I come close to the pir. the pir pin turn high and LEDs start blinking. No problem :)) The problem is when I unplug the USB from the laptop and putting it back ; I need to upload the code again and make sure no one unplugs the USB. otherwise nothing works

Do you have any extra hardware connected to your UNO?

The PIR and the LEDs and wires that's it.

Maybe try a pull-down resistor?

I can't see why it doesn't start up when USB is plugged in but this statement doesn't look quite right:

    for(int pinled=6; pinled>=2; pinled --)

I think the initialization should be "int pinled=7;"

Pete

ferkheusa: The PIR and the LEDs and wires that's it.

Current limiting resistors?

what you mean current limiting resistor? I don't have that. You mean for each LED I need to put a current limiting resistor in the negative side and tie it to ground. Right?

ferkheusa:
You mean for each LED I need to put a current limiting resistor in the negative side and tie it to ground. Right?

Either side of the LED.

what you mean current limiting resistor? I don’t have that.

You have to have something in the circuit to limit the current passing through the LEDs. (Well, you don’t “have to” but the side-effect is that there is going to be permanent damage; most likely to the processor.) The simplest “something” is a resistor.

el_supremo:
This statement doesn’t look quite right:

    for(int pinled=6; pinled>=2; pinled --)

I think the initialization should be “int pinled=7;”

Only if he wants the 7 LED to blink both on the way up and on the way down. I think 6 is reasonable. Might even want to stop at 3 so the LED on 2 doesn’t blink twice.