Components and supplies
1
Arduino Nano R3
1
Temperature Sensor IC, Thermocouple
1
Nextion NX4024T032 - Generic 3.2" HMI TFT Intelligent LCD Touch Display Module
1
SG90 Micro-servo motor
Tools and machines
1
3D Printer (generic)
Apps and platforms
1
Nextion
1
Arduino IDE
Project description
Code
Wood Stove Controller
arduino
1 2 3 4#include <EEPROM.h> 5#include <Servo.h> 6#include <Wire.h> 7#include <SimpleTimer.h> 8#include <max6675.h> 9#include <Servo.h> 10#include <SoftwareSerial.h> 11#include <Average.h> 12 13#define rxPin 2 14#define txPin 3 15#define buzzerPin 7 16#define motor_pin 9 17#define ktcCLK 10 18#define ktcCS 11 19#define ktcSO 12 20 21SimpleTimer timer; 22int timerId1, timerId2, timerId3, timerId4, timerId5 ; 23// set up a new serial port 24SoftwareSerial Serial1 = SoftwareSerial(rxPin, txPin); 25Servo Motor_inTake; 26 27long lastTouchtDelta, lastTouch; 28int min_possition = 20; 29int max_possition = 104; 30int from2; 31 32byte dimSet = 100; 33MAX6675 ktc(ktcCLK, ktcCS, ktcSO); 34 35int Tmp; 36float tDelta, Delta1, taux; 37float derive = 0; 38byte HMIButton; 39int set_tmp ; // 'Celsius //////////// 40int spd ; //msec////////////////////////// 41int pos2go, posStep; 42int pos_old = 60; 43int bipN; 44int lastPosition; 45int tangential,tmp1,tmp2; 46//char msg[6]; 47 48int counter = 0; 49String msg; 50boolean moreWood = true; 51Average<float> tmpAve(10); 52 53/////////////////////////////////////////////////////// 54void setup() { 55 ///////////////////////////////////////////////////// 56 Serial.begin(9600); 57 Serial1.begin(9600); 58 pinMode(buzzerPin, OUTPUT); 59 bipN = 7; 60 61 digitalWrite(buzzerPin, 0); 62 EEPROM.get(0, min_possition); 63 if (min_possition < 1)min_possition = 10; 64 EEPROM.get(5, max_possition); 65 if (max_possition < 1)max_possition = 180; 66 EEPROM.get(10, set_tmp ); 67 if (set_tmp < 100)set_tmp = 250; 68 EEPROM.get(15, spd ); 69 if (spd < 10) { 70 spd = 100; 71 EEPROM.put(15, spd ); 72 } 73 spd = 100; 74 EEPROM.get(20, from2); 75 76 HMIPrint("page 0"); 77 HMIPrint("t0.txt=\\"FOYER MK3.0\\""); 78 79 timerId1 = timer.setInterval(5000, takeReading); 80 timerId2 = timer.setInterval(5000, printTemp); 81 timerId3 = timer.setTimer(500, bip, 6); 82 timerId4 = timer.setInterval(20000, TmpRoll); 83 timerId5 = timer.setInterval(500, takeTEMP); 84 HMIPrint("dims=80"); 85 HMIPrint("t0.txt=\\" \\""); 86 HMIPrint("page 1"); 87 HMIPrint("t0.txt=\\"Calibration\\""); 88 HMIPrint("t1.txt=\\"Min:" + String(min_possition) + "\\""); 89 HMIPrint("t2.txt=\\"Max:" + String(max_possition) + "\\""); 90 delayx(2000); 91 HMIPrint("page 0"); 92 HMIPrint("s0.pco2=25388"); 93 //motor(0); 94 //motor(255); 95 96} 97 98 99////////////////////////////////////////////////////////////////////// 100void loop() { // 101 ////////////////////////////////////////////////////////////////////// 102 timer.run(); 103 HMIButton = 0; 104 HMIButton = touchscreen(); 105 106 if (HMIButton == 0x01) { 107 calibration(); 108 } 109 if (HMIButton == 0x0A) { 110 // HMIPrint("dim=80"); 111 HMIPrint("page 0"); 112 } 113} 114 115 116 117/////////////////////////////////////////////////////////////////////////////////////////////////// 118/////////////////////////////////////////////////////////////////////////////////////////////////// 119void takeTEMP() { 120 tmpAve.push(Tmp = ktc.readCelsius()); 121} 122 123 124/////////////////////////////delay x/////////////////////////////////////////////////////////////// 125void delayx(int timer) { 126 unsigned long t3 = 0; 127 unsigned long t2 = millis(); 128 129 while (millis() - t2 < timer) { 130 t3++; 131 // timer.run(); 132 } 133} 134void TmpRoll() { 135 136 tmp2 = tmp1; 137 tmp1 = Tmp; 138} 139 140/////////////////////////////touchscreen/////////////////////////////////////////////////////////// 141int touchscreen() { 142 static const byte numBytes = 32; 143 static byte receivedBytes[numBytes]; 144 static byte numReceived = 0; 145 static boolean recvInProgress = false; 146 static byte ndx = 1; 147 byte startMarker = 101; 148 byte endMarker = 0xff; 149 150 static byte rb; 151 boolean newData; 152 receivedBytes[0] = 0; 153 HMIButton = 0; 154 155 while (Serial1.available() > 0 ) { //&& newData == false) { 156 rb = Serial1.read(); 157 if (rb != 0xff) { 158 Serial.print(rb, HEX); Serial.print(", "); 159 } 160 if (recvInProgress == true) { 161 if (rb != endMarker) { 162 receivedBytes[ndx] = rb; 163 //Serial.print(ndx); Serial.print("= "); Serial.print(receivedBytes[ndx]); Serial.println(""); 164 ndx++; 165 if (ndx >= numBytes) { 166 ndx = numBytes - 1; 167 } 168 } 169 else { 170 receivedBytes[ndx] = '\\0'; // terminate the string 171 recvInProgress = false; 172 numReceived = ndx; // save the number for use when printing 173 ndx = 1; 174 newData = true; 175 lastTouchtDelta = millis() - lastTouch; 176 } 177 } 178 if (rb == 0x65) { 179 receivedBytes[0] = rb; 180 recvInProgress = true; 181 } 182 } 183 delayx (100); 184 //if (rb!=255)Serial.print(rb); Serial.println("< "); 185 if (newData == true) { 186 if (receivedBytes[0] == 0x65) { //Puch Press Event 0x65 187 //bipN = 1; 188 189 Serial.println(); 190 Serial.print ("HMIButton= "); Serial.print (HMIButton); 191 /*Serial.print ("Byte 1="); Serial.print(receivedBytes[1], DEC); 192 Serial.print ("Byte 2="); Serial.print(receivedBytes[2], DEC); 193 Serial.print ("Byte 3="); Serial.print(receivedBytes[3], DEC); 194 Serial.print ("Byte 4="); Serial.print(receivedBytes[4], DEC); 195 //Serial.print ("slider ="); Serial.println(slider); 196 */ 197 } 198 199 if (receivedBytes[0] == 0x65) { //Puch Release Event 200 201 HMIButton = receivedBytes[3]; 202 Serial.print("box="); Serial.print(HMIButton); Serial.println(""); 203 lastTouch = millis(); 204 } 205 if (!dimSet) { 206 HMIPrint("dims=60"); 207 dimSet = true; 208 } 209 newData = false; 210 } 211 return (HMIButton); 212} 213 214///////////////////////////print temp////////////////////////////////////////////////////////////// 215void printTemp() { 216 //Serial.print ("temp:"); Serial.println (Tmp); 217 HMIPrint(""); 218 HMIPrint("t0.txt=\\"" + String(Tmp) + "\\""); 219 //HMIPrint("add 3,2," + String(map(set_tmp, 0, 100, 0, 40))); 220 221 HMIPrint("add 3,0," + String(map(Tmp, 0, 100, 0, 40))); 222 223} 224double scale(float input, float factor) 225{ 226 double value = pow(input, factor); 227 if (input < 0.0) value = -value; // only adjust calculated factor when input is negative. 228 return value; 229} 230/////////////////take reading and action/////////////////////////////////////////////////////////// 231 232void takeReading() { 233 int pourcent; 234 int proportionnel; 235 //Serial.println("Take Reading and Action"); 236 Tmp = tmpAve.mean(); 237 if ( Tmp > 500 || Tmp < 5) { // max/min temperature 238 pos2go = min_possition; 239 timerId3 = timer.setTimer(1000, bip, 6); 240 HMIPrint("t2.bco=YELLOW"); 241 HMIPrint("t2.pco=BLACK"); 242 HMIPrint("t2.txt=\\"Temperature ANORMAL\\""); 243 } 244 else if (Tmp < 25) {//COLD DOWN 245 HMIPrint("t2.txt= \\"Cold Down\\""); 246 pos2go = min_possition; 247 } 248 else if (Tmp > 75) { 249 HMIPrint("t2.bco=BLACK"); 250 HMIPrint("t2.pco=WHITE"); 251 tDelta = set_tmp - Tmp; 252 tDelta = constrain(tDelta, -30, 30); 253 proportionnel = map(tDelta, -30, 30, min_possition, max_possition); 254 tangential = scale(tDelta/7.00,2) -scale((tmp1 - tmp2),2) ; 255 Serial.print (", tang= "); Serial.println (tangential); 256 257 pos2go = proportionnel + tangential; 258 pos2go = constrain(pos2go, min_possition, max_possition); 259 260 Serial.print (", tmp= "); Serial.print (Tmp); 261 Serial.print (", tDelta= "); Serial.print (tDelta); 262 Serial.print (", tmp1/2= "); Serial.print (tmp1);Serial.print (" / ");Serial.print (tmp2); 263 264 Serial.print (", min/max_possition= "); Serial.print (min_possition); 265 Serial.print (" / "); Serial.print (max_possition); 266 Serial.print (", pos2go= "); Serial.print (pos2go); 267 Serial.println (", "); 268 269 if ( Tmp < 180 && moreWood == true) { 270 bipN = 1; 271 moreWood = false; 272 timerId3 = timer.setTimer(1000, bip, 2); 273 HMIPrint("t2.txt= \\"Add Wood\\""); 274 Serial.print("[108]"); 275 } 276 if ( Tmp > 190 && moreWood == false) { 277 moreWood = true; 278 } 279 } 280 281 282 pourcent = map (pos2go, min_possition, max_possition, 0, 200); 283 pourcent = constrain(pourcent, 0, 200); 284 HMIPrint("h0.val=" + String(pourcent)); 285 HMIPrint("add 3,1," + String(pourcent)); 286 motor (constrain(pos2go, min_possition, max_possition)) 287 ; 288 289} 290///////////////////////////////HMI Print//////////////////////////////////////////////////////////// 291 292void HMIPrint(String msg2) { 293 Serial1.print("dim=80"); 294 Serial1.print ("\\xFF\\xFF\\xFF"); 295 Serial1.print ("\\xFF\\xFF\\xFF"); 296 //delayx(50); 297 Serial1.print(msg2); 298 Serial1.print("\\xFF\\xFF\\xFF"); 299 //Serial.println(msg2); 300 delayx(50); 301} 302//////////////////////////////mOTOR///////////////////////////////////////////////////////////////// 303void motor ( int go2) { 304 constrain(go2, 0, 255); 305 //Serial.println("motor"); 306 if (from2 != go2) { 307 Motor_inTake.attach(motor_pin); 308 if (from2 < go2) { 309 HMIPrint("t2.txt=\\"" + String(from2) + " >> " + String(go2) + "\\""); 310 for ( posStep = from2; posStep <= go2; ++posStep) { 311 Motor_inTake.write(posStep); 312 delayx (spd);// speed 313 } 314 } 315 else { 316 HMIPrint("t2.txt=\\"" + String(go2) + " << " + String(from2) + "\\""); 317 //Serial.println("motor 3"); 318 //Serial.println(spd); 319 for ( posStep = from2; posStep >= go2; --posStep) { 320 Motor_inTake.write(posStep); 321 delayx (spd);//speed 322 //Serial.println("motor 1"); 323 } 324 } 325 Motor_inTake.detach(); 326 } 327 timerId4 = timer.setTimer(2000, clearHMIt2, 1); 328 //HMIPrint("t2.txt=\\" \\""); 329 //Serial.println("motor 2"); 330 from2 = go2; 331 EEPROM.put(20, from2); 332 333} 334///////////////////////////////////////////////////////////////// 335///////////////////////////////////////////////////////////////// 336void clearHMIt2() { 337 HMIPrint("t2.txt=\\" \\""); 338 //Serial.println("clr screen"); 339} 340 341///////////////////////////////calibration////////////////////////////////////////////////////////// 342void calibration() { 343 Serial.println("calibration"); 344 spd = 30; 345 HMIPrint("page 1"); 346 HMIPrint("t0.txt=\\"Set Temp.\\""); 347 HMIPrint("t1.txt=\\"Temp:" + String(set_tmp) + "\\""); 348 HMIPrint("t2.txt=\\" \\""); 349 HMIButton = 0; 350 351 352 while (HMIButton != 0x09 && HMIButton != 0x0A) { 353 HMIButton = touchscreen(); 354 if (HMIButton == 0x06) { 355 set_tmp += 5; 356 // // HMIPrint("dim=80"); 357 HMIPrint("t1.txt=\\"Temp:" + String(set_tmp) + "\\""); 358 } 359 if (HMIButton == 0x08) { 360 set_tmp -= 5; 361 // // HMIPrint("dim=80"); 362 HMIPrint("t1.txt=\\"Min:" + String(set_tmp) + "\\""); 363 } 364 } 365 if (HMIButton == 0x0A)return; 366 HMIPrint("t0.txt=\\"Set Close\\""); 367 HMIPrint("t1.txt=\\"Min:" + String(min_possition) + "\\""); 368 HMIButton = 0; 369 370 371 372 motor (min_possition); 373 while (HMIButton != 0x09 && HMIButton != 0x0A) { 374 HMIButton = touchscreen(); 375 if (HMIButton == 0x06) { 376 min_possition += 5; 377 HMIPrint("t1.txt=\\"Min:" + String(min_possition) + "\\""); 378 motor (min_possition); 379 } 380 if (HMIButton == 0x08) { 381 min_possition -= 5; 382 HMIPrint("t1.txt=\\"Min:" + String(min_possition) + "\\""); 383 motor (min_possition); 384 } 385 } 386 if (HMIButton == 0x0A)return; 387 388 389 EEPROM.get(5, max_possition); 390 HMIPrint("t0.txt=\\"Set Open\\""); 391 HMIPrint("t1.txt=\\"Max:" + String(max_possition) + "\\""); 392 motor (max_possition); 393 394 HMIButton = 0; 395 396 while (HMIButton != 0x09) { 397 HMIButton = touchscreen(); 398 if (HMIButton == 0x06) { 399 max_possition += 5; 400 HMIPrint("t1.txt=\\"Max:" + String(max_possition) + "\\""); 401 motor (max_possition); 402 } 403 if (HMIButton == 0x08) { 404 max_possition -= 5; 405 HMIPrint("t1.txt=\\"Max:" + String(max_possition) + "\\""); 406 motor (max_possition); 407 } 408 } 409 EEPROM.get(15, spd); 410 HMIPrint("t0.txt=\\"Set Speed\\""); 411 HMIPrint("t1.txt=\\"Speed:" + String(spd) + "\\""); 412 motor (max_possition); 413 414 HMIButton = 0; 415 416 while (HMIButton != 0x09) { 417 HMIButton = touchscreen(); 418 if (HMIButton == 0x06) { 419 spd += 5; 420 HMIPrint("t1.txt=\\"Speed:" + String(spd) + "\\""); 421 422 } 423 if (HMIButton == 0x08) { 424 spd -= 5; 425 HMIPrint("t1.txt=\\"Sped:" + String(spd) + "\\""); 426 motor (max_possition); 427 } 428 } 429 EEPROM.put(0, min_possition); 430 EEPROM.put(5, max_possition); 431 EEPROM.put(10, set_tmp ); 432 EEPROM.get(15, spd ); 433 HMIPrint("t0.txt=\\"Saved\\""); 434 HMIPrint("t1.txt=\\" \\""); 435 HMIPrint("t2.txt=\\" \\""); 436 HMIButton = 0; 437 while (HMIButton != 0x0A) { 438 HMIButton = touchscreen(); 439 } 440 HMIButton = 0; 441 HMIPrint("page 0"); 442 timer.restartTimer(timerId1); 443 timer.restartTimer(timerId2); 444 timer.restartTimer(timerId3); 445 timer.restartTimer(timerId4); 446 Serial.println("calibration completed"); 447} 448 449///////////////////////////////////////////////////////////////////////////////////////////////////// 450// BIP BIP 451///////////////////////////////////////////////////////////////////////////////////////////////////// 452 453void bip() { 454 digitalWrite (buzzerPin, !digitalRead(buzzerPin)); 455}
Downloadable files
Wood Stove Controller 3 d parts
Wood Stove Controller 3 d parts
Wood Stove Controller 3 d parts
Wood Stove Controller 3 d parts
Comments
Only logged in users can leave comments