官方提供了一些库,使Arduino入门起来更加快速,我们连原理都不用懂,就能通过函数控制终端。但是,这样也带来了很多的缺陷,比如,库函数的功能有限,有些无法实现。然后还有库函数因为要考虑其他的情况,你是四线的还是两线的,于是整个程序就会写的很麻烦。

我想用Sony无线手柄控制电机停止、顺时针、逆时针转动,按Start键能启动。但是库里根本没有这个功能。

还有我发现,一旦我的无线手柄里面加入了电机的相关程序,无线手柄与接收器的通信就会变迟钝,往往需要按着才能等到电机反向转动,而且有时候我改变方向,结果它转了一圈又回到原来方向了,完全就是乱套了。不过,中断还是能照常反应,但是还是会出现延迟现象。

后来看了一下代码,库文件只有在转完一圈才会跳出循环,所以按键的消息根本没办法马上反应。

后来就直接根据库文件写了函数,反应很迅速!!!!

下面是库文件的函数

   1: #ifndef Stepper_h

   2: #define Stepper_h

   3:  

   4: // library interface description

   5: class Stepper {

   6:   public:

   7:     // constructors:

   8:     Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2);

   9:     Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4);

  10:  

  11:     // speed setter method:

  12:     void setSpeed(long whatSpeed);

  13:  

  14:     // mover method:

  15:     void step(int number_of_steps);

  16:  

  17:     int version(void);

  18:  

  19:   private:

  20:     void stepMotor(int this_step);

  21:     

  22:     int direction;        // Direction of rotation

  23:     int speed;          // Speed in RPMs

  24:     unsigned long step_delay;    // delay between steps, in ms, based on speed

  25:     int number_of_steps;      // total number of steps this motor can take

  26:     int pin_count;        // whether you're driving the motor with 2 or 4 pins

  27:     int step_number;        // which step the motor is on

  28:     

  29:     // motor pin numbers:

  30:     int motor_pin_1;

  31:     int motor_pin_2;

  32:     int motor_pin_3;

  33:     int motor_pin_4;

  34:     

  35:     long last_step_time;      // time stamp in ms of when the last step was taken

  36: };

  37:  

  38: #endif

  39:  

   1: #include "Arduino.h"

   2: #include "Stepper.h"

   3:  

   4: /*

   5:  * two-wire constructor.

   6:  * Sets which wires should control the motor.

   7:  */

   8: Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)

   9: {

  10:   this->step_number = 0;      // which step the motor is on

  11:   this->speed = 0;        // the motor speed, in revolutions per minute

  12:   this->direction = 0;      // motor direction

  13:   this->last_step_time = 0;    // time stamp in ms of the last step taken

  14:   this->number_of_steps = number_of_steps;    // total number of steps for this motor

  15:   

  16:   // Arduino pins for the motor control connection:

  17:   this->motor_pin_1 = motor_pin_1;

  18:   this->motor_pin_2 = motor_pin_2;

  19:  

  20:   // setup the pins on the microcontroller:

  21:   pinMode(this->motor_pin_1, OUTPUT);

  22:   pinMode(this->motor_pin_2, OUTPUT);

  23:   

  24:   // When there are only 2 pins, set the other two to 0:

  25:   this->motor_pin_3 = 0;

  26:   this->motor_pin_4 = 0;

  27:   

  28:   // pin_count is used by the stepMotor() method:

  29:   this->pin_count = 2;

  30: }

  31:  

  32:  

  33: /*

  34:  *   constructor for four-pin version

  35:  *   Sets which wires should control the motor.

  36:  */

  37:  

  38: Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4)

  39: {

  40:   this->step_number = 0;      // which step the motor is on

  41:   this->speed = 0;        // the motor speed, in revolutions per minute

  42:   this->direction = 0;      // motor direction

  43:   this->last_step_time = 0;    // time stamp in ms of the last step taken

  44:   this->number_of_steps = number_of_steps;    // total number of steps for this motor

  45:   

  46:   // Arduino pins for the motor control connection:

  47:   this->motor_pin_1 = motor_pin_1;

  48:   this->motor_pin_2 = motor_pin_2;

  49:   this->motor_pin_3 = motor_pin_3;

  50:   this->motor_pin_4 = motor_pin_4;

  51:  

  52:   // setup the pins on the microcontroller:

  53:   pinMode(this->motor_pin_1, OUTPUT);

  54:   pinMode(this->motor_pin_2, OUTPUT);

  55:   pinMode(this->motor_pin_3, OUTPUT);

  56:   pinMode(this->motor_pin_4, OUTPUT);

  57:  

  58:   // pin_count is used by the stepMotor() method:  

  59:   this->pin_count = 4;  

  60: }

  61:  

  62: /*

  63:   Sets the speed in revs per minute

  64: 

  65: */

  66: void Stepper::setSpeed(long whatSpeed)

  67: {

  68:   this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;

  69: }

  70:  

  71: /*

  72:   Moves the motor steps_to_move steps.  If the number is negative, 

  73:    the motor moves in the reverse direction.

  74:  */

  75: void Stepper::step(int steps_to_move)

  76: {  

  77:   int steps_left = abs(steps_to_move);  // how many steps to take

  78:   

  79:   // determine direction based on whether steps_to_mode is + or -:

  80:   if (steps_to_move > 0) {this->direction = 1;}

  81:   if (steps_to_move < 0) {this->direction = 0;}

  82:     

  83:     

  84:   // decrement the number of steps, moving one step each time:

  85:   while(steps_left > 0) {

  86:   // move only if the appropriate delay has passed:

  87:   if (millis() - this->last_step_time >= this->step_delay) {

  88:       // get the timeStamp of when you stepped:

  89:       this->last_step_time = millis();

  90:       // increment or decrement the step number,

  91:       // depending on direction:

  92:       if (this->direction == 1) {

  93:         this->step_number++;

  94:         if (this->step_number == this->number_of_steps) {

  95:           this->step_number = 0;

  96:         }

  97:       } 

  98:       else { 

  99:         if (this->step_number == 0) {

 100:           this->step_number = this->number_of_steps;

 101:         }

 102:         this->step_number--;

 103:       }

 104:       // decrement the steps left:

 105:       steps_left--;

 106:       // step the motor to step number 0, 1, 2, or 3:

 107:       stepMotor(this->step_number % 4);

 108:     }

 109:   }

 110: }

 111:  

 112: /*

 113:  * Moves the motor forward or backwards.

 114:  */

 115: void Stepper::stepMotor(int thisStep)

 116: {

 117:   if (this->pin_count == 2) {

 118:     switch (thisStep) {

 119:       case 0: /* 01 */

 120:       digitalWrite(motor_pin_1, LOW);

 121:       digitalWrite(motor_pin_2, HIGH);

 122:       break;

 123:       case 1: /* 11 */

 124:       digitalWrite(motor_pin_1, HIGH);

 125:       digitalWrite(motor_pin_2, HIGH);

 126:       break;

 127:       case 2: /* 10 */

 128:       digitalWrite(motor_pin_1, HIGH);

 129:       digitalWrite(motor_pin_2, LOW);

 130:       break;

 131:       case 3: /* 00 */

 132:       digitalWrite(motor_pin_1, LOW);

 133:       digitalWrite(motor_pin_2, LOW);

 134:       break;

 135:     } 

 136:   }

 137:   if (this->pin_count == 4) {

 138:     switch (thisStep) {

 139:       case 0:    // 1010

 140:       digitalWrite(motor_pin_1, HIGH);

 141:       digitalWrite(motor_pin_2, LOW);

 142:       digitalWrite(motor_pin_3, HIGH);

 143:       digitalWrite(motor_pin_4, LOW);

 144:       break;

 145:       case 1:    // 0110

 146:       digitalWrite(motor_pin_1, LOW);

 147:       digitalWrite(motor_pin_2, HIGH);

 148:       digitalWrite(motor_pin_3, HIGH);

 149:       digitalWrite(motor_pin_4, LOW);

 150:       break;

 151:       case 2:    //0101

 152:       digitalWrite(motor_pin_1, LOW);

 153:       digitalWrite(motor_pin_2, HIGH);

 154:       digitalWrite(motor_pin_3, LOW);

 155:       digitalWrite(motor_pin_4, HIGH);

 156:       break;

 157:       case 3:    //1001

 158:       digitalWrite(motor_pin_1, HIGH);

 159:       digitalWrite(motor_pin_2, LOW);

 160:       digitalWrite(motor_pin_3, LOW);

 161:       digitalWrite(motor_pin_4, HIGH);

 162:       break;

 163:     } 

 164:   }

 165: }

 166:  

 167: /*

 168:   version() returns the version of the library:

 169: */

 170: int Stepper::version(void)

 171: {

 172:   return 4;

 173: }

