所以我用我的步进电机和按钮传感器做了更多的工具(我想让电机在按下按钮时停止;我大部分都知道了)。然而,为了简化我的代码,我设法让我的电机完全停止运动。在下面的代码中,想法是通过将大部分动作包含在单个动作中来浓缩电机的主要动作(将平台升到玻璃上,拍照,然后降低并重复该过程) if 语句,以便计算机可以按照我的意图更好地阅读它。
#include <AccelStepper.h>
const int buttonPin=4; //number of the pushbutton pin
const int opto_shoot=2; // Pin that controls the shoot function
int maxDistance=-12000; //intial distance for motor to move
int button_state=0;
int sensorPin=0; //select input pin for the photocell
int sensorValue=0; //variable to store the vaule coming from the photocell
int motorSpeed = 9600; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 80000; //steps/second/second to accelerate
int motorDirPin = 8; //digital pin 8
int motorStepPin = 9; //digital pin 9
//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin);
void setup(){
pinMode(buttonPin,INPUT); //set that the button is an input
pinMode(opto_shoot,OUTPUT); // set the pin that controls the shoot function
stepper.setMaxSpeed(motorSpeed);
stepper.setSpeed(motorSpeed);
stepper.setAcceleration(motorAccel);
}
void loop(){
stepper.moveTo(maxDistance); //move 2000 steps (gets close to the top)
stepper.run();
if (digitalRead(buttonPin) == HIGH){
stepper.stop();
stepper.runToPosition();
digitalWrite(opto_shoot,HIGH); //SHOOT
delay(500);
digitalWrite(opto_shoot,LOW);
delay(1);
goto Lower; }
// for(int i=0;i<36;i++)
// Serial.read();
//
else{
if(stepper.distanceToGo() == 0){
stepper.stop();
stepper.runToPosition();
stepper.moveTo(maxDistance);
}
}
Lower:{
maxDistance=-1*(maxDistance+500);
stepper.moveTo(maxDistance);}
//these must be called as often as possible to ensure smooth operation
//any delay will cause jerky motion
stepper.run();
}
请帮助我了解这段代码哪里出错了。