Pages: [1] 2   Go Down
Author Topic: Get Motor respons From Ping Sensor  (Read 1594 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Code:
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 smiley
Logged

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

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.

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you , very much smiley i will try to work things out and publish the results smiley by the way wath <snip> means at the end of the ending schetch?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad Please help
Logged

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

Quote
by the way wath <snip> 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
Code:
ong distLeft = ping(pinLeft);
long distRight = ping(pinRight);
if(distLeft > distRight)
   turnLeft();
else
   turnRight();
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sory i'm hard head ok i'd made the part of the code but it does nothing i thing
Code:
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 smiley-neutral Sry pauls i really need to get it done because is a school project that's way i'm so insistent smiley-neutral
Logged

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

Quote
"pinNumToReadFrom"  i don't have this declared anywere
Code:
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.

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

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

smiley) i don't try to finish something before everyone. i just wanth to make it work smiley.
this is a ruff shape of the code plese give it a look and tell me wath i did bad smiley-neutral
Code:
//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 smiley



Logged

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

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.

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

  this is the car with only 2 sensors reactig to my hand ! smiley-grin
Logged

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

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

Pages: [1] 2   Go Up
Jump to: