include <Stepper.h>
const int stepsPerRevolution = 64;
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
const int aInPin=A0;
int val;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(200);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
val=analogRead(aInPin);
val=map(val,0,1023,0,99);
Serial.println(val);
// step one revolution in two direction:
if(val>50){
Serial.println("clockwise");
//原library 是整步,整步走的角度是半步走的角度的兩倍,半步走的聲音比較小
//些,啟動時最好是半步的走比較穩定
//四拍運行時步距角為θ=360 度/(revolution*4)(俗稱整步),八拍運行
//步距角為θ=360 度/(revolution*8)(俗稱半步)
myStepper.step(stepsPerRevolution/2);//
delay(500);
}
else{
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution/2);
delay(500);
}
}