arduino步进电机的操控代码

#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 100

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 4, 5, 6, 7);//ina ,inb ,inc,ind;);  设置步进电机的引脚接入引脚;

// the previous reading from the analog input
int previous = 0;

void setup()
{
  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(120);//set speed;  //设置电机的转速,但是次函数不会使电机转动;
}

void loop()
{
  // get the sensor value
  int val = analogRead(0);

  // move a number of steps equal to the change in the
  // sensor reading
  stepper.step(val - previous);//此函数的作用是控制步进电机按照setspeed()设置的转速转动一定的步距角;

  // remember the previous value of the sensor
  previous = val;
}

你可能感兴趣的:(arduino步进电机的操控代码)