Stop automatically


Stop automatically

You can automatically stop a stepper motor when a limit switch is closed by using stepGotoSW() function. The state of a motor will be stop if it stopped by this function.

step.stepGotoSW(id, dir)
step.stepGotoSW(id, dir, speed)
step.stepGotoSW(id, dir, speed, accel)

Example

#include <PhpocExpansion.h>
#include <Phpoc.h>

byte spcId = 1;

ExpansionStepper step(spcId);

void setup() {
    Serial.begin(9600);
    while(!Serial)
        ;

    Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
    Expansion.begin();

    Serial.println(step.getPID());
    Serial.println(step.getName());

    step.setMode(4);
    step.setVrefDrive(8);
    step.setSpeed(400);
    step.setAccel(0);

    // rotate until digital input 0 is LOW
    Serial.print("find positive limit ...");
    step.stepGotoSW(0, 1);
    while(step.getState()) {
        delay(1);
    }
    Serial.println("done");

    delay(1000);

    // rotate until digital input 1 is LOW
    Serial.print("find negative limit ...");
    step.stepGotoSW(1, -1);
    while(step.getState()) {
        delay(1);
    }
    Serial.println("done");
}

void loop() {

}
find positive limit ...done
find negative limit ...done