-
Notifications
You must be signed in to change notification settings - Fork 0
/
EightAccelStepper.cpp
executable file
·175 lines (159 loc) · 4.96 KB
/
EightAccelStepper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// EightAccelStepper.cpp
// EightAccelStepper
//
// Created by Sam McHardy on 7/10/12.
// Copyright (c) 2012 Josephmark. All rights reserved.
//
#include <AccelStepper.h>
#include "EightAccelStepper.h"
EightAccelStepper::EightAccelStepper(uint8_t pins, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4) : AccelStepper(pins, pin1, pin2, pin3, pin4)
{
}
// Implements steps according to the current speed
// You must call this at least once per step
// returns true if a step occurred
boolean EightAccelStepper::runSpeed()
{
unsigned long time = millis();
if (time > getLastStepTime() + getStepInterval())
{
if (getSpeed() > 0)
{
// Clockwise
setCurrentPos(getCurrentPos() + 1);
}
else if (getSpeed() < 0)
{
// Anticlockwise
setCurrentPos(getCurrentPos() - 1);
}
if (getPins() == 8) {
step(getCurrentPos() & 0x7); // Bottom 3 bits (same as mod 8, but works with + and - numbers)
} else {
step(getCurrentPos() & 0x3); // Bottom 2 bits (same as mod 4, but works with + and - numbers)
}
setLastStepTime(time);
return true;
}
else
return false;
}
// Subclasses can override
void EightAccelStepper::step(uint8_t step)
{
switch (getPins())
{
case 0:
step0();
break;
case 1:
step1(step);
break;
case 2:
step2(step);
break;
case 4:
step4(step);
break;
case 8:
step8(step);
break;
}
}
/**
* 4 pin step function
* This is passed the current step number (0 to 3)
*/
void EightAccelStepper::step4(uint8_t step)
{
switch (step)
{
case 0: // 1010
digitalWrite(getPin(0), LOW);
digitalWrite(getPin(1), LOW);
digitalWrite(getPin(2), HIGH);
digitalWrite(getPin(3), HIGH);
break;
case 1: // 0110
digitalWrite(getPin(0), LOW);
digitalWrite(getPin(1), HIGH);
digitalWrite(getPin(2), HIGH);
digitalWrite(getPin(3), LOW);
break;
case 2: //0101
digitalWrite(getPin(0), HIGH);
digitalWrite(getPin(1), HIGH);
digitalWrite(getPin(2), LOW);
digitalWrite(getPin(3), LOW);
break;
case 3: //1001
digitalWrite(getPin(0), HIGH);
digitalWrite(getPin(1), LOW);
digitalWrite(getPin(2), LOW);
digitalWrite(getPin(3), HIGH);
break;
}
}
/**
* 8 pin step function
* This is passed the current step number (0 to 7)
* Based on pin states from
* http://www.geeetech.com/wiki/index.php/Stepper_Motor_5V_4-Phase_5-Wire_%26_ULN2003_Driver_Board_for_Arduino
*/
void EightAccelStepper::step8(uint8_t step)
{
switch (step)
{
case 0:
digitalWrite(getPin(0), LOW);
digitalWrite(getPin(1), LOW);
digitalWrite(getPin(2), LOW);
digitalWrite(getPin(3), HIGH);
break;
case 1:
digitalWrite(getPin(0), HIGH);
digitalWrite(getPin(1), LOW);
digitalWrite(getPin(2), LOW);
digitalWrite(getPin(3), HIGH);
break;
case 2:
digitalWrite(getPin(0), HIGH);
digitalWrite(getPin(1), LOW);
digitalWrite(getPin(2), LOW);
digitalWrite(getPin(3), LOW);
break;
case 3:
digitalWrite(getPin(0), HIGH);
digitalWrite(getPin(1), HIGH);
digitalWrite(getPin(2), LOW);
digitalWrite(getPin(3), LOW);
break;
case 4:
digitalWrite(getPin(0), LOW);
digitalWrite(getPin(1), HIGH);
digitalWrite(getPin(2), LOW);
digitalWrite(getPin(3), LOW);
break;
case 5:
digitalWrite(getPin(0), LOW);
digitalWrite(getPin(1), HIGH);
digitalWrite(getPin(2), HIGH);
digitalWrite(getPin(3), LOW);
break;
case 6:
digitalWrite(getPin(0), LOW);
digitalWrite(getPin(1), LOW);
digitalWrite(getPin(2), HIGH);
digitalWrite(getPin(3), LOW);
break;
case 7:
digitalWrite(getPin(0), LOW);
digitalWrite(getPin(1), LOW);
digitalWrite(getPin(2), HIGH);
digitalWrite(getPin(3), HIGH);
break;
default:
break;
}
}