请豆包帮忙写的,调整几次之后,可以正常运行,用的是Arduino板子,豆包写梯形图的能力还不强,写这种编程语言厉害些。
// 定义步进电机控制引脚
const int stepPin = 2;
const int dirPin = 3;
// 定义启动按键引脚
const int startButtonPin = 4;
// 定义 12 个开关引脚
const int switchPins[12] = {5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2};
// 定义原点感应点位引脚
const int originPin = A3;
// 定义料件感应点位引脚
const int materialSensorPin = A4;
// 定义振动盘点位引脚
const int vibrationDiskPin = A5;
// 定义步进电机参数
const int stepsPerRevolution = 1000;  // 步进电机每转脉冲数
// 定义速度参数(毫秒)
const unsigned long normalSpeedDelay = 1;  // 正常速度延迟
const unsigned long fastSpeedDelay = 0.2;  // 快速速度延迟
const unsigned long returnSpeedDelay = 0.5; // 返回原点速度延迟
// 定义停留时间(毫秒)
const unsigned long pauseTime = 500;
// 定义变量
bool startButtonState = false;
bool lastStartButtonState = false;
bool isRunning = false;
bool isReturning = false;
int currentSegment = 0;
int remainingSteps = stepsPerRevolution;
unsigned long lastStepTime = 0;
unsigned long lastPauseTime = 0;
bool isPausing = false;
void setup() {
  // 初始化步进电机控制引脚为输出模式
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  // 初始化启动按键引脚为输入模式,并启用上拉电阻
  pinMode(startButtonPin, INPUT_PULLUP);
  // 初始化 12 个开关引脚为输入模式,并启用上拉电阻
  for (int i = 0; i < 12; i++) {
    pinMode(switchPins, INPUT_PULLUP);
  }
  // 初始化原点感应点位引脚为输入模式,并启用上拉电阻
  pinMode(originPin, INPUT_PULLUP);
  // 初始化料件感应点位引脚为输入模式,并启用上拉电阻
  pinMode(materialSensorPin, INPUT_PULLUP);
  // 初始化振动盘点位引脚为输出模式
  pinMode(vibrationDiskPin, OUTPUT);
  // 设置初始方向
  digitalWrite(dirPin, HIGH);
}
void loop() {
  // 读取启动按键状态
  startButtonState = digitalRead(startButtonPin);
  // 检测启动按键按下事件
  if (startButtonState == LOW && lastStartButtonState == HIGH) {
    isRunning = true;
    currentSegment = 0;
    remainingSteps = stepsPerRevolution;
    isReturning = false;
  }
  // 保存上一次启动按键状态
  lastStartButtonState = startButtonState;
  // 控制振动盘
  bool materialDetected = digitalRead(materialSensorPin) == LOW;
  digitalWrite(vibrationDiskPin, !materialDetected);
  // 如果电机正在运行
  if (isRunning) {
    if (isReturning) {
      unsigned long currentTime = millis();
      if (currentTime - lastStepTime >= returnSpeedDelay) {
        digitalWrite(dirPin, LOW); // 设置返回方向
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(10);  // 确保脉冲宽度
        digitalWrite(stepPin, LOW);
        lastStepTime = currentTime;
        if (digitalRead(originPin) == LOW) {
          isRunning = false;
          isReturning = false;
          digitalWrite(dirPin, HIGH); // 恢复正向
        }
      }
    } else {
      if (isPausing) {
        if (millis() - lastPauseTime >= pauseTime) {
          isPausing = false;
        }
      } else {
        unsigned long currentTime = millis();
        bool switchState = digitalRead(switchPins[currentSegment]);
        unsigned long speedDelay = switchState ? normalSpeedDelay : fastSpeedDelay;
        if (currentTime - lastStepTime >= speedDelay) {
          if (remainingSteps > 0) {
            digitalWrite(stepPin, HIGH);
            delayMicroseconds(10);  // 确保脉冲宽度
            digitalWrite(stepPin, LOW);
            remainingSteps--;
            lastStepTime = currentTime;
          }
          if (remainingSteps * 12 <= (11 - currentSegment) * stepsPerRevolution) {
            if (switchState) {
              isPausing = true;
              lastPauseTime = currentTime;
            }
            currentSegment++;
          }
          if (currentSegment >= 12) {
            isReturning = true;
          }
        }
      }
    }
  }
  // 短暂延迟以减少 CPU 负载
  delay(1);
}