Go Down

Topic: Arduino with Ping sensor (Read 1 time) previous topic - next topic

Feb 28, 2011, 07:56 pm Last Edit: Feb 28, 2011, 07:59 pm by Photoresistor Reason: 1
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 :)

Code: [Select]
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);

}

PaulS

Code: [Select]
float distCalc()  {
}

Why is distCalc defined to return a float? Where is the return statement?

Code: [Select]
void loop()  {

distCalc();
avoideWalls();
delay(20);

}

avoideWalls() looks at the value of dist. Where does dist get a value?

i thought dist got value from this, and return as well right under:

Code: [Select]
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;

}

PaulS

I missed that. It's very confusing having functions that return a value also set a global variable.

Thanks for your reply, but do you see anything that i can change to make it work?

PaulS

Quote
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.


Quote
Thanks for your reply, but do you see anything that i can change to make it work?

I'm not sure what software changes you expect will be able to make the hardware work properly.

Without the ping sensor in the sketch, do the motors work properly?

I tested another sketch with a pushbutton, and the motors worked just fine. They switched direction when the button was pressed down so I don't think the motors are the problem.

Schmidtn

Can you make your Ping do something simpler, like turn on an LED if it's within 15cm of an object?  Maybe get the motors working in one sketch, like with your push button, then get the Ping working in another Sketch, like with an LED and then work on combining the two sketches?

Sounds like a good idea :)
I have already tried to combine some different sketches, to get a value from the ping and another sketch to control the motor with the H-bridge. I can try your example first and see if I get any positive results ^^

Schmidtn

Yeah, that's how I normally troubleshoot sketches that aren't working out for me... but then again I make A LOT of silly little errors that lead to minor breakdowns.  So far I haven't thrown my Arduino but I know some day I probably will. :smiley-red:

Yeah ^^
Some small faults could lead to a big problem. That's what making programming hard in some cases.

Go Up