/*
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe
*/
#include <Stepper.h>
const int stepsPerRevolution = 64; // change this to fit the number of steps per revolution for your motor
// initialize the stepper library on pins 8 through 11:
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是整步,整步走的角度是半步走的角度的兩倍,半步走的聲音比較小,平穩(wěn)些,啟動(dòng)時(shí)最好是半步的走比較穩(wěn)定
//022中l(wèi)ibrary修改為半步模式不成功,???FUCK
//四拍運(yùn)行時(shí)步距角為θ=360度/(revolution*4)(俗稱整步),,八拍運(yùn)行時(shí)步距角為θ=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);
}
}
|