#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 3,2);
String comdata = ""; //接收串口数据
int stepCount = 0; // number of steps the motor has taken
void setup() {
// initialize the serial port:
Serial.begin(9600);
myStepper.setSpeed(200);
}
void loop() {
// step one step:
//接收串口数据
while (Serial.available() > 0)
{
comdata += char(Serial.read());
delay(2);
}
//数据类型转换
if (comdata.length() > 0)
{
stepCount = comdata.toInt();
Serial.println(stepCount);
comdata = "";
}
if(stepCount>0){
myStepper.step(stepCount);
stepCount = 0;
}
}
步进电机 接收指定步数测试程序
最新推荐文章于 2023-09-10 19:20:47 发布