-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeensy-Plasma-THC-Master.ino
427 lines (378 loc) · 12 KB
/
Teensy-Plasma-THC-Master.ino
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//Libraries
#include <FreqCount.h>
//https://github.com/PaulStoffregen/FreqCount
#include <OneButton.h>
//https://github.com/mathertel/OneButton
#include <LiquidCrystal.h>
#include <EEPROM.h>
//Floating Map Function
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//THC A-D Input must be on pin 5 on Uno or pin 13 on Teensy 3.1
//OneButton pin needs swapped if changing between Uno and Teensy, pin 13 on Uno or pin 5 on Teensy 3.1
//LCD Pins
#define LCD_RS 12
#define LCD_EN 11
#define LCD_D4 6
#define LCD_D5 4
#define LCD_D6 8
#define LCD_D7 7
#define LCD_CHARS 20
#define LCD_LINES 4
//Encoder Pins
int encoderPinA = 3;
int encoderPinB = 2;
//New OneButton
OneButton button(5,true);
//LCD Pin Setup and Custom Character
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
uint8_t testChar[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; // Custom char
//Variable Setup
int16_t TargetVal, EncoderValue, TipEncoder, HysEncoder, TargetHys, PrevEncoder;
bool flash, thcStatus;
unsigned long startMillis, torchMillis, currentMillis;
float TorchVal, PrevTorch;
int ButtonPress, tUp, tDown, tOff, minVolt, maxVolt, lastEncoded;
void setup() {
FreqCount.begin(10);
//Variable setup
flash = false;
lastEncoded = 0;
ButtonPress = 0;
tUp = 0;
tDown = 0;
tOff = 0;
minVolt = 25;
maxVolt = 175;
TargetVal = EEPROM.read(0);
TipEncoder = TargetVal;
TargetHys = EEPROM.read(1);
HysEncoder = TargetHys;
thcStatus = EEPROM.read(2);
EncoderValue = TipEncoder; //Start with TipEncoder since it is first in button statement
//Timer setup
startMillis = millis();
torchMillis = millis();
//Button setup
button.attachClick(ClickFunction); //Create Click function for button
button.attachDoubleClick(DoubleClickFunction); //Create Double Click function for button
button.attachLongPressStart(HoldFunction); //Create Hold function for button
//Output setup
pinMode(9, OUTPUT); //Torch Up output
digitalWrite(9, LOW); //Turn output off
pinMode(10, OUTPUT); //Torch Down output
digitalWrite(10, LOW); //Turn output off
//LCD Setup
lcd.begin(LCD_CHARS, LCD_LINES);
lcd.clear();
lcd.createChar(0, testChar); //Create custom full block char and send to LCD
//LCD startup message
lcd.setCursor(0,0);
lcd.print("CNC Plasma Torch");
lcd.setCursor(0,1);
lcd.print("Height Control");
lcd.setCursor(0,2);
lcd.print("Created by");
lcd.setCursor(0,3);
lcd.print("Tyler Bennett");
delay(5000);
lcd.clear();
//LCD text setup
lcd.setCursor(0, 0);
lcd.print("Tip Voltage:");
lcd.setCursor(13,0);
lcd.print("-----");
lcd.setCursor(0, 1);
lcd.print("Tip Target:");
lcd.setCursor(0, 2);
lcd.print("Hysteresis:");
if(thcStatus){
lcd.setCursor(12, 1);
lcd.print(TargetVal); //Write initial TargetVal, updated by button press
lcd.setCursor(12, 2);
lcd.print(TargetHys); //Write initial TargetHys, updated by button press
}
else {
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 2);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print("DISABLED");
lcd.setCursor(12, 2);
lcd.print("DISABLED");
}
lcd.setCursor(0,3);
lcd.print("UP:");
lcd.setCursor(6,3);
lcd.print("Down:");
lcd.setCursor(14,3);
lcd.print("Off:");
//Encoder Setup
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
//get starting position
int lastMSB = digitalRead(encoderPinA);
int lastLSB = digitalRead(encoderPinB);
//let start be lastEncoded so will index on first click
lastEncoded = (lastMSB << 1) |lastLSB;
}//Setup
void loop() {
currentMillis = millis(); //Grab current loop time, millis used to not delay code execution
button.tick(); //Check button for input
if (FreqCount.available()) {
TorchVal = mapfloat(FreqCount.read(), 1197, 9265, 0.0, 10.0); //Mapping information from Mesa THCAD
TorchVal = TorchVal*50;
if (currentMillis - torchMillis >= 250 && TorchVal != PrevTorch) { //Delay printing of TorchVal to make it easier to read and dont print if same as previous value
torchMillis = millis();
PrevTorch = TorchVal;
lcd.setCursor(12,0);
lcd.print(" ");
lcd.setCursor(13,0);
if (TorchVal < minVolt){ //Disable display below minVolt to signify no action
lcd.print("------");
} //End If
else if (TorchVal > maxVolt){ //Disable display above maxVolt to signify no action
lcd.setCursor(12,0);
lcd.print("OVER-MAX");
} //End If
else{
lcd.print(TorchVal,1);
} //End Else
} //End If
} //End If
if (ButtonPress == 1 || ButtonPress == 2) {
int MSB = digitalRead(encoderPinA); //MSB = most significant bit
int LSB = digitalRead(encoderPinB); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0010)EncoderValue ++;
if(sum == 0b1110 || sum == 0b0001)EncoderValue --;
lastEncoded = encoded; //store this value for next time
} //End If
if (ButtonPress == 1) {
if(EncoderValue != TargetVal){
if(EncoderValue <= minVolt)EncoderValue = minVolt; //Don't let EncoderValue below minVolt
if(EncoderValue >= maxVolt)EncoderValue = maxVolt; //Don't let EncoderValue above maxVolt
if(EncoderValue != PrevEncoder) {
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print(EncoderValue);
PrevEncoder = EncoderValue;
} //End If
} //End If
else if(EncoderValue != PrevEncoder){
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print(EncoderValue);
PrevEncoder = EncoderValue;
} //End Else If
//Begin code for flashing Tip Target text
if (currentMillis - startMillis >= 500 && flash) { //Hide Tip Target text
startMillis = millis();
lcd.setCursor(0, 1);
lcd.print(" ");
flash = false;
} //End If
else if(currentMillis - startMillis >= 300 && !flash) { //Show Tip Target text
startMillis = millis();
lcd.setCursor(0, 1);
lcd.print("Tip Target:");
flash = true;
} //End Else If
} //End If
else if (ButtonPress ==2){
if(EncoderValue != TargetHys){
if(EncoderValue <= 0)EncoderValue = 0; //Don't let EncoderValue below 0
if(EncoderValue >= 15)EncoderValue = 15; //Don't let EncoderValue above 15
if(EncoderValue != PrevEncoder) {
lcd.setCursor(12, 2);
lcd.print(" ");
lcd.setCursor(12, 2);
lcd.print(EncoderValue);
PrevEncoder = EncoderValue;
} //End If
} //End If
else if(EncoderValue != PrevEncoder) {
lcd.setCursor(12, 2);
lcd.print(" ");
lcd.setCursor(12, 2);
lcd.print(EncoderValue);
PrevEncoder = EncoderValue;
} //End Else If
//Begin code for flashing Tip Target text
if (currentMillis - startMillis >= 500 && flash) { //Hide Tip Target text
startMillis = millis();
lcd.setCursor(0, 2);
lcd.print(" ");
flash = false;
} //End If
else if(currentMillis - startMillis >= 300 && !flash) { //Show Tip Target text
startMillis = millis();
lcd.setCursor(0, 2);
lcd.print("Hysteresis:");
flash = true;
} //End Else If
} //End Else If
//Code for moving torch based on tip value compared to requested value, add outputs here.
if(TorchVal < (TargetVal - TargetHys) && TorchVal >= minVolt && thcStatus){ //Voltage too low, raise torch
if(tUp != 1) { //Test if output has already been set
//Turn Down off, Up on
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
tUp = 1;
tDown = 0;
tOff = 0;
lcd.setCursor(4,3);
lcd.print((char)0);
//Clear Other Squares
lcd.setCursor(12,3);
lcd.print(" ");
lcd.setCursor(19,3);
lcd.print(" ");
} //End If
} //End If
else if(TorchVal > (TargetVal + TargetHys) && TorchVal <= maxVolt && thcStatus){ //Voltage too high, lower torch
if(tDown != 1) { //Test if output has already been set
//Turn Up off, Down on
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
tUp = 0;
tDown = 1;
tOff = 0;
lcd.setCursor(12,3);
lcd.print((char)0);
//Clear Other Squares
lcd.setCursor(19,3);
lcd.print(" ");
lcd.setCursor(4,3);
lcd.print(" ");
} //End If
} //End Else If
else { //Voltage Stable - Outputs Off
if(tOff != 1) { //Test if output has already been set
//Turn both outputs off
digitalWrite(10, LOW);
digitalWrite(9, LOW);
tUp = 0;
tDown = 0;
tOff = 1;
lcd.setCursor(19,3);
lcd.print((char)0);
//Clear Other Squares
lcd.setCursor(4,3);
lcd.print(" ");
lcd.setCursor(12,3);
lcd.print(" ");
} //End If
} //End Else
}// Loop
void ClickFunction() { //Runs on single click of encoder button
if(thcStatus){
if(ButtonPress == 3)ButtonPress = 0; //Loop back around
if(ButtonPress == 1){
if(EncoderValue != TargetVal){//Encoder was changed,
TargetVal = EncoderValue;
TipEncoder = EncoderValue;
ButtonPress = -1;
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print(TargetVal);
}
else {
EncoderValue = HysEncoder; //Swap to HysEncoder so it is ready for next button press
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print(TargetVal);
}
}
if(ButtonPress == 2){
if(EncoderValue != TargetHys){
TargetHys = EncoderValue;
HysEncoder = EncoderValue;
ButtonPress = -1;
lcd.setCursor(12, 2);
lcd.print(" ");
lcd.setCursor(12, 2);
lcd.print(TargetHys);
}
else {
lcd.setCursor(12, 2);
lcd.print(" ");
lcd.setCursor(12, 2);
lcd.print(TargetHys);
}
EncoderValue = TipEncoder;
}
//Write back to LCD in case lines were blank when exiting flashing statement
lcd.setCursor(0, 1);
lcd.print("Tip Target:");
lcd.setCursor(0, 2);
lcd.print("Hysteresis:");
ButtonPress++;
}
}
void DoubleClickFunction() { //Runs on double click of encoder button
if(ButtonPress == 1) {
EncoderValue = EEPROM.read(0);
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print(EncoderValue);
}
if(ButtonPress == 2) {
EncoderValue = EEPROM.read(1);
lcd.setCursor(12, 2);
lcd.print(" ");
lcd.setCursor(12, 2);
lcd.print(EncoderValue);
}
}
void HoldFunction() { //Runs on hold of encoder button
if(ButtonPress == 1) {
EEPROM.write(0, TargetVal); //Write current TargetVal to EEPROM 0
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print("SAVED");
}
else if(ButtonPress == 2) {
EEPROM.write(1, TargetHys); //Write current TargetHys to EEPROM 1
lcd.setCursor(12, 2);
lcd.print(" ");
lcd.setCursor(12, 2);
lcd.print("SAVED");
}
else {
if (thcStatus) {
thcStatus = false;
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 2);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print("DISABLED");
lcd.setCursor(12, 2);
lcd.print("DISABLED");
EEPROM.write(2, thcStatus); //Write current thcStatus to EEPROM 2
}
else{
thcStatus = true;
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 2);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print(TargetVal);
lcd.setCursor(12, 2);
lcd.print(TargetHys);
EEPROM.write(2, thcStatus); //Write current thcStatus to EEPROM 2
}
}
}