关于Arduino 步进电机Stepper库的一些想法的更多相关文章

  1. arduino中SCoop库的简单应用案例

    转载:https://www.csdn.net/gather_27/MtTaggzsMDExMS1ibG9n.html arduino中SCoop库的简单应用案例首先这篇文章来在视频https://v ...

  2. arduino红外遥控库IRremote的IRsend类sendRaw函数溢出问题及其解决方法

    最近在调试红外遥控格力空调,在论坛中学到了不少东西.参考: (1)<解决问题系列(4)——红外编码分析利器使用> (2)<315Mhz模块传输替代315Mhz遥控器> 调试环境 ...

  3. 手把手教你看懂并理解Arduino PID控制库——引子

    介绍 本文主要依托于Brett Beauregard大神针对Arduino平台撰写的PID控制库Arduino PID Library及其对应的帮助博客Improving the Beginner’s ...

  4. Arduino学习——u8glib库资料整理

    第一部分,u8glib标准语法格式: 本文使用的是DFRobot出品的LCD12864 Shield V1.0 端口占用情况: SPI Com: SCK = 13, MOSI = 11, CS = 1 ...

  5. Arduino利用TimerOne库产生固定频率和占空比的方波

    TimerOne地址: https://code.google.com/archive/p/arduino-timerone/downloads ex: #include "TimerOne ...

  6. Arduino 使用舵机库时 其它引脚输出怪异 解决方案

    使用Servo.h时,不管你在初始化时用的是9还是10脚,都不要把这两个脚作为舵机以外的用途! 例: servo.attach(9); digitalWrite(10,1);//错,不能把第10脚用作 ...

  7. Arduino Micro USB库

    USBCore.cpp #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_co ...

  8. Arduino下LCD1602综合探究(下)——如何减少1602的连线,LiquidCrystal库,LiquidCrystal库中bug的解决方法

    一.前言: 上文中,笔者系统的阐述了1602的两种驱动方式,并简单的提到了Arduino的LiquidCrystal库.本文紧接上文,对以下两个问题进行更加深入的探讨:如何能够使1602对Arduin ...

  9. 如何编写自己的Arduino库?

    一开始写Arduino 的时候很不习惯,没有main函数,因为好多东西都被隐藏了.一直想搞清楚,以便编写自己的库文件.于是研究一下午,下面是一些总结. Arduino工程的初步认识 一.目录规范 当你 ...

