透過限制 stepper物件 的最大速度,以加速的形式來達到改變速度的效果
用 if (stepper.distanceToGo() == 0) 來規範每一次動作確實都有做到位
stepper物件的 position 有可能會因為累加而超過 long 的長度範圍,透過完成動作後歸 0 的方式避免
#include <AccelStepper.h>
// Define a stepper and the pins it will use
//AccelStepper stepper(1, STEP, DIR);
AccelStepper stepper(1, 9, 8);
void setup(){
}
void loop(){
if (stepper.distanceToGo() == 0){
stepper.setCurrentPosition (0);
// Random change to speed, position and acceleration
// Make sure we dont get 0 speed or accelerations
delay(100);
//stepper.moveTo(rand() % 200);
stepper.move(rand() % 200);
stepper.setMaxSpeed((rand() % 200) + 1);
stepper.setAcceleration((rand() % 200) + 1);
}
stepper.run();
}