Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: Arduino with Ping sensor  (Read 1062 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

}
« Last Edit: February 28, 2011, 01:59:07 pm by Photoresistor » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 671
Posts: 51669
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
float distCalc()  {
}
Why is distCalc defined to return a float? Where is the return statement?

Code:
void loop()  {

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

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

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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;
 
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 671
Posts: 51669
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 671
Posts: 51669
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Full Member
***
Karma: 0
Posts: 129
That's me, the boy with the broken halo.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sounds like a good idea smiley
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 ^^
Logged

California
Offline Offline
Full Member
***
Karma: 0
Posts: 129
That's me, the boy with the broken halo.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: