1

ライブラリを使用しAccelStepperてステッピング モーターを制御していますが、ボタンを押したときにモーターを停止させる方法がわかりません。

コマンドからの移動が完了したらモーターを停止させることはできますが、完了moveToする前に停止させることはできません。モーターを実行するために使用している while ループ内にネストされた if ステートメントを使用してみましたが、サイコロは使用しませんでした。

そのままのコードは次のとおりです。

#include <AccelStepper.h>

const int buttonPin=4;  //number of the pushbutton pin

int buttonState=0;
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
int state = 0;
int currentPosition=0;

//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin); 

void setup(){  
    pinMode(buttonPin,INPUT);  
    stepper.setMaxSpeed(motorSpeed);
    stepper.setSpeed(motorSpeed);
    stepper.setAcceleration(motorAccel);
    stepper.moveTo(-12000); //move 2000 steps (gets close to the top)
}

void loop(){
    while( stepper.currentPosition()!=-10000)
        stepper.run();

    // move to next state
    buttonState = digitalRead(buttonPin);
    currentPosition=stepper.currentPosition();

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    //if stepper is at desired location
    if (buttonState == HIGH ){//need to find a way to alter current move to command
        stepper.stop();
        stepper.runToPosition();
        stepper.moveTo(12000);
    }

    if(stepper.distanceToGo() == 0)
        state=1;

    if(state==1){
        stepper.stop();
        stepper.runToPosition();
        stepper.moveTo(12000);
    }
    //these must be called as often as possible to ensure smooth operation
    //any delay will cause jerky motion
    stepper.run();
}
4

2 に答える 2

1

あなたがすでにそれを理解しているかどうかはわかりませんが、私はこのスレッドに出くわし、あなたの問題を解決するかもしれないし、しないかもしれない何かに気づきました. 現在、accelstepper も使用しています。

.stop を使用してモーターを停止しても、まだ新しい目的地 (stepper.moveTo(12000)) を割り当てていると感じています。その後、下部で stepper.run() コマンドを実行します、ステッパーを「12000ステップに向かって実行」させます。多分これを試してみませんか?

uint8_t button_state = 0; // cf problem 1.
void loop() {
if (digitalRead(buttonPin) == HIGH) {
    if (button_state == 0) {
        stepper.stop();
        stepper.moveTo(12000);
        button_state = 1;
    }
} else {
    button_state = 0;
}
if (stepper.distanceToGo() == 0) {
    stepper.stop();
    stepper.moveTo(12000);
}
if(button_state = 0) {
    stepper.run();
}
}

これが機能するかどうかはわかりませんが、この方法では、ボタンが押された場合、button_state 変数は、1 に設定されているときにステッパーが実行されないようにする必要があります。

お役に立てれば、

ただ通りすがりの学生

于 2013-09-25T09:28:26.110 に答える