Get Motor respons From Ping Sensor

Hy i have a project going on, and i need some help.
I have connected 4 ping sensors to a arduino uno and a motor shield, and i have created 1 schetch for the ping sensors to see they ar working and one for the motors, now i wanth them to make the motors respons from the ping sensors. ( it's a Object avoiding robot) This is the code for ping sensors

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 the code for the motors

int E1 = 7; 
int M1 = 6; 
int E2 = 4; 
int M2 = 5; 

void setup() 
  { 
    Serial.begin(9600);
  pinMode(M1, OUTPUT); 
  pinMode(M2, OUTPUT); 
  }
  
  void loop()
  
  {
Front();
delay(700);
FrontRight();
delay(700);
FrontLeft();
delay(700);
Back();
delay(700);
BackRight();
delay(700);
BackLeft();
delay(700);
Right();
delay(700);
Left();
delay(700);

  }
  
  
  
void Front()
{ 
digitalWrite(M1,HIGH); 
digitalWrite(M2,LOW);
analogWrite(E1, 0); //PLL Speed Control 
analogWrite(E2, 128); //PLL Speed Control 
} 
void Back()
{ 
digitalWrite(M1,HIGH); 
digitalWrite(M2,HIGH);
analogWrite(E1, 0); //PLL Speed Control 
analogWrite(E2, 128); //PLL Speed Control 
} 
void Right()
{ 
digitalWrite(M1,HIGH); 
digitalWrite(M2,HIGH);
analogWrite(E1, 128); //PLL Speed Control 
analogWrite(E2, 0); //PLL Speed Control 
} 
void Left()
{
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 0);
}
void FrontRight()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,LOW);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void FrontLeft()
{
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void BackRight()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void BackLeft()
{
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 128);
}

i want : if the one of the ping sensors read a specific distent the motor to respons with an action ( stop, go, revers, frontRight etc.) Please give me something to work with:) .Thankyou very much :slight_smile:

First, look at the code for the pingN() functions. Notice that the only difference between ping9 and ping8 is the local variable names (cm9 vs. cm8, etc.) and the pin number to read from. Get rid of three of the functions, and make the pin number an argument.

void ping(int pinNumToReadFrom, char *prefix)
{
  long duration, cm;
  pinMode(pinNumToReadFrom, OUTPUT);
  digitalWrite(pinNumToReadFrom, LOW);
  delayMicroseconds(2);
  digitalWrite(pinNumToReadFrom, HIGH);
  delayMicroseconds(5);
  digitalWrite(pinNumToReadFrom, LOW);
  pinMode(pinNumToReadFrom, INPUT);
  duration = pulseIn(pinNumToReadFrom, HIGH);
  cm = microsecondsToCentimeters(duration);
  Serial.print(prefix);
  Serial.print(" = ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
}

Then, call this function in loop:

void loop()
{
  ping(pingPin2, "sensorFataDreapta");
  delay(500);
  ping(pingPin8, "whatever...");
  delay(500);
<snip>
}

Next, notice that the function does not return anything that the robot can use. Change the function again:

long ping(int pinNumToReadFrom)
{
  long duration, cm;
  pinMode(pinNumToReadFrom, OUTPUT);
  digitalWrite(pinNumToReadFrom, LOW);
  delayMicroseconds(2);
  digitalWrite(pinNumToReadFrom, HIGH);
  delayMicroseconds(5);
  digitalWrite(pinNumToReadFrom, LOW);
  pinMode(pinNumToReadFrom, INPUT);
  duration = pulseIn(pinNumToReadFrom, HIGH);
  cm = microsecondsToCentimeters(duration);
  return cm;
}

And, change how you call it:

void loop()
{
  long dist2 = ping(pingPin2);
  delay(500);
  long dist8 = ping(pingPin8);
  delay(500);
<snip>
}

Now, you have something that can be used to make decisions.

The next step is to decide what the robot should do, based on the values obtained from each sensor. When you know what the actions are, you can combine the code from the "get me a value" sketch and the "do something with that value" sketch, to make a useful sketch.

Thank you , very much :slight_smile: i will try to work things out and publish the results :slight_smile: by the way wath means at the end of the ending schetch?

If you are a gentleman will you please show me a short schetch with the ping and motor respons, i have problems with the "IF" statement i canot see to do it :frowning: Please help

by the way wath means at the end of the ending schetch?

It just means that I snipped some of the code that was there, because what I left illustrated the point well enough.

l

ong distLeft = ping(pinLeft);
long distRight = ping(pinRight);
if(distLeft > distRight)
   turnLeft();
else
   turnRight();

Sory i'm hard head ok i'd made the part of the code but it does nothing i thing

long ping(int pinNumToReadFrom)
{
  long duration, cm;
  pinMode(pinNumToReadFrom, OUTPUT);
  digitalWrite(pinNumToReadFrom, LOW);
  delayMicroseconds(2);
  digitalWrite(pinNumToReadFrom, HIGH);
  delayMicroseconds(5);
  digitalWrite(pinNumToReadFrom, LOW);
  pinMode(pinNumToReadFrom, INPUT);
  duration = pulseIn(pinNumToReadFrom, HIGH);
  cm = microsecondsToCentimeters(duration);
  return cm;
}

"pinNumToReadFrom" i don't have this declared anywere :expressionless: Sry pauls i really need to get it done because is a school project that's way i'm so insistent :expressionless:

"pinNumToReadFrom" i don't have this declared anywere

long ping(int [color=orange]pinNumToReadFrom[/color])
{
  long duration, cm;
  pinMode([color=orange]pinNumToReadFrom[/color], OUTPUT);
  digitalWrite([color=orange]pinNumToReadFrom[/color], LOW);
  delayMicroseconds(2);
  digitalWrite([color=orange]pinNumToReadFrom[/color], HIGH);
  delayMicroseconds(5);
  digitalWrite([color=orange]pinNumToReadFrom[/color], LOW);
  pinMode(p[color=orange]inNumToReadFrom[/color], INPUT);
  duration = pulseIn([color=orange]pinNumToReadFrom[/color], HIGH);
  cm = microsecondsToCentimeters(duration);
  return cm;
}

Looks to me like you do.

but it does nothing

That's not possible. It might fail to compile. It might always return 0. It might always return -239. But, it can not "do nothing".

is a school project that's way i'm so insistent

Maybe you ought to start doing your homework earlier than the day before it's due.

:)) i don't try to finish something before everyone. i just wanth to make it work :).
this is a ruff shape of the code plese give it a look and tell me wath i did bad :expressionless:

