Arduino line follower

Hi,

I'm doing a little line follower robot, and I'm having problems when uploading the code to the arduino mega2560.
Everything is working find while I have de usb wire connecte to my computer but when I disconnect it from arduino board, the robots starts doing crazy things, somebody can help me?
It's my first robot and I'm a bit lost.

Thanks

I have attached the code used

Main_prin.ino (13.2 KB)

How are you powering the Mega when it's not connected to USB?

long readPing1()
{
  // establish variables for duration of the ping, 
  // and the distance result in centimeters:
  long duration1, cm1;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin1, OUTPUT);
  digitalWrite(pingPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin1, HIGH);
  delayMicroseconds(15);
  digitalWrite(pingPin1, LOW);
  delayMicroseconds(20);
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin1, INPUT);
  duration1 = pulseIn(pingPin1, HIGH);
  // convert the time into a distance
  cm1 = microsecondsToCentimeters(duration1);
  Serial.print(cm1);
  Serial.print("cm");
  Serial.println();
  delay(10);
  return(cm1);  
}

You've got three near-identical functions - why?

positionOpto=(o1*0+o2*1000+o3*2000+o4*3000+o5*4000+o6*5000+o7*6000+o8*7000)/28000;

I can think of simpler ways of ignoring "o1".

When the usb is not connected I'm using a battery to supply voltage to the engines and the arduino board and sensors.

I have three similar functions because I'm using three ultrasonics sensors.

I have three similar functions because I'm using three ultrasonics sensors.

But it is the same code, the only difference is the Ping interface pin.

long readPing(const int pingPin)
{
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(15);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(20);
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  long duration = pulseIn(pingPin, HIGH);
  // convert the time into a distance
  long cm = microsecondsToCentimeters(duration);
  Serial.print(cm);
  Serial.println("cm");
  delay(10);
  return cm;  
}

When the usb is not connected I'm using a battery

What sort of battery?

Yes, the only difference is the pin. Can I ready three ultrasonic sensors with just one function?
The battery I'm using is a valve regulated lead acid battery about 12V, and for arduino and sensors I'm using a L7805 to provide 5 V.

Yes, the only difference is the pin. Can I ready three ultrasonic sensors with just one function?

Yes, you can, and unless you want to type and debug three functions instead of just one, you should.

Where are you feeding the 5V from the regulator to?

To the power supply connector on arduino board, and to the ultrasonic sensors. I have measured and all voltage values are correct. Do you this could be the problem that the program just work when it is the usb wire connected to my PC?

Difficult to say; just stating:

the robots starts doing crazy things

could mean lots of things - is the robot quoting Edwin Lear, or Jack Kerouac?
Performing a pas-de-deux?

I'd probably prop the robot up on blocks, run it off the battery and try to measure the 5V rail whilst it is operating.