-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZamnieta_komora_z_menu.ino
306 lines (262 loc) · 7.28 KB
/
Zamnieta_komora_z_menu.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
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DS18B20.h>
#include <AM2320.h>
#include "HX711.h"
#include <PID_v1.h>
AM2320 th;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //definicja wyświetlacza
// definicja przycisków
#define BTN_BACK 11
#define BTN_NEXT 10
#define BTN_PREV 8
#define BTN_OK 9
#define ONEWIRE_PIN 4 // czujnik temp 18b20
#define SENSORS_NUM 3 // ilosc czujnikow 18b20
#define calibration_factor -2400 //Wartosc uzyskana za pomoca SparkFun_HX711_Calibration sketch
#define zero_factor 9959920//16180400 //wartosc uzyska za pomoca SparkFun_HX711_Calibration sketch
//definicja pinow dla HX711
#define DOUT 3
#define CLK 2
HX711 scale(DOUT, CLK);
// Adresy czujnikow DS18B20 dla magistrali one wire
const byte address[SENSORS_NUM][8] PROGMEM = {
0x28, 0xAC, 0x71, 0x7B, 0x5, 0x0, 0x0, 0x96,
0x28, 0x61, 0x64, 0x11, 0xA9, 0x88, 0x81, 0xA0,
0x28, 0x61, 0x64, 0x11, 0xA9, 0xB7, 0x74, 0xDE
};
OneWire onewire(ONEWIRE_PIN);
DS18B20 sensors(&onewire);
int czujnik[3];
float czujnikAM[2];
//zmienne dla regulatorow PID
int wentylator_gora = 6;
int wentylator_dol = 12;
double Setpoint, Input, Output;
double Setpoint1, Input1, Output1;
//Nastawy regulatorow PID
double Kp=80, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, REVERSE);
double Kp1=80, Ki1=5, Kd1=1;
PID myPID1(&Input1, &Output1, &Setpoint1, Kp1, Ki1, Kd1, REVERSE);
//struktura danych dla menu
typedef struct {
String label;
int minVal;
int maxVal;
int currentVal;
void (*handler)();
} STRUCT_MENUPOS;
typedef enum {
BACK, NEXT, PREV, OK, NONE
} ENUM_BUTTON;
STRUCT_MENUPOS menu[6]; //liczba pozycji menu
int currentMenuPos = 0;
int menuSize;
bool isInLowerLevel = false;
int tempVal;
void setup() {
Serial.begin(9600);
lcd.begin(20, 4);
lcd.setBacklight(255);
sensors.begin();
sensors.request();
scale.set_scale(calibration_factor); //ustawienie wagi na 0
scale.set_offset(zero_factor); //ustawienie wagi na 0 przy znanej wartosci
pinMode(5, OUTPUT);
pinMode(BTN_NEXT, INPUT_PULLUP);
pinMode(BTN_PREV, INPUT_PULLUP);
pinMode(BTN_BACK, INPUT_PULLUP);
pinMode(BTN_OK, INPUT_PULLUP);
pinMode(wentylator_gora, OUTPUT);
//Wejscie i wartosc zadana dla PID
Input = (czujnikAM[1]+czujnik[2])/2; //srednia z dwoch czujnikow wewnatrz strefy wydruku
Setpoint = (menu[2].currentVal + menu[4].currentVal)/2 ;
Input1 = czujnik[1];
Setpoint1 = menu[3].currentVal;
//wlaczenie PIDow w trybie automatycznym
myPID.SetMode(AUTOMATIC);
myPID1.SetMode(AUTOMATIC);
//definicja Menu
menu[0] = {"Parametry", 0, 1, 5, parametry};
menu[1] = {"Oswietlenie LED", 0, 1, 0, oswietlenieLed};
menu[2] = {"Ustaw temp srodek ", 0, 100, 30, NULL};
menu[3] = {"Ustaw temp dol", 0, 100, 30, NULL};
menu[4] = {"Ustaw temp gora", 0, 100, 30, NULL};
menu[5] = {"Ustaw czas druku", 0, 100, 40, NULL};
menuSize = sizeof(menu)/sizeof(STRUCT_MENUPOS);
}
void loop() {
drawMenu();
if(currentMenuPos == 0) {
parametry();
}
Input = (czujnikAM[1]+czujnik[0])/2;
Setpoint = (menu[2].currentVal + menu[4].currentVal)/2 ;
Input1 = czujnik[1];
Setpoint1 = menu[3].currentVal;
//Obliczenie PIDow
myPID.Compute();
myPID1.Compute();
//Ograniczenie sygnału PWM wysterowania ( przy małych prędkościach obrotowych wentylatory wydają piskliwe dzwięki)
if (Output >= 130){
analogWrite(wentylator_gora,Output);
}
else
{
analogWrite(wentylator_gora,0);
}
if (Output1 >= 130){
analogWrite(wentylator_dol,Output1);
}
else
{
analogWrite(wentylator_dol,0);
}
//wyswietlanie wartosci regulatorow PID za pomoca portu szeregowego ( w celach diagnostycznych)
Serial.print(Input);
Serial.print(" ");
Serial.print(Setpoint);
Serial.print(" ");
Serial.print(Output);
Serial.print(" ");
Serial.print(Input1);
Serial.print(" ");
Serial.print(Setpoint1);
Serial.print(" ");
Serial.println(Output1);
delay(100);
}
ENUM_BUTTON getButton() {
if(!digitalRead(BTN_BACK)) return BACK;
if(!digitalRead(BTN_NEXT)) return NEXT;
if(!digitalRead(BTN_PREV)) return PREV;
if(!digitalRead(BTN_OK)) return OK;
return NONE;
}
void drawMenu() {
//odczyt z czujnika AM2320
th.Read();
czujnikAM[0]=th.h;
czujnikAM[1]=th.t;
for (byte i=0; i<SENSORS_NUM; i++){
czujnik[i]=sensors.readTemperature(FA(address[i]));
}
sensors.request();
static unsigned long lastRead = 0;
static ENUM_BUTTON lastPressedButton = OK;
static unsigned int isPressedSince = 0;
int autoSwitchTime = 500;
ENUM_BUTTON pressedButton = getButton();
if(pressedButton == NONE && lastRead != 0) {
isPressedSince = 0;
return;
}
if(pressedButton != lastPressedButton) {
isPressedSince = 0;
}
if(isPressedSince > 3) autoSwitchTime = 70;
if(lastRead != 0 && millis() - lastRead < autoSwitchTime && pressedButton == lastPressedButton) return;
isPressedSince++;
lastRead = millis();
lastPressedButton = pressedButton;
switch(pressedButton) {
case NEXT: handleNext(); break;
case PREV: handlePrev(); break;
case BACK: handleBack(); break;
case OK: handleOk(); break;
}
lcd.home();
lcd.clear();
if(isInLowerLevel && currentMenuPos != 0 ) {
lcd.print(menu[currentMenuPos].label);
if(menu[currentMenuPos].handler != NULL) {
(*(menu[currentMenuPos].handler))();
} else {
lcd.setCursor(0, 1);
lcd.print(tempVal);
}
} else if (currentMenuPos != 0) {
lcd.print(menu[currentMenuPos].label);
}
}
void handleNext() {
if(isInLowerLevel) {
tempVal++;
if(tempVal > menu[currentMenuPos].maxVal) tempVal = menu[currentMenuPos].maxVal;
} else {
currentMenuPos = (currentMenuPos + 1) % menuSize;
}
}
void handlePrev() {
if(isInLowerLevel) {
tempVal--;
if(tempVal < menu[currentMenuPos].minVal) tempVal = menu[currentMenuPos].minVal;
} else {
currentMenuPos--;
if(currentMenuPos < 0) currentMenuPos = menuSize - 1;
}
}
void handleBack() {
if(isInLowerLevel) {
isInLowerLevel = false;
}
}
void handleOk() {
if(menu[currentMenuPos].handler != NULL && menu[currentMenuPos].maxVal <= menu[currentMenuPos].minVal) {
(*(menu[currentMenuPos].handler))();
return;
}
if(isInLowerLevel) {
menu[currentMenuPos].currentVal = tempVal;
isInLowerLevel = false;
} else {
tempVal = menu[currentMenuPos].currentVal;
isInLowerLevel = true;
}
}
void parametry() {
lcd.setCursor(0,0);
lcd.print(menu[2].currentVal);
lcd.print("|");
lcd.print(czujnik[0]);
lcd.setCursor(5,0);
lcd.print("*C");
lcd.setCursor(0,2);
lcd.print(menu[3].currentVal);
lcd.print("|");
lcd.print(czujnik[1]);
lcd.setCursor(5,2);
lcd.print("*C");
lcd.setCursor(0,1);
lcd.print(menu[4].currentVal);
lcd.print("|");
lcd.print(czujnikAM[1],0);
lcd.setCursor(5,1);
lcd.print("*C");
lcd.setCursor(9,0);
lcd.print(czujnikAM[0],0);
lcd.setCursor(11,0);
lcd.print("%RH");
lcd.setCursor(9,1);
lcd.print(czujnik[2]);
lcd.setCursor(11,1);
lcd.print("*C");
lcd.setCursor(9,2);
lcd.print(-scale.get_units(), 0);
// lcd.setCursor(14,2);
lcd.print("g");
}
void oswietlenieLed() {
String dictonary[] = {"ON", "OFF"};
lcd.setCursor(0,1);
lcd.print(dictonary[tempVal]);
if (dictonary[tempVal] == "OFF"){
digitalWrite(5, HIGH);
}
else
{
digitalWrite(5, LOW);
}
}