151 lines
4.1 KiB
C++
151 lines
4.1 KiB
C++
// A4988引脚连接Arduino引脚编号
|
|
#include <AccelStepper.h>
|
|
|
|
const int dirPin = 46; // Direction
|
|
const int stepPin = 47; // Step
|
|
const int limit_forward = 28; // Ms3
|
|
const int limit_back = 30; // Ms2
|
|
// 步进电机旋转一周步数
|
|
const int STEPS_PER_REV = 1000;
|
|
|
|
char cmd[10]; //用户指令字符
|
|
int data; //用户指令数据
|
|
int motorSpeed = 500; //电机转速(数值越小速度越小)
|
|
AccelStepper stepper(4, stepPin, dirPin);
|
|
unsigned long startTime;
|
|
unsigned long endTime;
|
|
unsigned long executionTime;
|
|
void setup() {
|
|
// 设置引脚模式
|
|
pinMode(stepPin, OUTPUT);
|
|
pinMode(dirPin, OUTPUT);
|
|
pinMode(limit_forward, INPUT);
|
|
pinMode(limit_back, INPUT);
|
|
|
|
// 初始化电机步进模式为1/16步进
|
|
//digitalWrite(ms1Pin, HIGH);
|
|
//digitalWrite(ms2Pin, HIGH);
|
|
//digitalWrite(ms3Pin, HIGH);
|
|
|
|
|
|
|
|
Serial.begin(9600);
|
|
Serial.println("++++++++++++++++++++++++++++++++++");
|
|
Serial.println("+ Taichi-Maker A4988 Steper Demo +");
|
|
Serial.println("+ www.taichi-maker.com +");
|
|
Serial.println("++++++++++++++++++++++++++++++++++");
|
|
Serial.println("");
|
|
Serial.println("Please input motor command:");
|
|
digitalWrite(dirPin, HIGH);
|
|
digitalWrite(limit_forward, HIGH);
|
|
digitalWrite(limit_back, HIGH);
|
|
// stepper.setMaxSpeed(100000); // 设置最大速度(根据你的需要调整)
|
|
// stepper.setSpeed(50); // 设置起始速度(根据你的需要调整)
|
|
}
|
|
//void loop()
|
|
//{
|
|
// startTime = millis(); // 记录开始时间
|
|
// for (int j = 0; j < 100; j++)
|
|
// {
|
|
// for (int i = 0; i < 200; i++)
|
|
// {
|
|
// digitalWrite(stepPin, HIGH);
|
|
// delayMicroseconds(motorSpeed);
|
|
// digitalWrite(stepPin, LOW);
|
|
// delayMicroseconds(motorSpeed);
|
|
// }
|
|
// }
|
|
// endTime = millis();
|
|
// executionTime = endTime - startTime;
|
|
//
|
|
// Serial.print("Execution time: ");
|
|
// Serial.print(executionTime);
|
|
// Serial.println(" ms");
|
|
//}
|
|
//void loop()
|
|
//{
|
|
// if (Serial.available())
|
|
// {
|
|
// Serial.readBytes(cmd, 10);
|
|
// Serial.println(atoi(cmd));
|
|
// motorSpeed = atoi(cmd);
|
|
//
|
|
// startTime = millis(); // 记录开始时间
|
|
// for (int j = 0; j < 100; j++)
|
|
// {
|
|
// for (int i = 0; i < 200; i++)
|
|
// {
|
|
// digitalWrite(stepPin, HIGH);
|
|
// delayMicroseconds(motorSpeed);
|
|
// digitalWrite(stepPin, LOW);
|
|
// delayMicroseconds(motorSpeed);
|
|
// }
|
|
// }
|
|
// endTime = millis();
|
|
// executionTime = endTime - startTime;
|
|
//
|
|
// Serial.print("Execution time: ");
|
|
// Serial.print(executionTime);
|
|
// Serial.println(" ms");
|
|
// }
|
|
//}
|
|
//void loop() {
|
|
// if (Serial.available() > 0) {
|
|
// char str[20];
|
|
// int STEPS;
|
|
// Serial.readBytes(str, 20);
|
|
// char *token = strtok(str, "\n"); // 使用 "\n" 作為分隔符
|
|
// if (token != NULL) {
|
|
// STEPS = atoi(token); // 將分割後的字串轉換為整數
|
|
// Serial.println(STEPS);
|
|
// } else {
|
|
// Serial.println("未找到換行符");
|
|
// }
|
|
// if (digitalRead(limit_forward) != HIGH || digitalRead(limit_back) != HIGH)
|
|
// {
|
|
// stepper.runToNewPosition(STEPS);
|
|
// Serial.println("Motor end");
|
|
// }
|
|
// }
|
|
//}
|
|
void loop() {
|
|
if (Serial.available() > 0) {
|
|
// 讀取進來的 byte
|
|
char inByte = Serial.read();
|
|
switch (inByte) {
|
|
case '1':
|
|
digitalWrite(dirPin,LOW);
|
|
while(true)
|
|
{
|
|
if(digitalRead(limit_forward) == LOW){
|
|
Serial.println("Motor emergency stop");
|
|
break;
|
|
}
|
|
digitalWrite(stepPin,HIGH);
|
|
delayMicroseconds(motorSpeed);
|
|
digitalWrite(stepPin,LOW);
|
|
delayMicroseconds(motorSpeed);
|
|
}
|
|
Serial.println("Motor end");
|
|
break;
|
|
case '2':
|
|
digitalWrite(dirPin,HIGH);
|
|
while(true)
|
|
{
|
|
if(digitalRead(limit_back) == LOW){
|
|
Serial.println("Motor emergency stop");
|
|
break;
|
|
}
|
|
digitalWrite(stepPin,HIGH);
|
|
delayMicroseconds(motorSpeed);
|
|
digitalWrite(stepPin,LOW);
|
|
delayMicroseconds(motorSpeed);
|
|
}
|
|
Serial.println("Motor end");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|