-
Notifications
You must be signed in to change notification settings - Fork 0
/
AeroTec_Forno.ino
267 lines (203 loc) · 4.97 KB
/
AeroTec_Forno.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
#define T_SETPOINT 50
//#define DEBUG
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h> // Comes with Arduino IDE
#include <stdlib.h>
// set the LCD address to 0x27 for a 16 chars 2 line display
// A FEW use address 0x3F
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
// SAMPLING
#define SAMPLING_T 100 // in ms
unsigned long previousMillis;
#define N_SENSORS 3
#define N_AVG 5
// TEMPERATURE
float T[N_SENSORS][N_AVG];
int currentT = 0;
int TPin[N_SENSORS];
float TSetPoint;
#define TSP_PIN A3
float stdT;
// RELAY
#define RELAY_PIN 12
// STATE
int stateG;
int stateC;
#define OFF 0
#define ON 1
#define MALFUNCTIONING_ON 2
#define MALFUNCTIONING_OFF 3
//SWITCH
#define SWITCH_PIN 2
int stateS;
unsigned long OnSince;
#define SWITCH_TIME 2000
// TERESHOLDS
#define STD_THRESHOLD 8
#define T_THRESHOLD 0.2
#include <Servo.h>
Servo ESC;
#define PIN_MOTOR 9
#define THROTTLE 1175
void setup() {
Serial.begin(9600);
pinMode(RELAY_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT_PULLUP);
digitalWrite(RELAY_PIN, LOW);
previousMillis = 0;
currentT = 0;
stateG = OFF;
stateC = OFF;
TPin[1] = A0;
TPin[2] = A1;
TPin[3] = A2;
Serial.println("SETUP");
//TSetPoint = getTSetPoint();
TSetPoint = T_SETPOINT;
Serial.println(TSetPoint);
lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight(); // finish with backlight on
ESC.attach(PIN_MOTOR);
ESC.writeMicroseconds(1000);
}
void loop() {
unsigned long currentMillis = millis();
float Tc;
if (currentMillis - previousMillis > SAMPLING_T){
Tc = getTemperature();
if(stateG == ON){
control(Tc);
//Serial.println(Tc);
previousMillis = currentMillis;
}else{
digitalWrite(RELAY_PIN, LOW);
stateC = OFF;
}
}
/*if(stateG == OFF){
TSetPoint = getTSetPoint();
}*/
if(stateG == ON){
ESC.writeMicroseconds(THROTTLE);
}else{
ESC.writeMicroseconds(1000);
}
checkState();
printScreen(Tc);
delay(100);
}
// TEMPERATURE
float getTemperature() {
T[0][currentT] = analogRead(A0);
//T[0][currentT] = (T[0][currentT]*(5000)/(1023.0)-750)/10.0+25;
T[0][currentT] = (T[0][currentT]*(5000)/(1023.0)-1000)/10.0+50;
delay(10);
T[1][currentT] = analogRead(A1);
//T[1][currentT] = (T[1][currentT]*(5000)/(1023.0)-750)/10.0+25;
T[1][currentT] = (T[1][currentT]*(5000)/(1023.0)-1000)/10.0+50;
delay(10);
T[2][currentT] = analogRead(A2);
//T[2][currentT] = (T[2][currentT]*(5000)/(1023.0)-750)/10.0+25;
T[2][currentT] = (T[2][currentT]*(5000)/(1023.0)-1000)/10.0+50;
delay(10);
#ifdef DEBUG
for (int i = 0; i < N_SENSORS; i++) {
Serial.print(T[i][currentT]);
Serial.print(' ');
}
Serial.println();
#endif
currentT++;
if (currentT >= N_AVG){
currentT = 0;
}
float Tc = 0.0;
for(int i = 0; i<N_SENSORS; i++){
for(int j = 0; j<N_AVG; j++){
Tc = Tc + T[i][j];
}
}
Tc = Tc/(N_AVG*N_SENSORS);
stdT = 0;
for(int i = 0; i<N_SENSORS; i++){
for(int j = 0; j<N_AVG; j++){
stdT += pow((T[i][j]-Tc),2);
}
}
stdT = stdT/(N_AVG*N_SENSORS);
stdT = sqrt(stdT);
if(stateG == ON && stdT > STD_THRESHOLD){
stateG = MALFUNCTIONING_ON;
}else if(stateG == OFF && stdT > STD_THRESHOLD){
stateG = MALFUNCTIONING_OFF;
}else if(stdT <= STD_THRESHOLD){
if(stateG == MALFUNCTIONING_OFF){
stateG = OFF;
}else if(stateG == MALFUNCTIONING_ON){
stateG = ON;
}
}
//Serial.println(std);
return Tc;
}
void control(float Tc){
//Serial.println(stateG);
if(stateG != ON)
return;
if(stateC == ON && Tc > TSetPoint + T_THRESHOLD){
digitalWrite(RELAY_PIN, LOW);
stateC = OFF;
}else if(stateC == OFF && Tc < TSetPoint - T_THRESHOLD){
digitalWrite(RELAY_PIN, HIGH);
stateC = ON;
}
}
float getTSetPoint(){
int reading = analogRead(TSP_PIN);
delay(5);
return 0.1*round(10*map(reading, 0, 1024, 45, 90));
}
void checkState(){
int reading = digitalRead(2);
if(reading == LOW){
if(stateS == ON && millis()-OnSince> SWITCH_TIME){
if(stateG < MALFUNCTIONING_ON){
stateG = -stateG + 1;
stateS = OFF;
}
}else if(stateS == OFF){
stateS = ON;
OnSince = millis();
}
}else{
stateS = OFF;
}
}
void printScreen(float Tc){
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("T:"); lcd.print(Tc); lcd.print(" SD:"); lcd.print(stdT);
lcd.setCursor(0,1);
lcd.print("TSP:"); lcd.print(TSetPoint);
switch(stateG){
case ON:{
lcd.print(" ON");
break;
}
case OFF:{
lcd.print(" OFF");
break;
}
case MALFUNCTIONING_ON:{
lcd.print("ERROR ON");
break;
}
case MALFUNCTIONING_OFF:{
lcd.print("ERROR OFF");
break;
}
}
}