中断控制电机反向转动

 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的缺点,不过肯定有优化的方法,也可能是我对电机的库函数的理解不对!

转载于:https://www.cnblogs.com/zjjsxuqiang/p/3476605.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值