/
vapoduino_fatty.ino
319 lines (271 loc) · 8.45 KB
/
vapoduino_fatty.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
#include <Arduino.h>
/*
* Created 04/28/2014 - vapoduino.ino
*
* Vapoduino - Arduino based controller for a Vaporizer.
*
* Copyright (C) 2014 Benedikt Schlagberger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <SoftwareSerial.h>
#define MOSFET_GATE_PIN 5
#define BUTTON_PIN 3
#define LED_PIN 6
#define VIBRATOR_PIN 9
#define BATTERY_PIN A3
#define BT_RX_PIN 8
#define BT_TX_PIN 7
//#define PID_P 19 // counter-"force"
//#define PID_I 12 // counter-offset
//#define PID_D 12 // dampen controlling, react on fast changes
#define PID_P_HEATING 3.6
#define PID_I_HEATING 0.2
#define PID_D_HEATING 4
#define MAX_HEATING_POWER 255
#define VOLTAGE_DIVIDER 0.01505277f
#define MAXIMUM_VOLTAGE 12.6f
#define MINIMUM_VOLTAGE 11.0f
#define STANDBY_TIMEOUT 1500
#define TEMPERATURE_EEPROM_ADDRESS 0
#define PID_P_EEPROM_ADDRESS 1
#define PID_I_EEPROM_ADDRESS 2
#define PID_D_EEPROM_ADDRESS 3
#include <PID_v1.h>
#include <EEPROM.h>
double temp, output, desired_temp;
double pid_p, pid_i, pid_d;
PID pid(&temp, &output, &desired_temp, pid_p, pid_i, pid_d, DIRECT);
SoftwareSerial bt_serial(BT_RX_PIN, BT_TX_PIN);
boolean new_cycle;
int cycles;
int too_much_draw_counter;
void setup() {
Serial.begin(9600);
bt_serial.begin(9600);
Serial.println("#####################################");
Serial.println("##### VAPODUINO #####");
Serial.println("#####################################\n\n");
// Set pinmodes
pinMode(MOSFET_GATE_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
pinMode(VIBRATOR_PIN, OUTPUT);
desired_temp = get_desired_temp();
loadPID();
// Setting up max31865
while (!max_init()) {
error("Could not initialize sensor!");
}
// Bugfix, don't know why...
temp = max_get_temp();
while (temp < 0) {
// retrying, sometimes it takes some time
error(" Reading negative temps... Retrying");
temp = max_get_temp();
Serial.print(temp);
Serial.print(" ");
}
// Start PID control
temp = max_get_temp();
pid.SetMode(AUTOMATIC);
new_cycle = true;
too_much_draw_counter = 0;
digitalWrite(LED_PIN, HIGH);
vibrate_for_ms(500);
digitalWrite(LED_PIN, LOW);
}
void loop() {
temp = max_get_temp();
digitalWrite(LED_PIN, LOW);
digitalWrite(VIBRATOR_PIN, LOW);
if (digitalRead(BUTTON_PIN) == HIGH) {
// reset standby timeout
cycles = 0;
if (new_cycle) {
heatUpChamber();
new_cycle = false;
pid = PID(&temp, &output, &desired_temp, pid_p, pid_i, pid_d, DIRECT);
pid.SetOutputLimits(0, 255);
pid.SetMode(AUTOMATIC);
}
pid.Compute();
analogWrite(MOSFET_GATE_PIN, output);
if (output == 255) {
too_much_draw_counter++;
if (too_much_draw_counter > 20) {
digitalWrite(LED_PIN, HIGH);
digitalWrite(VIBRATOR_PIN, HIGH);
}
} else {
too_much_draw_counter = 0;
digitalWrite(LED_PIN, LOW);
digitalWrite(VIBRATOR_PIN, LOW);
}
} else {
cycles++;
// count till timeout
if (cycles == STANDBY_TIMEOUT) {
vibrate_for_ms(1000);
cycles = 0;
}
pid.SetMode(MANUAL);
output = 0;
analogWrite(MOSFET_GATE_PIN, output);
digitalWrite(LED_PIN, LOW);
new_cycle = true;
// debouncing the button
delay(20);
}
printStatus();
check_serial();
delay(50);
}
void heatUpChamber() {
// heat up chamber to desired_temp
double heating_temp = desired_temp * 1.05;
pid = PID(&temp, &output, &heating_temp, PID_P_HEATING, PID_I_HEATING, PID_D_HEATING, DIRECT);
pid.SetOutputLimits(0, MAX_HEATING_POWER);
pid.SetMode(AUTOMATIC);
while (digitalRead(BUTTON_PIN) == HIGH && temp < desired_temp) {
temp = max_get_temp();
digitalWrite(LED_PIN, HIGH);
pid.Compute();
analogWrite(MOSFET_GATE_PIN, output);
printStatus();
delay(50);
}
if (temp > desired_temp) {
vibrate_for_ms(200);
}
}
void printStatus() {
int raw = analogRead(A3);
// Serial.print("Desired Temp: ");
// Serial.print((int) desired_temp);
// Serial.print(", Temp: ");
// Serial.print(temp);
// Serial.print(", P: ");
// Serial.print(pid_p);
// Serial.print(", I: ");
// Serial.print(pid_i);
// Serial.print(", D: ");
// Serial.print(pid_d);
// Serial.print(", Heat: ");
// Serial.print(output);
// Serial.print(", Volts: ");
// Serial.print(raw * VOLTAGE_DIVIDER);
// Serial.print(", Percents: ");
// Serial.println(get_battery_percents(raw * VOLTAGE_DIVIDER));
bt_serial.print("s;");
bt_serial.print(temp);
bt_serial.print(";");
bt_serial.print(output);
bt_serial.print(";");
bt_serial.print(raw * VOLTAGE_DIVIDER);
bt_serial.print(";");
bt_serial.println(get_battery_percents(raw * VOLTAGE_DIVIDER));
}
void error(String message) {
Serial.println(message);
digitalWrite(MOSFET_GATE_PIN, LOW);
}
void check_serial() {
if (bt_serial.available()) {
digitalWrite(MOSFET_GATE_PIN, LOW);
char command = bt_serial.read();
switch (command) {
case 't':
while (!bt_serial.available());
command = bt_serial.read();
switch (command) {
case '=':
while (!bt_serial.available());
set_desired_temp(bt_serial.read());
case '?':
bt_serial.print("t;");
bt_serial.println(EEPROM.read(TEMPERATURE_EEPROM_ADDRESS));
break;
default:
bt_serial.println("Unknown Command");
}
break;
case 'p':
while (!bt_serial.available());
command = bt_serial.read();
switch (command) {
case '=':
check_PID();
case '?':
bt_serial.print("p;");
bt_serial.print((float) EEPROM.read(PID_P_EEPROM_ADDRESS) / 10);
bt_serial.print(";");
bt_serial.print((float) EEPROM.read(PID_I_EEPROM_ADDRESS) / 10);
bt_serial.print(";");
bt_serial.println((float) EEPROM.read(PID_D_EEPROM_ADDRESS) / 10);
break;
default:
bt_serial.println("Unknown Command");
}
default:
bt_serial.println("Unknown Command");
}
}
}
void check_PID() {
while (!bt_serial.available());
byte p = bt_serial.read();
while (!bt_serial.available());
byte i = bt_serial.read();
while (!bt_serial.available());
byte d = bt_serial.read();
setPID(p, i, d);
}
float get_desired_temp() {
return (float) EEPROM.read(TEMPERATURE_EEPROM_ADDRESS);
}
void loadPID() {
pid_p = (double) EEPROM.read(PID_P_EEPROM_ADDRESS) / 10;
pid_i = (double) EEPROM.read(PID_I_EEPROM_ADDRESS) / 10;
pid_d = (double) EEPROM.read(PID_D_EEPROM_ADDRESS) / 10;
}
void setPID(byte p, byte i, byte d) {
EEPROM.write(PID_P_EEPROM_ADDRESS, p);
EEPROM.write(PID_I_EEPROM_ADDRESS, i);
EEPROM.write(PID_D_EEPROM_ADDRESS, d);
loadPID();
}
int get_battery_percents(float voltage) {
int percent = (voltage - MINIMUM_VOLTAGE) / (MAXIMUM_VOLTAGE - MINIMUM_VOLTAGE) * 100;
return percent < 0 ? 0 : percent;
}
void set_desired_temp(byte new_temp) {
EEPROM.write(TEMPERATURE_EEPROM_ADDRESS, new_temp);
desired_temp = (float) new_temp;
}
void delay_with_interrupt(int time, int interrupting_button_state) {
int counter = 0;
while (digitalRead(BUTTON_PIN) != interrupting_button_state & counter < time) {
counter++;
delay(1);
}
// debounce
delay(20);
}
void vibrate_for_ms(int ms) {
digitalWrite(VIBRATOR_PIN, HIGH);
delay(ms);
digitalWrite(VIBRATOR_PIN, LOW);
}