-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.cpp
334 lines (295 loc) · 8.39 KB
/
controller.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <Arduino.h>
#include <math.h>
// this code was written to control KNF NF-12 pumps with a Teensy 3.5 microcontroller
// WHITE WIRES, SPEED CONTROL
#define PIN_1P_PWM 2
#define PIN_1N_PWM 3
// #define PIN_2P_PWM 4
// #define PIN_2N_PWM 5
// #define PIN_3P_PWM 6
// #define PIN_3N_PWM 7
// #define PIN_4P_PWM 8
// #define PIN_4N_PWM 9
// #define PIN_5P_PWM 10
// #define PIN_5N_PWM 29
// GREEN WIRES, PUMP SPEED OUTPUT FREQUENCY
#define PIN_1P_TACHO 23
#define PIN_1N_TACHO 22
// #define PIN_2P_TACHO 21
// #define PIN_2N_TACHO 20
// #define PIN_3P_TACHO 19
// #define PIN_3N_TACHO 18
// #define PIN_4P_TACHO 17
// #define PIN_4N_TACHO 16
// #define PIN_5P_TACHO 15
// #define PIN_5N_TACHO 36
// INPUT PUMP SPEED/DUTY CYCLE, 0-255 is 0-100%, PUMPS MINIMUM SPEED STARTS FROM ~3.8 - 7.6 %
int speed1P = 0;
int speed1N = 0;
// int speed2P = 0;
// int speed2N = 0;
// int speed3P = 0;
// int speed3N = 0;
// int speed4N = 0;
// int speed4P = 0;
// int speed5N = 0;
// int speed5P = 0;
// OUTPUT PUMP SPEED
unsigned long rpm1P = 0;
unsigned long rpm1N = 0;
// unsigned long rpm2P = 0;
// unsigned long rpm2N = 0;
// unsigned long rpm3P = 0;
// unsigned long rpm3N = 0;
// unsigned long rpm4N = 0;
// unsigned long rpm4P = 0;
// unsigned long rpm5N = 0;
// unsigned long rpm5P = 0;
//PUMP COUNTERS
unsigned long count1P = 0;
unsigned long count1N = 0;
// unsigned long count2P = 0;
// unsigned long count2N = 0;
// unsigned long count3P = 0;
// unsigned long count3N = 0;
// unsigned long count4N = 0;
// unsigned long count4P = 0;
// unsigned long count5N = 0;
// unsigned long count5P = 0;
const byte buffSize = 40;
char inputBuffer[buffSize];
const char startMarker = '<';
const char endMarker = '>';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;
char messageFromPC[buffSize] = {0};
char cmd;
String val;
const int updateInterval = 1000; // serial update time period (ms)
elapsedMillis updateTimer = 0;
bool ledState = LOW;
void parseData()
{
cmd = strtok(inputBuffer, ",")[0];
val = strtok(NULL, ",");
}
void recieveFromPC()
{
// receive data from PC and save it into inputBuffer
if (Serial.available() > 0)
{
char x = Serial.read();
// the order of these IF clauses is significant
if (x == endMarker)
{
readInProgress = false;
newDataFromPC = true;
inputBuffer[bytesRecvd] = 0;
parseData();
}
if (readInProgress)
{
inputBuffer[bytesRecvd] = x;
bytesRecvd++;
if (bytesRecvd == buffSize)
{
bytesRecvd = buffSize - 1;
}
}
if (x == startMarker)
{
bytesRecvd = 0;
readInProgress = true;
}
}
}
void ISR_1P()
{
count1P++;
}
void ISR_1N()
{
count1N++;
}
// void ISR_2P()
// {
// count2P++;
// }
// void ISR_2N()
// {
// count2N++;
// }
// void ISR_3P()
// {
// count3P++;
// }
// void ISR_3N()
// {
// count3N++;
// }
// void ISR_4P()
// {
// count4P++;
// }
// void ISR_4N()
// {
// count4N++;
// }
// void ISR_5P()
// {
// count5P++;
// }
// void ISR_5N()
// {
// count5N++;
// }
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, ledState);
pinMode(PIN_1P_PWM, OUTPUT);
pinMode(PIN_1N_PWM, OUTPUT);
// pinMode(PIN_2P_PWM, OUTPUT);
// pinMode(PIN_2N_PWM, OUTPUT);
// pinMode(PIN_3P_PWM, OUTPUT);
// pinMode(PIN_3N_PWM, OUTPUT);
// pinMode(PIN_4P_PWM, OUTPUT);
// pinMode(PIN_4N_PWM, OUTPUT);
// pinMode(PIN_5P_PWM, OUTPUT);
// pinMode(PIN_5N_PWM, OUTPUT);
analogWriteFrequency(PIN_1P_PWM, 10000);
analogWriteFrequency(PIN_1N_PWM, 10000);
// analogWriteFrequency(PIN_2P_PWM, 10000);
// analogWriteFrequency(PIN_2N_PWM, 10000);
// analogWriteFrequency(PIN_3P_PWM, 10000);
// analogWriteFrequency(PIN_3N_PWM, 10000);
// analogWriteFrequency(PIN_4P_PWM, 10000);
// analogWriteFrequency(PIN_4N_PWM, 10000);
// analogWriteFrequency(PIN_5P_PWM, 10000);
// analogWriteFrequency(PIN_5N_PWM, 10000);
analogWrite(PIN_1P_PWM, 0);
analogWrite(PIN_1N_PWM, 0);
// analogWrite(PIN_2P_PWM, 0);
// analogWrite(PIN_2N_PWM, 0);
// analogWrite(PIN_3P_PWM, 0);
// analogWrite(PIN_3N_PWM, 0);
// analogWrite(PIN_4P_PWM, 0);
// analogWrite(PIN_4N_PWM, 0);
// analogWrite(PIN_5P_PWM, 0);
// analogWrite(PIN_5N_PWM, 0);
attachInterrupt(digitalPinToInterrupt(PIN_1P_TACHO), ISR_1P, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_1N_TACHO), ISR_1N, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_2P_TACHO), ISR_2P, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_2N_TACHO), ISR_2N, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_3P_TACHO), ISR_3P, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_3N_TACHO), ISR_3N, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_4P_TACHO), ISR_4P, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_4N_TACHO), ISR_4N, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_5P_TACHO), ISR_5P, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_5N_TACHO), ISR_5N, FALLING);
}
void controlPumps()
{
if (newDataFromPC)
{
switch (cmd)
{
case 'a':
flowRateA = val.toInt();
analogWrite(PIN_1P_PWM, flowRateA);
break;
case 'b':
flowRateB = val.toInt();
analogWrite(PIN_1N_PWM, flowRateB);
break;
// case 'c':
// analogWrite(PIN_2P_PWM, val.toInt());
// break;
// case 'd':
// analogWrite(PIN_2N_PWM, val.toInt());
// break;
// case 'e':
// analogWrite(PIN_3P_PWM, val.toInt());
// break;
// case 'f':
// analogWrite(PIN_3N_PWM, val.toInt());
// break;
// case 'g':
// analogWrite(PIN_4P_PWM, val.toInt());
// break;
// case 'h':
// analogWrite(PIN_4N_PWM, val.toInt());
// break;
// case 'i':
// analogWrite(PIN_5P_PWM, val.toInt());
// break;
// case 'j':
// analogWrite(PIN_5N_PWM, val.toInt());
// break;
}
}
}
void replyToPC()
{
if (updateTimer >= updateInterval)
{
detachInterrupt(digitalPinToInterrupt(PIN_1P_TACHO));
detachInterrupt(digitalPinToInterrupt(PIN_1N_TACHO));
// detachInterrupt(digitalPinToInterrupt(PIN_2P_TACHO));
// detachInterrupt(digitalPinToInterrupt(PIN_2N_TACHO));
// detachInterrupt(digitalPinToInterrupt(PIN_3P_TACHO));
// detachInterrupt(digitalPinToInterrupt(PIN_3N_TACHO));
// detachInterrupt(digitalPinToInterrupt(PIN_4P_TACHO));
// detachInterrupt(digitalPinToInterrupt(PIN_4N_TACHO));
// detachInterrupt(digitalPinToInterrupt(PIN_5P_TACHO));
// detachInterrupt(digitalPinToInterrupt(PIN_5N_TACHO));
rpm1P = count1P * 10;
rpm1N = count1N * 10;
// rpm2P = count2P * 10;
// rpm2N = count2N * 10;
// rpm3P = count3P * 10;
// rpm3N = count3N * 10;
// rpm4N = count4N * 10;
// rpm4P = count4P * 10;
// rpm5N = count5N * 10;
// rpm5P = count5P * 10;
Serial.print(flowRateA);
Serial.print(',');
Serial.print(flowRateB);
Serial.print(',');
Serial.print(rpm1P);
Serial.print(',');
Serial.println(rpm1N);
updateTimer = 0;
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
count1P = 0;
count1N = 0;
// count2P = 0;
// count2N = 0;
// count3P = 0;
// count3N = 0;
// count4N = 0;
// count4P = 0;
// count5N = 0;
// count5P = 0;
attachInterrupt(digitalPinToInterrupt(PIN_1P_TACHO), ISR_1P, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_1N_TACHO), ISR_1N, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_2P_TACHO), ISR_2P, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_2N_TACHO), ISR_2N, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_3P_TACHO), ISR_3P, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_3N_TACHO), ISR_3N, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_4P_TACHO), ISR_4P, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_4N_TACHO), ISR_4N, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_5P_TACHO), ISR_5P, FALLING);
// attachInterrupt(digitalPinToInterrupt(PIN_5N_TACHO), ISR_5N, FALLING);
}
}
void loop()
{
recieveFromPC();
controlPumps();
replyToPC();
}