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