简体   繁体   English

Arduino直流电机与伺服电机同步

[英]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. 我有这个简单的arduino代码,它使用L298N驱动两个Dc电机,L298N是一个电机驱动器以及两个伺服电机。 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. 我听说过多线程,但是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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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