使用操纵杆激活两个单独的步进电机(使用带有两个电机的X和Y操纵杆)

时间:2019-11-11 00:07:47

标签: arduino joystick stepper motordriver

我正在使用两个步进电机28BYJ-48和一个具有X和Y轴功能的简单操纵杆。我有使它们两个都可以单独工作的代码,但是,我希望我的arduino同时监视x和y值,这样,如果我对角移动它,它将同时旋转两个电动机。现在,我一次只能转动一个电动机。我确切地知道我需要做什么,但是我不知道如何在arduino中做到这一点,我通常将TI控制器与代码块配合使用,因此我可以在代码运行时对其进行跟踪。

我需要我的代码同时检查X和Y值,而我只能使它一次又一次地检查它们,这就是为什么一次只旋转一个电动机,有时却看不到我的原因完全在移动操纵杆。有时会感到困惑和卡住。我可以想象它就像是一个中断。我不确定arduino是否这么复杂。

我尝试了很多不同的功能,例如仅使用while循环或do whiles,或者将我的第二个马达代码放在第一个马达的else语句中,以尝试并查看它。 我环顾四周,试图做到这一点,但是没有人一次只有两个电机。我还尝试将两个电动机代码一起包含在内,以便它们可以一起运行,但没有任何效果。

下面的代码是从我在网上找到的仅适用于一个电机的另一个代码修改而来的。

// include Arduino stepper motor library
#include <Stepper.h>

// define number of steps per revolution
#define STEPS 32

// define stepper motor control pins
#define IN5  7
#define IN6  6
#define IN7  5
#define IN8  4

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11

// initialize stepper library
Stepper stepper(STEPS, IN8, IN6, IN7, IN5);
Stepper stepper_vert(STEPS, IN4, IN2, IN3, IN1);

// joystick pot output is connected to Arduino A0
#define joystick  A1
#define joystick_vert A0 //vert is referring to the vertical axis

void setup()
{

}

void loop()
{
  // read analog value from the potentiometer
  int val = analogRead(joystick);
  int val_vert = analogRead(joystick_vert);

  // if the joystic is in the middle ===> stop the motor
  if(  (val > 490) && (val < 540))
  {
    digitalWrite(IN5, LOW);
    digitalWrite(IN6, LOW);
    digitalWrite(IN7, LOW);
    digitalWrite(IN8, LOW);
  }

  else
  {
    // move the motor in the first direction
    while (val >= 520)
    {
      // map the speed between 5 and 500 rpm
      int speed_ = map(val, 520, 1023, 5, 500);
      // set motor speed
      stepper.setSpeed(speed_);

      // move the motor (1 step)
      stepper.step(1);

      val = analogRead(joystick);
    }

    // move the motor in the other direction
    while (val <= 500)
    {

      int speed_ = map(val, 500, 0, 5, 500);// map the speed between 5 and 500 rpm
      // set motor speed
      stepper.setSpeed(speed_);

      // move the motor (1 step)
      stepper.step(-1);

      val = analogRead(joystick);

    }
  }

if(  (val_vert > 490) && (val_vert < 540) )
  {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  }

  else
  {

    while (val_vert >= 540)// move the motor in the first direction
    {
      // map the speed between 5 and 500 rpm
      int speed_ = map(val_vert, 540, 1023, 5, 500);
      // set motor speed
      stepper_vert.setSpeed(speed_);

      // move the motor (1 step)
      stepper_vert.step(1);

      val_vert = analogRead(joystick_vert);
    }

    // move the motor in the other direction
    while (val_vert <= 500)
    {
      // map the speed between 5 and 500 rpm
      int speed_ = map(val_vert, 500, 0, 5, 500);
      // set motor speed
      stepper_vert.setSpeed(speed_);

      // move the motor (1 step)
      stepper_vert.step(-1);

      val_vert = analogRead(joystick_vert);
    }

  }



}

没有错误消息,只要它们的值告诉它们,就需要两个电动机同时工作。如果我将操纵杆垂直向上或向下拉,则只有一个电机可以工作,左右都一样。但是,如果我向左走,我需要两个电动机都旋转,这取决于它们的值,它们会给它们一个速度。

0 个答案:

没有答案