/
soldering_station.ino
228 lines (178 loc) · 6.25 KB
/
soldering_station.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
// include the library code:
#include <LiquidCrystal.h>
/*
The circuit:
* LCD RS pin to digital pin D12
* LCD Enable pin to digital pin D11
* LCD D4 pin to digital pin A5
* LCD D5 pin to digital pin A4
* LCD D6 pin to digital pin A3
* LCD D7 pin to digital pin A2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K potentiometer: ends to +5V and ground and wiper to LCD VO pin (pin 3)
*/
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, A5, A4, A3, A2);
//Encoder's pins
int pinA = 3; // Connected to D3 (Rotation)
int pinB = 4; // Connected to D4 (Rotation)
int SW = 5; // Connected to D5 (Pressing)
const int sIThermoPin = A0; // the analog input pin for reading an amplified voltage of the soldering iron's thermocouple
const int hGThermoPin = A1; // the analog input pin for reading an amplified voltage of the heat gun's thermocouple
const int sIHeatingElementPin = 8; // the output pin for controlling the soldering iron's heating element
const int hGFanPWMPin = 9; // the output pin for controlling the heat gun's fan
const int hGHeatingElementPin = 7; // the output pin for controlling the heat gun's heating element
int sIEncoderPosCount = 0;
int hGEncoderPosCount = 0;
int fanEncoderPosCount = 255;
int fan_pwm_value = 255 ;
int buttonLast=HIGH;
int buttonMenuPos=0;
int pinALast;
int aVal;
boolean bCW;
int sIThermo = 0;
int outputValue = 0;
int hGThermo = 0;
const int P_coef = 4;
const int displayRedrawCount = 100;
int cyclesCount = 0;
void setup() {
pinMode (pinA,INPUT_PULLUP);
pinMode (pinB,INPUT_PULLUP);
pinMode (SW,INPUT_PULLUP);
pinMode (hGHeatingElementPin,OUTPUT);
pinMode (sIHeatingElementPin,OUTPUT);
/* Read Pin A
Whatever state it's in will reflect the last position
*/
pinALast = digitalRead(pinA);
Serial.begin (115200);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop() {
// 2 step calib:
// 1) {Xi set temperature, Yi temperature measured by an external device} : linear fit {80,48},{100,62},{160,92},{200,123},{300,197}
// this gives us 0.672409 * x-8.56477, [1]
// 2) {Xi set temperature, Yi temperature measured by an external device} : linear fit {100,136},{150,205},{200,268},{222,300}
// this gives us 1.33191 * x + 3.48942, [2]
// Let's substitute [1] into [2]: 3.48942 + 1.33191 * (0.672409 * x-8.56477) and then let's simplify => finally we have (0.895588 * x -7.91808) , [3]
sIThermo = analogRead(sIThermoPin); // Reads the value from the specified analog pin, ADC will map input voltages between 0V and 5V into integer values between 0 and 1023
sIThermo = (int) (0.895588 *sIThermo -7.91808); // use [3]
// {Xi set temperature, Yi temperature measured by an external device}: linear fit {40,52},{80,95},{100,125},{160,195}
//1.202 * x+2.56
hGThermo = analogRead(hGThermoPin); // Reads the value from the specified analog pin, ADC will map input voltages between 0V and 5V into integer values between 0 and 1023
hGThermo = (int)(1.202*hGThermo+2.56);
int i;
//thermostats are implemented by using an on-off controller
//thermostat 1
i = sIEncoderPosCount - sIThermo;
i = i > 0 ? i : 0;
int sI_out;
if (i)
sI_out = HIGH;
else
sI_out = LOW;
digitalWrite(sIHeatingElementPin,sI_out);
//thermostat 2
i = hGEncoderPosCount - hGThermo;
i = i > 0 ? i : 0;
int hG_out;
if (i)
hG_out = HIGH;
else
hG_out = LOW;
digitalWrite(hGHeatingElementPin,hG_out);
//setting fan speed
i = fanEncoderPosCount;
i = i > 0 ? i : 0;
i = i < 255 ? i : 255;
fan_pwm_value = i;
analogWrite(hGFanPWMPin,fan_pwm_value);
// processing encoder signals
aVal = digitalRead(pinA);
if (aVal != pinALast){ // Means the knob is rotating
// if the knob is rotating, we need to determine direction
// We do that by reading pin B.
if (digitalRead(pinB) != aVal) { // Means pin A Changed first - We're Rotating Clockwise
switch(buttonMenuPos)
{
case 0:
sIEncoderPosCount += 10;
break;
case 1:
hGEncoderPosCount += 10;
break;
case 2:
fanEncoderPosCount += 10;
break;
}
bCW = true;
} else {// Otherwise B changed first and we're moving CCW
bCW = false;
switch(buttonMenuPos)
{
case 0:
sIEncoderPosCount -= 10;
break;
case 1:
hGEncoderPosCount -= 10;
break;
case 2:
fanEncoderPosCount -= 10;
break;
}
}
}
int buttonStatus = digitalRead(SW);
if ( buttonStatus == LOW && buttonStatus != buttonLast )
{
Serial.println ("Pushbutton");
buttonMenuPos = buttonMenuPos == 2 ? 0 : buttonMenuPos+1 ;
}
buttonLast = buttonStatus ;
char str[16];
char sISeparator = '-';
char hGSeparator = '-';
char fanSeparator = '=';
//refresh display and serial output
if (cyclesCount++ % displayRedrawCount == 0) { // skips some cycles, otherwise output processing will block input processing
Serial.print("sIenc = ");
Serial.print(sIEncoderPosCount);
Serial.print(" sIThermo = ");
Serial.print(sIThermo);
Serial.print(" sI_out = ");
Serial.print(sI_out);
Serial.print(" hG_out = ");
Serial.print(hG_out);
Serial.print(" hGenc = ");
Serial.print(hGEncoderPosCount);
Serial.print(" hGThermo = ");
Serial.print(hGThermo);
Serial.print(" fanEnc = ");
Serial.print(fanEncoderPosCount);
Serial.print(" hgFan = ");
Serial.print(fan_pwm_value);
Serial.print(" buttonMenuPos = ");
Serial.println(buttonMenuPos);
switch (buttonMenuPos)
{
case 0:
sISeparator = '*'; break;
case 1:
hGSeparator = '*'; break;
case 2:
fanSeparator = '*'; break;
}
sprintf(str, "S=%3d%c%3d ",sIThermo,sISeparator,sIEncoderPosCount);
lcd.setCursor(0, 0);
lcd.print(str);
sprintf(str, "H=%3d%c%3d F%c%3d%%",hGThermo,hGSeparator,hGEncoderPosCount,fanSeparator,fan_pwm_value*100/255);
lcd.setCursor(0, 1);
lcd.print(str);
}
pinALast = aVal;
}