Let's Make Robots!

ping programming problem

i am stuck once again i can't seem to figure out why my motors will activate when the ping reads closer than danger threshold, however it will not execute/motors won't activate in the next part of code when the sensor or ping reads farther than danger threshold.  what i am attempting to build is an obstacle avoidance rover.  if you have any ideas it would greatly appreciated..... smiley-lol

here is my code:

int pingPin = 11; //pin for ultrasound sensor

long duration; //time it takes to recieve PING signal

int dangerThresh = 10; //threshold for obstacles (in cm)

int E1 = 5;   //E1: PWM control for Motor1

int M1 = 4; //M1: Direction for Motor1 pin 4

int E2 = 6;   //E2: PWM control for Motor2                      

int M2 = 7;  //M2: Direction for Motor2 pin 7    

 

void setup()

  pinMode(M1, OUTPUT);   

  pinMode(M2, OUTPUT); 

}

 

void loop()

{

 int distanceFwd = ping();

 if (distanceFwd<dangerThresh) //if path is blocked

  {

  digitalWrite(M1,HIGH);

  digitalWrite(M2, LOW);

  analogWrite(E1, 80);   //PWM Speed Control

  analogWrite(E2,100);   //PWM Speed Control

  }

else  if  (distanceFwd>dangerThresh)//if path is clear

  {  

  digitalWrite(M1,HIGH);

  digitalWrite(M2, HIGH);

  analogWrite(E1, 150);   //PWM Speed Control

  analogWrite(E2, 150);   //PWM Speed Control

  }

}

long ping()

{

  // Send out PING))) signal pulse

  pinMode(pingPin, OUTPUT);

  digitalWrite(pingPin, LOW);

  delayMicroseconds(2);

  digitalWrite(pingPin, HIGH);

  delayMicroseconds(5);

  digitalWrite(pingPin, LOW);

 

  //Get duration it takes to receive echo

  pinMode(pingPin, INPUT);

  duration = pulseIn(pingPin, HIGH);

 

  //Convert duration into distance

  //return duration / 74 / 2; //inches

  return duration / 29 / 2; // centimeters

}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

i suggest your code is wrong ... and while waiting for someone with more experience than me you can try a few changes to where i think it is.

where path is blocked you have M1 HIGH and M2 LOW .... both should probably be HIGH or both LOW .... then where path is not blocked you have both M1 and M2 HIGH ..... so if there High for path blocked they should be low for path unblocked eg blocked high high unblocked low low or visa versa

also your motor control looks wrong too but not sure what needs to be changed.

try changing the High and lows .... im sure someone with more experiance will reply to your post with the proper answer ... but you can try this out while your waiting

may i also suggest you check out lumi robot code here http://letsmakerobots.com/node/26392   he has code attached ... youll find it at the top under the picture of the robot ... just dowload it and open it .... you could work from that .... when i was looking for code to make motors work and ping sensor work i searched through the site and found this .... im sure youll find it very useful and be able to get some advice if you get stuck. 

You set the pinMode of the motor pins, but, not the enable pins. Next, you declare distanceFwd as an int ( int distanceFwd = ping(); ) and then in the function you say it will return a long ( long ping() ).

My second point just means you have a variable declaration mismatch and could lead to truncation issues, which could massively affect your data (ie your distance measurement).