Probleme bei meinem Schrittmotor zu reagieren auf meine Schaltfläche sensor im Arduino-sketch

Ich bin mit der AccelStepper Bibliothek kann meinen Schrittmotor, und ich habe Schwierigkeiten, herauszufinden, wie man meinen motor zu stoppen, wenn my button gedrückt wird.

Bekomme ich den motor zu stoppen, sobald es schließt seine gesamte Bewegung von der moveTo Befehl, aber ich kann nicht ankommen es zu beenden, bevor es endet. Ich habe versucht, mit einer if-Anweisung geschachtelt innerhalb der while-Schleife verwende ich um den motor zu laufen, aber keine Würfel.

Der code, wie es steht, ist wie folgt:

#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();
}

InformationsquelleAutor MikePinnell | 2013-06-29

Schreibe einen Kommentar