4WD pirate platform issues

Hello i have a quick question, I have a project with the Arduino pirate and only one side of the wheel are moving forward while the other is going backwards, I don't know if it is my code or if is my wiring.

int trigPin=9;
int echoPin=10;
int speedPin_M1=5;
int speedPin_M2=6;
int directionPin_M1=4;
int directionPin_M2=7;
long duaration;

void setup(){
  
} 

   int carBack(int leftspeed,int rightspeed){
    analogWrite(speedPin_M2,leftspeed);
    digitalWrite(directionPin_M1,HIGH);
    analogWrite(speedPin_M1,rightspeed);
    digitalWrite(directionPin_M2,HIGH);
  }
   int carAdvance (int leftspeed,int rightspeed){
    analogWrite(speedPin_M2,rightspeed);
    digitalWrite(directionPin_M1,HIGH);
    analogWrite(speedPin_M1,leftspeed);
    digitalWrite(directionPin_M2,HIGH);
  }
   int carTurnLeft(int leftspeed,int rightspeed){
    analogWrite(speedPin_M2,leftspeed);
    digitalWrite(directionPin_M1,HIGH);
    analogWrite(speedPin_M1,leftspeed);
    digitalWrite(directionPin_M2,HIGH);
  }
   int carTurnRight(int leftspeed,int rightspeed){
    analogWrite(speedPin_M2,leftspeed);
    digitalWrite(directionPin_M1,HIGH);
    analogWrite(speedPin_M1,leftspeed);
    digitalWrite(directionPin_M2,HIGH);
  }

 void loop(){
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  byte duration=0;
  byte distance=0;
  duration=pulseIn(echoPin,HIGH);
  distance=duration/58.2;
  delay(10); 
 
 if(distance>19);
  {
   carAdvance(100,100);
  }
  if(distance<18)
  {
    carTurnLeft(100,100);
    carAdvance(100,100);
  }
 }

I know nothing about the pirate. In your situation I would see if there are any examples for the pirate platform and if there are I would run them first to see if the hardware is working as expected.

"the other" side motor wires are on the wrong driver pins.

thank you very much, I also have a sensor connected to it would that also be a problem, because every time I take it off it is fine.

Sometime devices interfere with other devices if not correctly installed. Show a drawing of your project and post your sketch (program) for better help.

Swap the two motor power wires on the side that is going backwards.

Thank you John

I’m not sure if wire are wrong because when I unplug infrasonic sensor car is going forward like should be
When I get home I will send you diagram and coding
Thank you


The HC-SR04 should be on pins 9 and 10, not 6 and 7 (motors are defined on 6 and 7).

Hello
Thank you for all help with this project
I switch trig wire to #9 and echo wire to #10 and car just going forward.
Also left side wheels motors are connected to M1 and right side motors to M2

My daughter is learning programming arduino (unfortunately I can’t help because I don’t have any clues about programming)
and trying to make car going forward and when is obstacle to turn left or right and go around it

I’m not sure if our ultrasonic sensor is not bad because back of it get really hot (untouchable)

Thank you for help

You can help. You are an objective observer. You see results as they are not as they should be.

In your code, you are missing a line to tell the TRIG pin to go LOW after 10us.

digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW); // <<---add this line here
  byte duration=0;
  byte distance=0;
  duration=pulseIn(echoPin,HIGH);
  distance=duration/58.2;
  delay(10); 

The way you have the TRIG pulse, it is not forced low at 10us. Adding the new line will form a better TRIG pulse.

Also, please replace your pictures of code with the code text, as you have done correctly in your post #1

Thank you for your quick response
i add line and still car going only forward and ultrasonic sensor look like no responding.
Could be my ultrasonic sensor bad? I'm not sure if is normal to back of sensor be untouchable hot.
Thank you

int trigPin=9;
int echoPin=10;
int speedPin_M1=5;
int speedPin_M2=6;
int directionPin_M1=4;
int directionPin_M2=7;
long duaration;

void setup(){

}

int carBack(int leftspeed,int rightspeed){
analogWrite(speedPin_M2,leftspeed);
digitalWrite(directionPin_M1,HIGH);
analogWrite(speedPin_M1,rightspeed);
digitalWrite(directionPin_M2,HIGH);
}
int carAdvance (int leftspeed,int rightspeed){
analogWrite(speedPin_M2,rightspeed);
digitalWrite(directionPin_M1,HIGH);
analogWrite(speedPin_M1,leftspeed);
digitalWrite(directionPin_M2,HIGH);
}
int carTurnLeft(int leftspeed,int rightspeed){
analogWrite(speedPin_M2,leftspeed);
digitalWrite(directionPin_M1,HIGH);
analogWrite(speedPin_M1,leftspeed);
digitalWrite(directionPin_M2,HIGH);
}
int carTurnRight(int leftspeed,int rightspeed){
analogWrite(speedPin_M2,leftspeed);
digitalWrite(directionPin_M1,HIGH);
analogWrite(speedPin_M1,leftspeed);
digitalWrite(directionPin_M2,HIGH);
}

void loop(){
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
byte duration=0;
byte distance=0;
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance=duration/58.2;
delay(10);

if(distance>19);
{
carAdvance(100,100);
}
if(distance<18)
{
carTurnLeft(100,100);
carAdvance(100,100);
}
}

