中断控制电机反向转动

 1:  
 2: /*
 3: Stepper Motor Control - one revolution
 4:  
 5: This program drives a unipolar or bipolar stepper motor.
 6: The motor is attached to digital pins 8 - 11 of the Arduino.
 7:  
 8: The motor should revolve one revolution in one direction, then
 9: one revolution in the other direction.
 10:  
 11:  
 12: Created 11 Mar. 2007
 13: Modified 30 Nov. 2009
 14: by Tom Igoe
 15:  
 16: */
 17:  
 18: #include <Stepper.h>
 19:  
 20: int stepsPerRevolution = 200;
 21: int pbIn = 5;
 22:  
 23:  
 24: Stepper myStepper(stepsPerRevolution, 9,7,8,6);
 25:  
 26: void setup() {
 27: attachInterrupt(pbIn,stateChange,RISING);
 28: // set the speed at 60 rpm:
 29: myStepper.setSpeed(60);
 30: // initialize the serial port:
 31: Serial.begin(9600);
 32: }
 33:  
 34: void loop() {
 35: // step one revolution in one direction:
 36: Serial.println("clockwise");
 37: myStepper.step(stepsPerRevolution);
 38: delay(500);
 39: }
 40:  
 41: void stateChange()
 42: {
 43: stepsPerRevolution = -stepsPerRevolution;
 44: }

不知道为什么,当行程开关按下的时间不一样时,会出现这样一种怪现象:电机先反向转了一圈,然后又恢复了原来的方向,有点诡异。而且电机对开关的响应还是迟钝,大概这个也是Arduino的缺点,不过肯定有优化的方法,也可能是我对电机的库函数的理解不对!

posted @ 2013-12-16 13:32  此去经年  阅读(369)  评论(0编辑  收藏  举报