Object Avoiding car !! Please Help

4 ping + arduino + motor shiel + 2 DC motors ( one for back and forward and one for steering). This are the codes for the ping sensors to give me a reeding

const int pingPin9 = 9;
const int pingPin8 = 8;
const int pingPin3 = 3;
const int pingPin2 = 2;

void setup()
{
  Serial.begin(9600);

}
//declar pini ce sunt 
void loop()
{
  ping2();
  delay(500);
  ping8();
  delay(500);
  ping3();
  delay(500);
  ping9();
  delay(500);
}
//viteza de rulare a algoritmului pt diferiti senzori sau motor DC
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void ping9()
{
  long duration9, cm9;
  pinMode(pingPin9, OUTPUT);
  digitalWrite(pingPin9, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin9, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin9, LOW);
  pinMode(pingPin9, INPUT);
  duration9 = pulseIn(pingPin9, HIGH);
  cm9 = microsecondsToCentimeters(duration9);
  Serial.print("sensorFataDreapta = ");
  Serial.print(cm9);
  Serial.print("cm");
  Serial.println();
}

void ping8()
{
  long duration8, cm8;
  pinMode(pingPin8, OUTPUT);
  digitalWrite(pingPin8, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin8, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin8, LOW);
  pinMode(pingPin8, INPUT);
  duration8 = pulseIn(pingPin8, HIGH);
  cm8 = microsecondsToCentimeters(duration8);
  Serial.print("sensorFataStanga = ");
  Serial.print(cm8);
  Serial.print("cm");
  Serial.println();
} 
void ping3()
{
  long duration3, cm3;
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);
  pinMode(pingPin3, INPUT);
  duration3 = pulseIn(pingPin3, HIGH);
  cm3 = microsecondsToCentimeters(duration3);
  Serial.print("sensorFata = ");
  Serial.print(cm3);
  Serial.print("cm");
  Serial.println();
} 
void ping2()
{
  long duration2, cm2;
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);
  pinMode(pingPin2, INPUT);
  duration2 = pulseIn(pingPin2, HIGH);
  cm2 = microsecondsToCentimeters(duration2);
  Serial.print("sensorSpate = ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
}

And this is for the motors work ( front, back , left , right etc)

const int pingPin9 = 9;
const int pingPin8 = 8;
const int pingPin3 = 3;
const int pingPin2 = 2;

void setup()
{
  Serial.begin(9600);

}
//declar pini ce sunt 
void loop()
{
  ping2();
  delay(500);
  ping8();
  delay(500);
  ping3();
  delay(500);
  ping9();
  delay(500);
}
//viteza de rulare a algoritmului pt diferiti senzori sau motor DC
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void ping9()
{
  long duration9, cm9;
  pinMode(pingPin9, OUTPUT);
  digitalWrite(pingPin9, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin9, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin9, LOW);
  pinMode(pingPin9, INPUT);
  duration9 = pulseIn(pingPin9, HIGH);
  cm9 = microsecondsToCentimeters(duration9);
  Serial.print("sensorFataDreapta = ");
  Serial.print(cm9);
  Serial.print("cm");
  Serial.println();
}

void ping8()
{
  long duration8, cm8;
  pinMode(pingPin8, OUTPUT);
  digitalWrite(pingPin8, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin8, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin8, LOW);
  pinMode(pingPin8, INPUT);
  duration8 = pulseIn(pingPin8, HIGH);
  cm8 = microsecondsToCentimeters(duration8);
  Serial.print("sensorFataStanga = ");
  Serial.print(cm8);
  Serial.print("cm");
  Serial.println();
} 
void ping3()
{
  long duration3, cm3;
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);
  pinMode(pingPin3, INPUT);
  duration3 = pulseIn(pingPin3, HIGH);
  cm3 = microsecondsToCentimeters(duration3);
  Serial.print("sensorFata = ");
  Serial.print(cm3);
  Serial.print("cm");
  Serial.println();
} 
void ping2()
{
  long duration2, cm2;
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);
  pinMode(pingPin2, INPUT);
  duration2 = pulseIn(pingPin2, HIGH);
  cm2 = microsecondsToCentimeters(duration2);
  Serial.print("sensorSpate = ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
}

i want to make the Car that avoids objects. usigin the ping sensors , Can any one help me ? Please. Thank you very much

This is the Car

I've not checked the validity of the code you posted (I assume it works?), but all you really need is to check the values from the sensors in your loop() then some simple logic to direct the car towards the sensor with the greatest distance, and then if the front sensors are all less than a certain value reverse out and turn to direct him away. For my robot that does something similar but with IR I detect if it's stuck (ie reversing hasn't improved the situation for a set number of iterations) then instead of continuing to reverse I "wriggle" him out - he might be stuck on something that requires more than a simple reverse action.

G.

Am I missing something or is there more to your question than: "Can someone tell me how to do this?"

Wow, awesome job on the vehicle!

I'm sorry I can't read your code and tell you if it will work or not. I'd probably need an identical vehicle so I could run it and see how it behaves. Can I suggest you start small on your testing? I'd try to start with simple things and build up.

  • Can you verify all your rangefinders work? Can you print an accurate distance measurement onto the serial monitor for every sensor? Can you move your hand around the car and see the sensors are responding as you expect?
  • Can you control the car on its own, while ignoring the sensors? Can you make it go straight? Does it go left when you want it to go left? It may seem basic, but you do need to make sure you have polarity correct on your motors. You don't want to spend a week chasing a software issue that's a simple "put the wires on backwards" issue
  • And so on. Combine each thing and build up. Will it turn left when it detects an obstacle on the right? Etc.

You could potentially run into some difficult issues, like blind spots between your forward sensors. Or you could find your turning circle isn't enough to avoid a wall - it turns, but not fast enough. If you try to get the whole thing working in one "big bang" then you're unlikely to succeed. Baby steps, my friend! 8)

Can you verify all your rangefinders work? Can you print an accurate distance measurement onto the serial monitor for every sensor? Can you move your hand around the car and see the sensors are responding as you expect?

  • Yes they work fine on theyre on :slight_smile:

Can you control the car on its own, while ignoring the sensors? Can you make it go straight? Does it go left when you want it to go left? It may seem basic, but you do need to make sure you have polarity correct on your motors. You don't want to spend a week chasing a software issue that's a simple "put the wires on backwards" issue

  • Yes the vehicle works on his on, on the second schetch i publisht.

I don't know how to use the Ping reedings to control the car. How can i combine this 2 schetches :expressionless: give me some code example plese :expressionless:

Do you realise you posted the same code twice?