Sketch works only when connected to pc

Please help me, the sketch works only when i connect the arduino to pc if i connect it to a 9v battery then the arduino starts but the code which i have uploaded does not implements it only implements it when i connect it to pc Please let me know what is wrong in the below sketch

#define LS 2 // left sensor
#define RS 3 // right sensor
#define LM1 5 // left motor M1a
#define LM2 4 // left motor M2a
#define RM1 7 // right motor M2a
#define RM2 6 // right motor M2b
void setup()
{
pinMode(LS, INPUT);
pinMode(RS, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
}
void loop()
{
if(digitalRead(LS) && digitalRead(RS)) // Move Forward on line
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
if(digitalRead(LS) && !(digitalRead(RS))) // turn left by rotationg left motors in forward and right ones in backward direction
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, HIGH);
}
if(!(digitalRead(LS)) && digitalRead(RS)) // Turn right by rotating right motors in forward and left ones in backward direction
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}

if(!(digitalRead(LS)) && !(digitalRead(RS))) // Finish line, stop both the motors
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}
}

You didn't provide us much to go on, nor any properly-posted code to look at, so giving you a genuinely-informed response is almost impossible.

However, my guess is it has nothing to do with your code.

silly_cone:
You didn't provide us much to go on, nor any properly-posted code to look at, so giving you a genuinely-informed response is almost impossible.

However, my guess is it has nothing to do with your code.

what information you need

The code looks fine. Without knowing what motor you're trying to drive or through what driver, my guess is you're not getting enough power from the battery. Those 9V batteries are not big on current.

DKWatson:
The code looks fine. Without knowing what motor you're trying to drive or through what driver, my guess is you're not getting enough power from the battery. Those 9V batteries are not big on current.

i have tried with a mobile charger .The motor am using is BO motor 150 rpm with a motor driver LM293D

Chargers, by default, don't supply much current so as not to overheat (and cause to explode) the battery they're charging. You'd be better of with a straight convertor rated at at least 500 mA. If you examine the LM293D you will note that there are two separate voltage inputs, one (5V) for the logic and the other (up to 12V) for the motor. If you try to run the motor off a digital pin, you are limited to about 20mA (the sheets say 40 but don't trust that). If you examine the Nano, there is a pin marked 5V between A7 and RST. This comes straight from the regulator and is not constrained by the processor thereby allowing for more current. It is however, never a recommended solution to drive any induction device directly from any pin on the board, they are not protected from the nasty world outside (back EMF and all that). Motors should always be powered by their own supply (making sure of common ground) and have de-coupling capacitors and protection diodes. Simple diagrams abound on the net and can be slapped together with about 25 cents worth of parts - cheap insurance. You can also high-side load the supply through a transistor (2N2222 would be fine) and use a pin connected to the base (through at least a 1k resistor) and use that as an on/off switch.