您的位置:首页 > 其它

中断控制电机反向转动

2013-12-16 13:32 344 查看
[code] [code] 


/*


Stepper Motor Control - one revolution




This program drives a unipolar or bipolar stepper motor.


The motor is attached to digital pins 8 - 11 of the Arduino.




The motor should revolve one revolution in one direction, then


one revolution in the other direction.






Created 11 Mar. 2007


Modified 30 Nov. 2009


by Tom Igoe




*/


 


#include <Stepper.h>


 


int stepsPerRevolution = 200;


int pbIn = 5;


 


 


Stepper myStepper(stepsPerRevolution, 9,7,8,6);


 


void setup(){


attachInterrupt(pbIn,stateChange,RISING);


// set the speed at 60 rpm:


myStepper.setSpeed(60);


// initialize the serial port:


Serial.begin(9600);


}


 


void loop(){


// step one revolution in one direction:


Serial.println("clockwise");


myStepper.step(stepsPerRevolution);


delay(500);


}


 


void stateChange()


{


stepsPerRevolution = -stepsPerRevolution;


}

[/code]
[/code]

不知道为什么,当行程开关按下的时间不一样时,会出现这样一种怪现象:电机先反向转了一圈,然后又恢复了原来的方向,有点诡异。而且电机对开关的响应还是迟钝,大概这个也是Arduino的缺点,不过肯定有优化的方法,也可能是我对电机的库函数的理解不对!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: