官方提供了一些庫,使Arduino入門起來更加快速,咱們連原理都不用懂,就能經過函數控制終端。可是,這樣也帶來了不少的缺陷,好比,庫函數的功能有限,有些沒法實現。而後還有庫函數由於要考慮其餘的狀況,你是四線的仍是兩線的,因而整個程序就會寫的很麻煩。git
我想用Sony無線手柄控制電機中止、順時針、逆時針轉動,按Start鍵能啓動。可是庫里根本沒有這個功能。app
還有我發現,一旦個人無線手柄裏面加入了電機的相關程序,無線手柄與接收器的通訊就會變遲鈍,每每須要按着才能等到電機反向轉動,並且有時候我改變方向,結果它轉了一圈又回到原來方向了,徹底就是亂套了。不過,中斷仍是能照常反應,可是仍是會出現延遲現象。函數
後來看了一下代碼,庫文件只有在轉完一圈纔會跳出循環,因此按鍵的消息根本沒辦法立刻反應。ui
後來就直接根據庫文件寫了函數,反應很迅速!!!!this
下面是庫文件的函數spa
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: }