-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.h
More file actions
181 lines (151 loc) · 4.04 KB
/
Copy pathModule.h
File metadata and controls
181 lines (151 loc) · 4.04 KB
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
176
177
178
179
180
181
#include <Arduino.h>
class Module {
public:
// Public vars
int motorPins[4];
bool homing;
char displayed;
bool moving;
// Module constructor
Module() {
// CONSTANTS
flapsCount = 40; // Flaps in module
stepsPerRev = 2048; // Steps per motor full revolution
stepsPerFlap = stepsPerRev / flapsCount; // Steps required to rotate one character forward
stepInterval = 1700; // Time interval between each motor step in microseconds
// VARS
stepSequenceIdx = 0;
flapStepIdx = 0;
lastStepTime = 0;
displayedIdx = 0;
moving = false;
offsetSteps = 0;
}
void setup(int hallPin, int in1, int in2, int in3, int in4, int off) {
offset = off;
// Rearrange stepper pins in order IN1-IN3-IN2-IN4
motorPins[0] = in1;
motorPins[1] = in3;
motorPins[2] = in2;
motorPins[3] = in4;
// Set all stepper pins as outputs
for (int i = 0; i < 4; i++) pinMode(motorPins[i], OUTPUT);
sensorPin = hallPin;
pinMode(sensorPin, INPUT_PULLUP); // Set hall effect sensor pin as an input with pullup resistor
}
void tick() {
if (homing) {
// If module is already on magnet before homing, rotate until not on magnet
if (preHoming) {
if (step() && !isOnMagnet()) {
preHoming = false;
}
return;
}
// Offset
if (offsetSteps) {
if (step()) {
offsetSteps--;
if (!offsetSteps) {
setHome(); // stop homing sequence
turnOffInputs();
}
}
return;
}
// Try to step and check if home position is reached
if (step() && isOnMagnet()) {
if (offset) {
offsetSteps = offset;
} else {
setHome(); // stop homing sequence
turnOffInputs();
}
}
return; // Finish homing before rotating to target character
}
// Step until target character has been reached
if (moving && step() && displayedIdx == targetIdx) {
moving = false;
turnOffInputs();
}
}
void home() {
preHoming = true;
homing = true;
}
void display(char c) {
c = toupper(c);
if (targetIdx == findCharIdx(c)) return;
targetIdx = findCharIdx(c);
// Loop back to home if target character is behind currently displayed
if (targetIdx < displayedIdx) {
home();
}
if (targetIdx != displayedIdx) {
moving = true;
}
}
private:
int sensorPin;
int flapsCount;
int stepsPerRev;
int stepsPerFlap;
int stepInterval;
int stepSequenceIdx;
int flapStepIdx;
int displayedIdx;
int targetIdx;
int offset;
int offsetSteps;
unsigned long lastStepTime;
bool preHoming;
bool stepSequence[4][4] = {
{ 1, 0, 0, 1 },
{ 0, 1, 0, 1 },
{ 0, 1, 1, 0 },
{ 1, 0, 1, 0 }
};
char chars[40] = { ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', ':', '$' };
void setHome() {
displayedIdx = 0;
displayed = chars[0];
flapStepIdx = 0;
homing = false;
}
bool step() {
unsigned long now = micros();
if (now - lastStepTime < stepInterval) return false;
// Write correct sequence to step the motor
for (int in = 0; in < 4; in++) digitalWrite(motorPins[in], stepSequence[stepSequenceIdx][in]);
// Next in sequence
stepSequenceIdx++;
if (stepSequenceIdx == 4) {
stepSequenceIdx = 0;
}
// flap step
flapStepIdx++;
if (flapStepIdx == stepsPerFlap) {
flapStepIdx = 0;
displayedIdx++;
if (displayedIdx == flapsCount) {
displayedIdx = 0;
}
displayed = chars[displayedIdx];
}
lastStepTime = now;
return true;
}
void turnOffInputs() {
for (int in = 0; in < 4; in++) {
digitalWrite(motorPins[in], LOW);
}
}
bool isOnMagnet() {
return !digitalRead(sensorPin);
}
int findCharIdx(char c) {
for (int i = 0; i < flapsCount; i++) if (chars[i] == c) return i;
return -1;
}
};