随机推荐

  1. jquery的跳转.禁止全url跳转.只需控制器+方法

    success:function(){ window.location.href="/enterprise/show"; } success:function(){ window. ...

  2. html中的a标签特例讲解

    将自己的博客写成了一个大杂烩了,遇见啥问题就写啥问题.但是当看见自己网页的成品就特别的开心. 还记得看见过的一个故事,说是收费的东西好还是免费的东西好,有一个答案是最让我记忆深刻的.回复的一个答案是: ...

  3. CSS3 学习小结

    写样式时有时遇到浏览器兼容问题:-webkit-transition:chrome和safari-moz-transition:firefox-ms-transition:IE-o-transitio ...

  4. 新浪 股票 API

    新浪期货数据接口 [例子]http://hq.sinajs.cn/list=M0豆粕连续 M0 返回值如下:var hq_str_M0="豆粕连续,145958,3170,3190,3145 ...

  5. localStorage请使用getItem 和setITem

    最近看别人的代码,发现他们在从localStorage里面的时候喜欢用dot来操作,而不是get setItem,记得以前说过这个事.下面再说一次吧. 用dot方式来操作(   每次以'hello'= ...

  6. Java多态(二)

    public class ExtendsTest { public static void main(String[] args) { A a1 = new A(); A a2 = new B(); ...

  7. ORACLE小工具:存储过程清空所有表或使所有触发器失效

    清空所有表: CREATE OR REPLACE PROCEDURE CLEAN_TABLES as v_tablename varchar2(256); cursor cur_tablename i ...

  8. power oj/2360/Change

    题目链接[https://www.oj.swust.edu.cn/problem/show/2360] 题意:给出两个四位数A.B,目地是用最少的步骤使A变成B.变换规则如下:1.相邻的两位数可以交换 ...

  9. tree btn

    @*        <a href="javascript:void(0)" onclick="saveChecked()" ><img sr ...

  10. flume+kafka+storm单机部署

    flume-1.6.0 kafka0.9.0.0 storm0.9.6 一.部署flume 1.解压 tar -xzvf apache-flume-1.6.0-bin.tar.gz -C ../app ...