Help with controlling motors

I have just started on this project. So I used an Arduino Uno board maybe R3 and it is attached to an L298P motor shield that drives two motors and I got an SG90 servo and Ultrasonic sensor (HC SR04).
My problem is when I run the code, only one motor turns on. The servo and the sensors are working as well as I expected. I've checked the power sources and connection and they all working well so far. And I suppose the problem is in the code.
Here's the code:

#include<Servo.h>
#include<NewPing.h>
Servo servo;
#define TRIG_PIN A0 
#define ECHO_PIN A1
#define MAX_DISTANCE 400
const int sped = 80;
const int dela = 30;
const int dL = 200;
int E1 = 10;
int M1 = 12;
int E2 = 11;
int M2 = 13;
int value;
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 
int distance = 100;
void setup() {
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
servo.attach(3);
 distance = readPing();
  delay(100);
  servo.write(90); 
  delay(2000);}

void loop() {
  // put your main code here, to run repeatedly:
int distanceR = 0;
 int distanceL =  0;
 delay(40);
   if(distance<=15)
 {
  slow();
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);
  if(distanceR>=distanceL)
  {
    turnright();
    slow();
  }else
  {
    turnleft();
    slow();
  }
 }else if(distance>15)
 {
  straight();
 }
 distance = readPing();
}

void straight(){
  for (value = 0 ; value <= sped; value += 5)
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, HIGH);
    analogWrite(E1, value);
    analogWrite(E2, value);
    delay(dela);
  } delay(dL);
   for (value = sped; value >= 0; value -= 5)
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, HIGH);
    analogWrite(E1, value);
    analogWrite(E2, value);
    delay(dela);
  } delay(dL);

}
void turnleft(){
  for (value = 0 ; value <= sped; value += 5)
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, HIGH);
    analogWrite(E1, value);
    analogWrite(E2,0);
    delay(dela);
    
  } delay(dL); 
  for (value = sped; value >= 0; value -= 5)
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, HIGH);
    analogWrite(E1, value);
    analogWrite(E2, 0);
    delay(dela);
  }
  
   delay(dL);
}

void slow(){
  for (value = sped; value >= 0; value -= 5)
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, LOW);
    analogWrite(E1, value);
    analogWrite(E2, value);
    delay(dela);
  } delay(dL);
}
void turnright(){
  for (value = 0 ; value <= sped; value += 5)
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, HIGH);
    analogWrite(E2, value);
    analogWrite(E1,0);
    delay(dela);}
    delay(dL);
    for (value = sped; value >= 0; value -= 5)
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, HIGH);
    analogWrite(E1, 0);
    analogWrite(E2, value);
    delay(dela);
  }
  
   delay(dL);
}
void returnn(){
   for (value = 0 ; value <= sped; value += 5)
  {
    digitalWrite(M1, LOW);
    digitalWrite(M2, LOW);
    analogWrite(E1, value);
    analogWrite(E2, value);
    delay(dela);
  } delay(dL);
  for (value = sped; value >= 0; value -= 5)
  {
    digitalWrite(M1, LOW);
    digitalWrite(M2, LOW);
    analogWrite(E1, value);
    analogWrite(E2, value);
    delay(dela);
  } delay(dL);}
int lookRight()
{
    servo.write(50); 
    delay(500);
    int distance = readPing();
    delay(100);
    servo.write(90); 
    return distance;
    delay(100);
}

int lookLeft()
{
    servo.write(170); 
    delay(500);
    int distance = readPing();
    delay(100);
    servo.write(90); 
    return distance;
    delay(100);
}
int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}


It looks like insufficient powering. Please post schematics showing the power situation.

Hello newegine

If you expect a realtime behaiviour of the program you have to get rid of the delay() function. This function blocks the execution.

Line  22:   delay(100);
	Line  24:   delay(2000);}
	Line  30:  delay(40);
	Line  35:   delay(200);
	Line  37:   delay(200);
	Line  61:     delay(dela);
	Line  62:   } delay(dL);
	Line  69:     delay(dela);
	Line  70:   } delay(dL);
	Line  80:     delay(dela);
	Line  82:   } delay(dL); 
	Line  89:     delay(dela);
	Line  92:    delay(dL);
	Line 102:     delay(dela);
	Line 103:   } delay(dL);
	Line 112:     delay(dela);}
	Line 113:     delay(dL);
	Line 120:     delay(dela);
	Line 123:    delay(dL);
	Line 132:     delay(dela);
	Line 133:   } delay(dL);
	Line 140:     delay(dela);
	Line 141:   } delay(dL);}
	Line 145:     delay(500);
	Line 147:     delay(100);
	Line 150:     delay(100);
	Line 156:     delay(500);
	Line 158:     delay(100);
	Line 161:     delay(100);
	Line 164:   delay(70);

Design and code your own none.blocking timer function by using the BlinkWithoutOut example of the IDE.

Have a nice day and enjoy coding in C++.

1 Like

Maybe you are right. I used a jack charger connect directly from the socket, and I also add six 1,5v pins but the other motor doesn't run. Eventually, these pins get really hot, and I wonder why?

I don't think that really resolves my problem but thanks. Have a nice day too

1 Like

Do you have a data sheet for the motors or can you post a link to a webpage.
What is the voltage and Amps of your power supply?

here's the link:

The adaptor provides 15V and 2A

Please post schematics. " Added 6 1.5 volt pins(?)" confuses more then it helps.

That adapter.... It depends on the motors. Provide a link to their datasheet.

datasheet motor

  • Operating voltage: 3V~ 9V DC (Works best from 6 - 8V)
  • Maximum torque: 800gf cm min 1:48 (3V)
  • No-load speed: 125 rpm (3V)
    (With 70mm wheel: 26m/1p)
  • 208 RPM / 1 Min (5V)
    (With 70mm wheel: 44m/1p)
  • No-load current: 70mA (250mA MAX)
  • Dimensions: 722.5cm

There are four pins for motors on the shield. But only two of them work. When I run the code that only tells motors to run, the two motors run perfectly. Perhaps, I think it was because of too much energy needed.

The motors works best from 6 - 8V, but you are giving them 15V, that's way too much.

If you are powering the Uno from the motor shield (OPT jumper ON) then the limit is 12V or you may burn out the Uno.

Make sure the shield is NOT touching the top of the metal USB connector on the Uno

1 Like

What is the stall current?

Words and pins..... What/which 4 pins"? Please use schematics to show what You have done.
Which 2 works? Bad connection of signal lines operating those pins?

Do you need

pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);

in setup?

They default as input. Will the compiler correct this when analogWrite is used?

Yes

thanks it is the solution I'm looking for
It worked

Just to satisfy my curiosity, what was the problem?

Not sure but maybe overloaded power

Thanks
Have a nice day

1 Like