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