2

所以我用我的步进电机和按钮传感器做了更多的工具(我想让电机在按下按钮时停止;我大部分都知道了)。然而,为了简化我的代码,我设法让我的电机完全停止运动。在下面的代码中,想法是通过将大部分动作包含在单个动作中来浓缩电机的主要动作(将平台升到玻璃上,拍照,然后降低并重复该过程) 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();
}

请帮助我了解这段代码哪里出错了。

4

1 回答 1

2

我要改变的一件事是对数字引脚使用中断。我现在没有设置我的 Arduino,但我相信这个attachInterrupt方法会奏效。这将允许您仅在某个事件发生时(即按钮已被按下)运行代码块。

int isPressed = 0;    // Init to off state
// Other init stuff goes here.

void setup(){
    // Other setup code...

    // Set up the interrupt so that when the button is pressed, myISR function 
    // runs. I opted to run it on the rising edge (i.e. low to high).
    attachInterrupt(buttonPin, myISR, RISING); 
}

void loop(){
    // Check the state of isPressed.
    if (isPressed == 0){
        // Do stuff, let the motor move.
    }
    else{
        // This runs when the button has been pressed.
        // Turn off the flag so this runs just once.
        isPressed = 0;

        // Then stop the motor until further notice.
        stepper.stop();
        stepper.runToPosition();

        // Do other stuff now that it's off.
    }
}

// ISR = interrupt service routine
void myISR() {
    // Set the state of isPressed to ON.
    isPressed = 1;
}

此外,也许您想重新打开电机?因此,只需设置另一个 ISR,它可能会在按下时attachInterrupt(anotherButtonPin, anotherISR, RISING);调用(即与将其关闭的引脚不同的引脚)。anotherISRanotherButtonPin

这是一个停止电机的简单示例这里是中断文档。

最后,在问了其他问题后,您有什么工作吗?我建议单独保存损坏的代码并返回到有效的代码。尝试了解有什么不同。另一个建议是创建最简单的代码来停止移动的电机,比如在经过一定时间后。然后尝试集成中断,以便它控制电机的停止。祝你好运!


编辑:我还注意到您正在使用goto Lower;而不是创建Lower一个函数并像Lower();. 该goto命令通常被认为是不好的做法。看到GOTO 仍然被认为是有害的?更多细节。

于 2013-07-02T01:45:24.293 回答