//sensors
const int pingPin9 = 9;
const int pingPin8 = 8;
const int pingPin3 = 3;
const int pingPin2 = 2;
// motor pins
int E1 = 7; 
int M1 = 6; 
int E2 = 4; 
int M2 = 5; 

void setup() 
  { 
    Serial.begin(9600);
  pinMode(M1, OUTPUT); 
  pinMode(M2, OUTPUT); 
  }
  
  void loop()
{
  long dist2 = ping(pingPin2);
  delay(500);
  long dist8 = ping(pingPin8);
  delay(500);
  
if(dist2 > dist8)
   Stanga();
else
   Dreapta();
}
  
  long ping(int pinNumToReadFrom)
{
  long duration, cm;
  pinMode(pinNumToReadFrom, OUTPUT);
  digitalWrite(pinNumToReadFrom, LOW);
  delayMicroseconds(2);
  digitalWrite(pinNumToReadFrom, HIGH);
  delayMicroseconds(5);
  digitalWrite(pinNumToReadFrom, LOW);
  pinMode(pinNumToReadFrom, INPUT);
  duration = pulseIn(pinNumToReadFrom, HIGH);
  cm = microsecondsToCentimeters(duration);
  return cm;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
 
 void MergeFata()
{ 
digitalWrite(M1,HIGH); 
digitalWrite(M2,LOW);
analogWrite(E1, 0); //PLL Speed Control 
analogWrite(E2, 128); //PLL Speed Control 
} 
void MergeSpate()
{ 
digitalWrite(M1,HIGH); 
digitalWrite(M2,HIGH);
analogWrite(E1, 0); //PLL Speed Control 
analogWrite(E2, 128); //PLL Speed Control 
} 
void Dreapta()
{ 
digitalWrite(M1,HIGH); 
digitalWrite(M2,HIGH);
analogWrite(E1, 128); //PLL Speed Control 
analogWrite(E2, 0); //PLL Speed Control 
} 
void Stanga()
{
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 0);
}
void MergeFataDreapta()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,LOW);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void MergeFataStanga()
{
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void MergeSpateDreapta()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void MergeSpateStanga()
{
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 128);
}

By the way this is my project :slight_smile:


You are reading the values from two of the sensors 1/2 a second apart. Why the delay?

Then, if one value (dist2) is larger than the other value (dist8), you call one function (Stanga). Otherwise, you call the other function (Dreapta).

You did NOT describe what is in front of the two sensors, if anything, and what does or does not happen. Given such little information, it is impossible to help you.

You have a Serial.begin() statement in setup(). Add Serial.print() statements in loop, to print the values returned from the two function calls.

Serial.print("dist2: "); Serial.println(dist2);
Serial.print("dist8: "); Serial.println(dist8);

Do the values that get printed appear reasonable?

Do the values returned from the sensors cause the correct block of code (one statement, in your case) to be executed, even if they are not reasonable?

I made the "Serial print" and in the serial monitor the dist2 and dist8 have the value 0.

in front of the 2 sensors is nothing, but when is something i wanth the car to steer. (go around)

I don't see anything wrong with the ping function, but you can add Serial.print statements to it, to confirm the pin number is passed correctly, and that the pulseIn function is returning a reasonable value.

PaulS you're the best. Thank YOU very very much, it's working, and i started to make the code with all the conditions of driving:D if you are curios, make shure you give this Post a visit i will publish the finish result and a video with the Car, thank you again man ! :smiley:

PaulS i have another question now every thing is working fine, the car respones from the date colected from the ping sensors and react to it. but the car i going backwards and forward to quickly, is there a function or a code that i can use to make the car go forward a little bit slower ? Thank again you're the best !

Arduino+DFRduinoShieldL298n+4Ping - YouTube this is the car with only 2 sensors reactig to my hand ! :smiley:

but the car i going backwards and forward to quickly, is there a function or a code that i can use to make the car go forward a little bit slower ?

The analogWrite() calls in Stanga(), Dreapta(), etc. are what define how fast the motors spin. Change the 128 to a smaller value to make the motor spin slower.

The speed that the motor spins, sitting on the stand, and the speed it spins actually moving the car will not be the same.

Can i change the digitalWrite(M1, M2) also analogWrite(M1,M2) or that is fixed as digitalWrite?

The digitalWrite() statements are setting the direction that the motor(s) will rotate. Leave them alone. The analotWrite() statements set the speed of the motor (the second argument) associated with the specified pin (the first argument).