Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 我不能让我的步进电机通过我为它建立的回路_Arduino - Fatal编程技术网

Arduino 我不能让我的步进电机通过我为它建立的回路

Arduino 我不能让我的步进电机通过我为它建立的回路,arduino,Arduino,所以我用我的步进电机和按钮传感器做了更多的工作,我想在按下按钮时让电机停止;我大部分都已经弄明白了。然而,为了简化我的代码,我设法让我的马达完全停止运动。在随后的代码中,这个想法是浓缩电机的主要动作,将平台升高到玻璃上,拍摄一张照片,然后降低,并通过将大部分动作包含在一个if语句中来重复这个过程,以便计算机能够更好地按照我的意图读取它 #include <AccelStepper.h> const int buttonPin=4; //number of the pushbutto

所以我用我的步进电机和按钮传感器做了更多的工作,我想在按下按钮时让电机停止;我大部分都已经弄明白了。然而,为了简化我的代码,我设法让我的马达完全停止运动。在随后的代码中,这个想法是浓缩电机的主要动作,将平台升高到玻璃上,拍摄一张照片,然后降低,并通过将大部分动作包含在一个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();
}

请帮助我了解我在使用此代码时哪里出了问题

我要改变的一件事是对数字管脚使用中断。我现在还没有设置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,可能会连接另一个按钮,另一个按钮,上升;当按下另一个按钮时,即一个与关闭按钮不同的别针时,该按钮称为另一个

这里有一个简单的例子,它停止了一个电机,并且是中断文件

最后,你问了之后有没有什么有用的东西?我建议您单独保存损坏的代码,然后返回工作状态。试着去理解什么是不同的。另一个建议是创建最简单的代码来停止移动的电机,比如说经过一定时间后。然后尝试整合中断,使其控制电机停止。祝你好运


编辑:我还注意到你正在使用GoToLower;而不是将Lower作为函数并像Lower;一样调用它;。“转到”命令通常被认为是不好的做法。有关更多详细信息,请参阅。

和另一个+1用于goto bashing!:-是的,我自己使用它感觉很糟糕,但是我和另一个程序员一起工作,他建议我用它来完成我正在做的事情。与我的另一位程序员朋友合作,代码目前主要基于函数,似乎基本上做了我想做的事情。谢谢你的帮助!“我将不得不提前支付这笔费用,并努力在未来为我的程序员同事提供更多帮助。@MikePinnell不客气,希望能有所帮助。”。另外,不要忘了点击任何最能回答您问题的答案上的“接受”+你代表2次,回答者代表+15