Controlling servo motors and DC motors

Hi, I'm trying to create a code that makes two DC motors run over 5 seconds, and after that action is completed I want the motors to stop and rotate two servo motors over 180 degrees so that they can pull a trigger. I'm unsure what part of my code is wrong, I'm trying to use Booleans to define state functions that represent the actions I described previously.

#include <Servo.h>

// Define pins for L298N
int enA = 9;
int in1 = 8;
int in2 = 7;
int enB = 6;
int in3 = 5;
int in4 = 4;

//Variable that moves the servos
int pos = 0;

// Define pins for servos
int servoPin1 = 11;
int servoPin2 = 10;

// Create servo objects
Servo servo1;
Servo servo2;

// Set up a Boolean variable to track whether DC motors have completed their action
bool dcMotorsCompleted = false;

void setup() {
  // Initialize motor pins as outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Initialize servo pins
  servo1.attach(servoPin1);
  servo2.attach(servoPin2);
}

void loop() {
  // Check if DC motors have completed their action
  if (!dcMotorsCompleted) 
  {    
    // Run motors for 5 seconds
    analogWrite(enA, 255);
    analogWrite(enB, 255);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    delay(5000);

    // Stop motors
    analogWrite(enA, 0);
    analogWrite(enB, 0);
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    delay(5000);

    // Set the Boolean variable to true
    dcMotorsCompleted = true;
  }
  
  // If DC motors have completed their action, move the servo motors
  if (dcMotorsCompleted) {
   for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo1.write(pos); 
    servo2.write(pos);   
    delay(15);          // tell servo to go to position in variable 'pos'                    // waits 15ms for the servo to reach the position
  }
  }
}

And what do you see is happening ?

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

Oh okay, here is the circuit, I'm using an L298N to control both of my DC motors, they are connected to pins 9,8,7,6,5,4.
Here is a link that explains the interface: https://lastminuteengineers.com/l298n-dc-stepper-driver-arduino-tutorial/
To control the servos I'm using a breadboard to connect two servos, the pins to control the servos are directly connected to the Arduino, they are pins 11 and 10. However to power the servos I connected the red and brown cables from each servo to the breadboard and I'm planning to put a second battery also connected to the breadboard to power the servos.


We don’t see a ground between the Arduino and the external circuits.

Show us the power supply connections too.

Here is a more clear picture between the l298n and the Arduino. Sorry for the provisional drawings

For the breadboard

What kind of 9v battery are you using ?

Servos need 5 to 6v for power, 9v can damage them.

I was using this one: Duracell Coppertop 9-Volt Alkaline Batteries 1 pk Carded - Ace Hardware
but I will try to use a smaller one, thanks for the information

image

This isn’t your actual circuit, we need your circuit.


9v batteries like these cannot supply the required current needed.

9v on a servo will damage something !

okay, I'll check on that, here is a more accurate schematic of the entire circuit I'm using. The battery for the motors is this one: https://www.hobbytown.com/eflite-3s-lipo-battery-30c-11.1v-450mah-eflb4503sj30/p147134
I will use a 6V battery for the servos, but the servos are these ones: KST DS215MG Digital Servo 6.0V 0.06s 51oz - Buddy RC
they were from a drone that I'm no longer using

Your servo ground needs to be connected to the Arduino GND.

Okay, so the three GND pins in the Arduino will be used right?

The servos share their grounds back on the solderless breadboard.

Okay, so should I directly connect the grounds from the servos to the GND pins in the Arduino?

A wire from the solderless breadboard ground bus connects to one of the free Arduino GND terminals.

Oh that makes sense, thanks. Do you think there is something wrong with the code though? I want it to first make both DC motors run over 5 seconds, then stop. And after it stops I want it to make rotate both servos over 180 degrees. When I tried it with the code that I sent, it just made the DC motors work but the servos didn’t do anything

Code looks reasonable.

After 10 seconds the servos will move, however, they need to be stopped if that’s what you desire.

Okay, by any chance do you know how can I stop them? Should i put some type of condition to break the if statement that I putted for the servos?

Once the servos move to where you want, use another variable that disables any further cycling of your servos.

Okay, thanks for your help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.