having problems with the motor diirections in my code can somebody please help?

II built a basic rover with a sonar sensor on top and everything seems to be ok but the rover rolls backwards instead of foward so I really can't perform a proper test I think I know what to do but I wanted an honest opinion on my code before I start making changes that would mess somthing else up. The ping sensor motor works perfect it's just I cant really ust let it loose until I get the motors right

I just need somebody with a little more skl then my self tolet me know what small tweeks I need to make to the code

Thanks a billion

code file also attached

#include <Servo.h> // Enables the Servo library
 
int E1 = 6; //M1 Speed Control 
int E2 = 5; //M2 Speed Control 
int M1 = 8; //M1 Direction Control 
int M2 = 7; //M1 Direction Control

Servo PingServo;
//int IRsensor = 3; //IR interface
int pingPin = 11;//Ping sensor
int minSafeDist = 11 ; // Minimum distance for ping sensor to know when to turn
int centerDist, leftDist, rightDist, backDist; // Define variables center, left, right and back distance
long duration, inches, cm; // Define variables for Ping sensor 

void setup() {
PingServo.attach(10); // Servo is attached to pin 10 in the motor shield
PingServo.write(90); // Center the Ping sensor (puts it at 90 degrees)
int i; 
for(i=5;i<=8;i++) 
//pinMode(M1, OUTPUT);
//pinMode(M2, OUTPUT);

Serial.begin(9600); // Enables Serial monitor for debugging purposes
Serial.println("Serial test!"); // Test the Serial communication

}
void stop(void) //Stop 
{ 
 digitalWrite(E1,LOW); 
 digitalWrite(E2,LOW); 
} 
void advance(char a,char b) //Move forward 
{ 
 analogWrite (E1,a); //PWM Speed Control 
 digitalWrite(M1,HIGH); 
 analogWrite (E2,b); 
 digitalWrite(M2,HIGH);
Serial.println("Going forward"); // Prints a line in the serial moni 
} 
void turn_R (char a,char b) //Turn Right 
{ 
 analogWrite (E1,a); 
 digitalWrite(M1,HIGH); 
 analogWrite (E2,b); 
 digitalWrite(M2,LOW); 
delay(1600); // Time required to turn right (1.6 seconds)
Serial.println("Motors going Right"); // Prints a line in the serial monitor

} 
void back_off (char a,char b) //Move backward 
{ 
 analogWrite (E1,a); 
 digitalWrite(M1,LOW); 
 analogWrite (E2,b); 
 digitalWrite(M2,LOW);
 delay(1600); // Time Required to go back (1.6 seconds)
 Serial.println("Backward"); // Prints a line in the serial monitor 
} 
void turn_L (char a,char b) //Turn Left 
{ 
 analogWrite (E1,a); 
 digitalWrite(M1,LOW); 
 analogWrite (E2,b); 
 digitalWrite(M2,HIGH); 
delay(1600); //Time Required to turn left (1.6)Seconds
Serial.println("Motors going Left");// Prints a line in the serial monitor
} 
void loop() 
{ 
LookAhead();
Serial.print(inches);
Serial.println(" inches"); // Prints a line in the serial monitor
if(inches >= minSafeDist) /* If the inches in front of an object is greater than or equal to the minimum safe distance (11 inches), react*/
{

advance(240, 240); // All wheels forward
delay(110); // Wait 0.11 seconds
}else // If not:

{
stop(); // Stop all motors
LookAround(); // Check your surroundings for best route
if(rightDist > leftDist) // If the right distance is greater than the left distance , turn right
{
turn_R(240, 240);
}else if (leftDist > rightDist) // If the left distance is greater than the right distance , turn left
{
turn_L(240, 240);
}else if (leftDist&&rightDist<minSafeDist) // If the left and right distance is smaller than the min safe distance (11 inch) go back
{
back_off(240, 240);
}
}
}
unsigned long ping() {
pinMode(pingPin, OUTPUT); // Make the Pingpin to output
digitalWrite(pingPin, LOW); //Send a low pulse
delayMicroseconds(2); // wait for two microseconds
digitalWrite(pingPin, HIGH); // Send a high pulse
delayMicroseconds(5); // wait for 5 micro seconds
digitalWrite(pingPin, LOW); // send a low pulse
pinMode(pingPin,INPUT); // switch the Pingpin to input
duration = pulseIn(pingPin, HIGH); //listen for echo

/*Convert micro seconds to Inches
-------------------------------------*/

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
}

long microsecondsToInches(long microseconds) // converts time to a distance
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) // converts time to a distance
{
return microseconds / 29 / 2;
}

void LookAhead() {
PingServo.write(90);// angle to look forward
delay(175); // wait 0.175 seconds
ping();
}

void LookAround(){
PingServo.write(180); // 180° angle
delay(320); // wait 0.32 seconds
ping();
rightDist = inches; //get the right distance
PingServo.write(0); // look to the other side
delay(620); // wait 0.62 seconds
ping(); 
leftDist = inches; // get the left distance
PingServo.write(90); // 90° angle
delay(275); // wait 0.275 seconds

// Prints a line in the serial monitor
Serial.print("RightDist: ");
Serial.println(rightDist);
Serial.print("LeftDist: ");
Serial.println(leftDist);
Serial.print("CenterDist: ");
Serial.println(centerDist);
}

Ping Test.txt (4.38 KB)

Is your problem related to this?

  for(i=5;i<=8;i++) 
    //pinMode(M1, OUTPUT);
    //pinMode(M2, OUTPUT);

Your code would be easier to read if you used the auto format tool in the IDE.

thnk you will try