用AccelStepper的函式庫,可以使用
stepper物件.moveTo(long pos);
直接讓 stepper物件 根據現在的 pos 自動計算所需的步數執行
估計是函式庫裡面有紀錄運動的counter
初始物件時這個 counter 預設為 0
可以用 stepper物件.distanceToGo() 得到還有多少步數要走
若只需走相當步數則使用
stepper物件.move(long pos);
stepper物件會依現在的位置移動所需的步數,負數則逆時針方向運動
API:http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#a68942c66e78fb7f7b5f0cdade6eb7f06
#include <AccelStepper.h>
// Define two steppers and the pins they will use
AccelStepper stepper1(1, 9, 8);
AccelStepper stepper2(1, 11, 10);
int pos1 = 100;
int pos2 = 100;
void setup(){
//M1
pinMode(6, OUTPUT);
//M2
pinMode(7, OUTPUT);
//M2,M1:LOW,LOW(最大:1,100個步進角為 2880 度)
//M2,M1:LOW,HIGH(次大:1/2,100個步進角為 1440 度)
//M2,M1:HIGH,LOW(次小大:1/4,100個步進角為 1 圈)
//M2,M1:HIGH,HIGH(最小:1/8,100個步進角為 1 圈)(default)
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
stepper1.setMaxSpeed(3000);
stepper1.setAcceleration(1000);
stepper2.setMaxSpeed(2000);
stepper2.setAcceleration(800);
}
void loop(){
if(stepper1.distanceToGo() == 0){
pos1 = -pos1;
stepper1.moveTo(pos1);
}
if(stepper2.distanceToGo() == 0){
pos2 = -pos2;
stepper2.moveTo(pos2);
}
stepper1.run();
stepper2.run();
}