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)
id - an ID of a input port(0 ~ 3)
dir - direction of rotation
dir | direction of rotation |
---|---|
0 or higher than 0 | forward |
otherwise | reverse |
speed - rotation speed in pps unit
※ pps: pulse per second
accel - acceleration in pps/s unit
The deceleration is applied with the same value with the acceleration.
#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