美文网首页
【arduino实验记录23】步进电机

【arduino实验记录23】步进电机

作者: Geekero | 来源:发表于2020-09-17 12:12 被阅读0次
/*************************************
 * name: stepper motor
 * function: you should see the rocker arm of the stepper motor spin clockwise and counterclockwise alternatedly
 * Stepper Motor Arduino Uno R3
 * IN1  11
 * IN2  10
 * IN3  9
 * IN4  8
 ****************************************/
 const int IN1=11;
 const int IN2=10;
 const int IN3=9;
 const int IN4=8;
 const char tab1[] = 
 {
  0x01, 0x03, 0x02, 0x06, 0x04, 0x0c, 0x08, 0x09
 };
 const char tab2[] = 
 {
  0x01, 0x09, 0x08, 0x0c, 0x04, 0x06, 0x02, 0x03
 };

 void setup()
 {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
 }

 void loop()
 {
  ctlStepMotor(360, 1);
  StepMotorStop();
  delay(1000);
  ctlStepMotor(-360, 1);
  StepMotorStop();
  delay(1000);
 }

 void ctlStepMotor(int angle, char speeds)
 {
  for (int j = 0; j < abs(angle); j++)
  {
    if (angle > 0)
    {
      for (int i = 0; i < 8; i++)
      {
        digitalWrite(IN1, ((tab1[i] & 0x01) == 0x01 ? true: false));
        digitalWrite(IN2, ((tab1[i] & 0x02) == 0x02 ? true: false));
        digitalWrite(IN3, ((tab1[i] & 0x04) == 0x04 ? true: false));
        digitalWrite(IN4, ((tab1[i] & 0x08) == 0x08 ? true: false));
        delay(speeds);
      }
    }
    else
    {
      for (int i = 0; i < 8; i++)
      {
        digitalWrite(IN1, ((tab2[i] & 0x01) == 0x01 ? true: false));
        digitalWrite(IN2, ((tab2[i] & 0x02) == 0x02 ? true: false));
        digitalWrite(IN3, ((tab2[i] & 0x04) == 0x04 ? true: false));
        digitalWrite(IN4, ((tab2[i] & 0x08) == 0x08 ? true: false));
        delay(speeds);
      }
    }
  }
 }

 void StepMotorStop()
 {
  digitalWrite(IN1, 0);
  digitalWrite(IN2, 0);
  digitalWrite(IN3, 0);
  digitalWrite(IN4, 0);
 }

Arduino入门教程14(步进电机):Arduino Uno R3+ULN2003驱动 实现步进电机正反转控制 的教程

/*
 * Stepper_Motor
 * 步进电机驱动,实现正反转
 */
 const int IN1=11;
 const int IN2=10;
 const int IN3=9;
 const int IN4=8;
 
 void setup()
 {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
 }


void clockwise(int num)
{
  for (int count = 0; count < num; count++)
  {
    for (int i = 2; i < 6; i++)
    {
      digitalWrite(i, HIGH);
      delay(3);
      digitalWrite(i, LOW);
    }
  }
}

void anticlockwise(int num)
{
  for (int count = 0; count < num; count++)
  {
    for (int i = 5; i > 1; i--)
    {
      digitalWrite(i, HIGH);
      delay(3);
      digitalWrite(i, LOW);
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  clockwise(512);
  delay(10);
  anticlockwise(512);
}

相关文章

网友评论

      本文标题:【arduino实验记录23】步进电机

      本文链接:https://www.haomeiwen.com/subject/qzhhyktx.html