Hi!
I have an arduino duemilanove and a ping sensor.
I have been trying to make 2 DC motor switching directions using a L293dne dual H-bridge when a ultrasonic Ping sensor(from Parallex) gets closer to an object. In this case i want my motors to go forward( when mounted on a chassis) and if the Ping detects an object less than 15cm infront of it, it will turn around and continue. If the sensor don't get interrupted both motors shall go forward.
The problem is when i turn on the aruino, one of the motors doesn't spin around at all. Just making strange noises. The other motor work half of the time and switches direction sometimes.
I tried to make this code, but apparently it's not working proporly. I hope sombody can help me with this one, thank you

const int pingPin = 12;
const int motor1A = 6;
const int motor1B = 5;
const int enablePin = 9;
const int motor2A = 1;
const int motor2B = 3;
const int enable2Pin = 10;
float dist = 0;
void straight()
{
digitalWrite(enablePin, HIGH);
digitalWrite(enable2Pin, HIGH);
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}
void turnRight()
{
digitalWrite(enablePin, HIGH);
digitalWrite(enable2Pin, HIGH);
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}
float distCalc() {
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// 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(5);
digitalWrite(pingPin, LOW);
// 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);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
dist = microseconds / 29 / 2;
return dist;
}
void avoideWalls()
{
if(dist < 15) {
turnRight();
delay(100);
}
else {
straight();
delay(100);
}
}
void setup()
{
// initialize serial communication:
Serial.begin(9600);
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
pinMode(enable2Pin, OUTPUT);
straight();
}
void loop() {
distCalc();
avoideWalls();
delay(20);
}