When you paste code, be sure it is formatted in your IDE, then select/copy the code, then click the < CODE > button in the message box, and paste your code where you see "type or paste code here"

Remove the ";" semicolon on the above line.

And... all your motors are being told to go full forward in "left" "right" "back" or "advance"

Have a look at this simulator... the lights are simulating motors... see how "left" and "right" turn on different lights (motors)? See if you can change your "left" and "right" to make them do what you want.

type or paste code here int trigPin=9;
int echoPin=10;
int speedPin_M1=5;
int speedPin_M2=6;
int directionPin_M1=4;
int directionPin_M2=7;
long duaration;

void setup(){
  
} 

   int carBack(int leftspeed,int rightspeed){
    analogWrite(speedPin_M2,leftspeed);
    digitalWrite(directionPin_M1,HIGH);
    analogWrite(speedPin_M1,rightspeed);
    digitalWrite(directionPin_M2,HIGH);
  }
   int carAdvance (int leftspeed,int rightspeed){
    analogWrite(speedPin_M2,rightspeed);
    digitalWrite(directionPin_M1,HIGH);
    analogWrite(speedPin_M1,leftspeed);
    digitalWrite(directionPin_M2,HIGH);
  }
   int carTurnLeft(int leftspeed,int rightspeed){
    analogWrite(speedPin_M2,leftspeed);
    digitalWrite(directionPin_M1,HIGH);
    analogWrite(speedPin_M1,leftspeed);
    digitalWrite(directionPin_M2,HIGH);
  }
   int carTurnRight(int leftspeed,int rightspeed){
    analogWrite(speedPin_M2,leftspeed);
    digitalWrite(directionPin_M1,HIGH);
    analogWrite(speedPin_M1,leftspeed);
    digitalWrite(directionPin_M2,HIGH);
  }

 void loop(){
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  byte duration=0;
  byte distance=0;
  digitalWrite(trigPin,LOW);
  duration=pulseIn(echoPin,HIGH);
  distance=duration/58.2;
  delay(10); 
 
 if(distance>19)
  {
   carAdvance(100,100);
  }
  if(distance<18)
  {
    carTurnLeft(100,100);
    carAdvance(100,100);
  }
 }

This tells me that you have two motors (not 4WD), and to make the wheels go forward they need a positive value (100 for leftspeed and 100 for rightspeed).

In the following code, "advance" and "back" use the same values (even though the lines are in jumbled order)...

... so I suspect "back" should have a "LOW" for each direction pin... like this...

   int carBack(int leftspeed,int rightspeed){
    analogWrite(speedPin_M2,leftspeed);
    digitalWrite(directionPin_M1,LOW// changed to LOW
    analogWrite(speedPin_M1,rightspeed);
    digitalWrite(directionPin_M2,LOW); // changed to LOW
  }

Would you give that a try on "carBack()"?

Sorry... I have been overlooking these two lines...

They must be "int" long because a "byte" will only work if the "duration" is < 256, and same with distance.

I did some work on your almost ready sketch. Your motors were full-forward, no backward, and instant left/right. This sketch will look for the same distances, but I added "STOP"

int trigPin = 9;
int echoPin = 10;

int speedPin_M1 = 5;
int directionPin_M1 = 4;

int speedPin_M2 = 6;
int directionPin_M2 = 7;

long duration;
long distance;

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

int carBack(int leftspeed, int rightspeed) {
  Serial.println("Back");
  analogWrite(speedPin_M1, rightspeed);
  digitalWrite(directionPin_M1, LOW);
  analogWrite(speedPin_M2, leftspeed);
  digitalWrite(directionPin_M2, LOW);
}

int carAdvance (int leftspeed, int rightspeed) {
  Serial.println("Advance");
  analogWrite(speedPin_M1, leftspeed);
  digitalWrite(directionPin_M1, HIGH);
  analogWrite(speedPin_M2, rightspeed);
  digitalWrite(directionPin_M2, HIGH);
}

int carTurnLeft(int leftspeed, int rightspeed) {
  Serial.println("Left");
  analogWrite(speedPin_M1, leftspeed);
  digitalWrite(directionPin_M1, HIGH);
  analogWrite(speedPin_M2, leftspeed);
  digitalWrite(directionPin_M2, HIGH);
}

int carTurnRight(int leftspeed, int rightspeed) {
  Serial.println("Right");
  analogWrite(speedPin_M1, leftspeed);
  digitalWrite(directionPin_M1, HIGH);
  analogWrite(speedPin_M2, leftspeed);
  digitalWrite(directionPin_M2, HIGH);
}

int carStop() {
  Serial.print("STOP ");
  analogWrite(speedPin_M1, 0);
  digitalWrite(directionPin_M1, 0);
  analogWrite(speedPin_M2, 0);
  digitalWrite(directionPin_M2, 0);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration / 58.2;

  if (distance > 19) {
    carAdvance(100, 100);
  }

  if (distance < 18) {
    carStop();
    Serial.print("Distance: ");
    Serial.println(distance);
    delay(1000);
    carBack(100, 100);
    delay(1000);
    if (random(2)) // choose random direction
      carTurnLeft(100, 100);
    else
      carTurnRight(100,100);
    delay(1000);
  }
}

12 posts were split to a new topic: Upload to DFRobot RoMeo BLE board fails