简体   繁体   中英

Arduino dc motor in sync with servo motor

I have this simple arduino code that drives two Dc motor using L298N which is a motor driver together with two servo motors. The code is working fine for the motor driver but not for the servo motor:

#include <Servo.h>

Servo myservo;
Servo myservo2;
int num=1;
int IN1=8;
int IN2=9;
int ENA=3;

int IN3=10;
int IN4=11;
int ENA2=4;

void setup()
{
  myservo.attach(40);
  myservo2.attach(42);

 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);

 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT); 
}
void loop()
{
   intialPos();    

 while(motor_run())
  {
        turnOne();
       delay(3000);
       intialPos();
       delay(10000);
       turnSecond();
       delay(3000);
  } 

}
int motor_run()
{

  analogWrite(ENA, 1500);// motor speed  
  digitalWrite(IN1,LOW);// rotate forward
  digitalWrite(IN2,HIGH);

  analogWrite(ENA2, 1500);// motor speed  
  digitalWrite(IN3,HIGH);// rotate forward
  digitalWrite(IN4,LOW);

  delay(3000);
  return (1);
}
void intialPos()
{
  myservo.write(70);
   myservo2.write(135);
  delay(2000);
}
void turnOne()
{
  myservo2.write(170);
  myservo.write(135);
  delay(2000);
}
void turnSecond()
{
  myservo2.write(70);
  myservo.write(30);
  delay(2000);
}

My problem is to make the dc motor code continues to execute while the servo motor do its thing which is turning. But all it does was to make the dc motor work and the servo motors are unmoving. I'm using the servo motor as a rudder in a boat so I need to make the dc motor continuously working while the servo motor turns to a direction. I've heard multi threading but it is not supported on arduino. I found another way to make them in sync with each other which is using cycle or timing, but the sample code was confusing so can someone give me a code snippet and some detailed explanation for it.

For the people who are curious on how i did it..this is the code with the little revision. Since I found out the movement is going backward not forward.

#include <Servo.h>

Servo myservo;
Servo myservo2;
int num=1;
int IN1=8;
int IN2=9;
int ENA=3;

int IN3=10;
int IN4=11;
int ENA2=4;

void setup()
{
  myservo.attach(40); // right side servo motor
  myservo2.attach(42); // left side servo motor

 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);

 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT); 
}
void loop()
{
   intialPos();   

 while(motor_run())
  {
       intialPos();
       turnOne();
       delay(3000);
       intialPos();
       delay(10000);
       turnSecond();
       delay(3000);
  } 

}
int motor_run()
{
  Serial.println("dc motor is working");
  analogWrite(ENA, 255);// motor speed  
  digitalWrite(IN1,HIGH);// rotate forward
  digitalWrite(IN2,LOW);

  analogWrite(ENA2, 255);// motor speed  
  digitalWrite(IN3,HIGH);// rotate forward
  digitalWrite(IN4,LOW);

  return (1);
}
void intialPos()
{
  Serial.println("servo motor in intial position");
  myservo.write(70);
  myservo2.write(135);

}
void turnOne()
{
  Serial.println("servo motor turnOne");
  myservo2.write(170);
  myservo.write(135);
}
void turnSecond()
{
  Serial.println("servo motor turnSecond");
  myservo2.write(70);
  myservo.write(30);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM