-
Notifications
You must be signed in to change notification settings - Fork 5
/
Arduino INI file
233 lines (177 loc) · 6.83 KB
/
Arduino INI file
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
/*
I am using code from SWOLEBRO THC design located here:
https://github.com/swolebro/swolebro-youtube
His youtube page is located here:
https://www.youtube.com/channel/UCRMLI3S0AFukV1tzX6Cl2Cw
I have adjusted his original code to add a 20x4 LCD using the liquid crystal display. I wanted to have it say Voltage reading,
then the Potentiometer set voltage on the next line, and finally the Voltage display coming from the plasma cutter.
This is currently the working code below in the lab. i have not live tested it yet. I will test it soon with a Harbor Freight 62204 Plasma cutter i bought a few years ago.
I also put a 50:1 voltage divider in this plasma cutter to allow me to use an Arduino THC that SWOLEBRO created.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
//Need to use for liquid crystal display LCD.---new---
//Pin layout will be in schematic
#include <LiquidCrystal.h>
//Initialize the library with the number of interface pins ---new----
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Setting the scale for the converting analogRead values to volts.
// 4.450 AREF voltage * 50 built-in voltage divider / 1023 resolution = 0.21749755 ADC counts per volt
// As far as I can tell, the arithmetic below *does* get optimized out by the compiler.
#define SCALE (4.330*50/1023)
// Threshold in ADC counts for when we say the torch is out of range.
// Multiply by SCALE for the threshold in volts.
// FYI: Some degree of buffer is needed to prevent awful see-sawing.
#define THRESH 5
// Adjustment range for the knob.
#define MINSET 110
#define MAXSET 150
// Naming other pins.
#define ADJUST A0
#define PLASMA A1
#define UP A2
#define DOWN A3
#define BUFSIZE 512 // Would technically let us do running averages up to BUFSIZE samples. In testing, shorter averages seemed better.
#define SAMP 16 // Use this many samples in the average; must be a power of 2 and no larger than BUFSIZE.
#define DISP 1024 // The number of samples to use in calculating a slower average for the display. Must also be a power of 2.
unsigned int shift = 0;
unsigned int values[BUFSIZE] = {0}; // buffer for ADC reads
unsigned long total = 0; // for keeping a rolling total of the buffer
unsigned long disp = 0; // for separately tracking ADC reads for the display
unsigned long target = 0; // voltage target, in ADC counts
// for tracking when to set opto pins
int diff = 0;
int mean = 0;
int mode = -1;
// generic temp vars
unsigned long tmp = 0;
float ftmp = 0;
float ftmp2 = 0;
// generic looping vars
int i = 0;
int j = 0;
//new lcd ---new---
//int analogPin = A1;
char formattedValue[5];
int brightness = 255;
// for the startup adjustment period
unsigned long timelimit = 0;
unsigned long ms = 0;
void setup() {
// The usual.
pinMode(ADJUST, INPUT);
pinMode(PLASMA, INPUT);
pinMode(UP, OUTPUT);
pinMode(DOWN, OUTPUT);
// Set the reference voltage to the external linear regulator
// Do a few throwaway reads so the ADC stabilizes, as recommended by the docs.
analogReference(EXTERNAL);
analogRead(PLASMA); analogRead(PLASMA); analogRead(PLASMA); analogRead(PLASMA); analogRead(PLASMA);
// We need to calculate how big the shift must be, for a given sample size.
// Since we are using bitshifting instead of division, I'm using a != here,
// so your shit will be totally broke if you don't set SAMP to a power of 2.
while((1 << shift) != SAMP)
shift++;
// Setup the LCD's columns and rows---new---;
lcd.begin(20, 4);
lcd.print ("Voltage Reading");
// Now enter the period where you can set the voltage via the potentiomenter.
// Default 5s period, plus an extension 2s as long as you keep adjusting it.
// By fixing this after boot, we save cycles from needing to do two ADC reads per loop(),
// avoid any nonsense from potentiometer drift, and don't need to think about the
// capacitance of the ADC muxer.
i=0;
ms = millis();
timelimit = ms + 5000;
while (ms < timelimit) {
tmp = analogRead(ADJUST);
// Keep a rotating total, buffer, and average. Since this value only moves
// a small amount due to noise in the AREF voltage and the physical
// potentiometer itself, 10 samples is fine.
total = total + tmp - values[i];
values[i] = tmp;
target = total / 10;
// Calculate the setpoint, based on min/max, and chop it to one decimal point.
ftmp2 = MINSET + ((MAXSET-MINSET) * (target/1023.0));
ftmp2 = ((int) (ftmp2*10))/10.0;
if (ftmp != ftmp2) {
ftmp = ftmp2;
timelimit = max(timelimit, ms + 2000);
// Print the set number Pin A0---new---
lcd.setCursor(0, 1);
lcd.print (ftmp);
}
i = (i + 1) % 10;
ms = millis();
}
// Convert the voltage target back into an int, for ADC comparison, with the scale the plasma pin uses.
target = ftmp / SCALE;
// Before carrying on, we now reset some of those variables.
for (i = 0; i < BUFSIZE; i++)
values[i] = 0;
total = 0;
i = 0;
j = 1; // Keeps display from triggering until we've done BUFSIZE samples.
}
void loop() {
tmp = analogRead(PLASMA);
disp += tmp; // non-rolling tally for the lower sample rate display
// Rolling window for a smaller sample
total = total + tmp - values[i];
values[i] = tmp;
// This mean truncates downwards. Oh well. At least it's fast.
mean = total >> shift;
diff = mean - target;
// If the mean is very low, then the plasma is turned off - it's just ADC
// noise you're seeing and it and should be ignored.
// This effectively checks if it's less than 2^4, ie. 16 counts, or ~3V with my scale factor.
if (!(mean>>4)) {
mode = 0;
digitalWrite(UP, 0);
digitalWrite(DOWN, 0);
}
// Otherwise, set pins as per reading.
// Set 0's first to turn off one direction before turning on reverse.
// We should never have both the UP and DOWN pins set to 1 - that would be nonsense.
// Checking for current setting before flipping saves a few cycles.
else if (diff > THRESH) {
if (mode != 2) {
mode = 2;
digitalWrite(UP, 0);
digitalWrite(DOWN, 1);
}
}
else if (diff < -THRESH) {
if (mode != 1) {
mode = 1;
digitalWrite(DOWN, 0);
digitalWrite(UP, 1);
}
}
else {
mode = 0;
digitalWrite(UP, 0);
digitalWrite(DOWN, 0);
}
//Print the voltage it currently reads from plasma pin A1
if (!j) {
lcd.setCursor(0, 2);
lcd.print ( (float) ((disp / DISP) * SCALE), 1 );
disp = 1;
}
// Faster than modular arithmetic, by far. Doing that drops us down to ~3kS/sec.
i = (i + 1) & (SAMP - 1);
j = (j + 1) & (DISP - 1);
}