Hi guys, help needed to create carry on the code for my parallel parking RC car project.
I have done the code for forward/reverse and left/right turn in the presence of an obstacle. Appreciate if anyone can help me to complete or provide guidance for the parallel parking part. I believe case coding is required but I'm not good in programming. Please help.
This is the code for moving away from obstacle.
const int pingPin = 7;
const int pingPin2 = 5;
void setup() {
// set the switch as an input:
// pinMode(switchPin, INPUT);
Serial.begin(9600);
//Dist.begin(A0);
// set all the other pins you're using as outputs:
// set enablePin high so that motor can turn on:
}
void loop() {
long duration, cm;
long duration2, cm2;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
pinMode(pingPin2, INPUT);
duration2 = pulseIn(pingPin2, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
cm2 = microsecondsToCentimeters(duration2);
Serial.print(cm);
Serial.print("cm");
Serial.print("\t"); // prints a tab
Serial.print(cm2);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
This is the code for multiple sensors (front, side and rear) which I have not integrate into the main code above:
// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin1 = 2;
const int pingPin2 = 7;
const int pingPin3 = 8;
//const int pingPin4 = 10;
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration1, cm;
pinMode(pingPin1, OUTPUT);
digitalWrite(pingPin1, LOW);
delayMicroseconds(2);
digitalWrite(pingPin1, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin1, LOW);
pinMode(pingPin1, INPUT);
duration1 = pulseIn(pingPin1, HIGH);
cm = microsecondsToCentimeters(duration1);
Serial.print("A ");
Serial.print(cm);
Serial.print("\t"); // prints a tab
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("B ");
Serial.print(cm2);
Serial.print("\t"); // prints a tab
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);
cm = microsecondsToCentimeters(duration3);
Serial.print("C ");
Serial.println(cm3);
/* long duration4;
pinMode(pingPin4, OUTPUT);
digitalWrite(pingPin4, LOW);
delayMicroseconds(2);
digitalWrite(pingPin4, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin4, LOW);
pinMode(pingPin4, INPUT);
duration4 = pulseIn(pingPin4, HIGH);
Serial.print("D ");
Serial.println(duration4);
*/
delay(10);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

