Verwarmingsinstallatie 2020 Installation de chauffage
Logiciel chauffage au gaz commandé par arduino Software gaskachelsturing met arduino
#include
#include
#include
#define KEYA 2 // Led drive and keyboard scan output (from...)
#define KEYB 1 // analog keyboard input
#define ROOMTEMP 5 // analog room temp
#define KACHELTEMP 2 // analog kacheltemp
#define SERVOPORT 12 // driving servomotor
#define TBservoreq 3 // temp boost servoreq +=3
#define PMservoreq 10 // temp a servoreq
bool leds[8], mainleds[8];
byte sp[3], qp[3], ap[3]; // startvalue servo, eeprom, requested temp
byte countdownstart, qcount; // start value countdown (minutes)
byte countdown, counter = 60;
byte servopos = 10, servoreq; // huidige en gewenste servopositie
long lasttime = 0;
bool starting = true;
bool blinker = true;
bool fl = false; // display as floating
bool servomenu = false;
bool automode = true; // auto servomotor drive (false: manual servo drive)
byte askedtemp = 13;
bool servodrive = false;
Servo svm; // Servo motor drive
SoftwareSerial Ser (1, 10);
// ------- //
// STARTUP //
// ------- //
void setup() {
static byte i;
Ser.begin(9600);
delay(1000);
Ser.write(254); Ser.write(1); // clear display
for (i = 0; i < 8; i++)
pinMode(i + KEYA, OUTPUT);
for (i = 0; i < 3; i++) {
sp[i] = EEPROM.read(i); qp[i] = sp[i];
showval(214 + i * 4, sp[i]);
}
ap[0] = 19; ap[1] = 16; ap[2] = 12;
countdownstart = EEPROM.read(3) + 1;
qcount = countdownstart; countdown = countdownstart;
servoreq = sp[2] + PMservoreq;
clearleds(2); leds[3] = true;
}
void showleds(bool status) { // show leds or blank leds before scanning
static byte i;
for (i = 0; i < 8; i++) digitalWrite(i + KEYA, status && leds[i]);
}
void clearleds(byte j) { // clear led table
static byte i;
for (i = 0; i < 8; i++) leds[i] = false;
if (j < 8) leds[j] = true;
}
void restoreleds() { // switching to main menu
static byte i; // pidmenu = false;
servomenu = false;
for (i = 0; i < 8; i++) leds[i] = mainleds[i];
}
void saveleds() { // switching to pid or servo menu
static byte i;
for (i = 0; i < 8; i++) mainleds[i] = leds[i];
clearleds(8);
}
// ------------------------- //
// INCREMENT DECREMENT VALUE //
// ------------------------- //
void incrdecr(byte pos) {
static int stpx; // stepper + or -
static byte i;
showleds(false);
if (pos == 3) {
stpx = 1; digitalWrite(5, 1);
}
else {
stpx = -1; digitalWrite(6, 1);
}
while (analogRead(KEYB) > 150) {
for (i = 0; i < 3; i++) {
if (leds[i]) {
sp[i] += stpx; showval(214 + i * 4, sp[i]);
}
}
if (leds[5]) {
servoreq += stpx; showval(226, servoreq);
for (i = 0; i < 8; i++) mainleds[i] = false;
automode = false;
}
else if (leds[7]) {
countdownstart += stpx; showval(193, countdownstart);
}
delay(500);
}
}
void showval(byte pos, int valu) { // display integer aligned right
static byte ofs; // starting pos and going left
static int vals; // with decimal point if (fl)
if (!servodrive) { // forcelcd na servodrive
ofs = pos; vals = valu;
do {
setpos(ofs); Ser.print(vals % 10);
vals = vals / 10;
ofs--;
if (fl && ofs + 2 == pos) {
setpos(ofs); Ser.print(".");
ofs--;
}
} while (vals > 0);
Ser.write(254); Ser.write(ofs); Ser.print(" ");
}
}
bool setpos(byte pos) {
if (!servodrive) {
Ser.write(254); Ser.write(pos);
}
return !servodrive;
}
int showif(byte pos, int val, int oval) {
if (val != oval) {
showval(pos, val);
if (!servodrive)
return val;
else
return oval;
}
}
// ------- //
// M A I N //
// ------- //
void loop() {
static long mils, lasttime, sleeptime;
static int ii, oii, analogtemp, kacheltemp, jj, ojj, oanalogtemp;
static float kachelslow;
static byte i, j, oservoreq, oservopos, oaskedtemp;
static bool stijgend, dalend, servodetach = false;
static char kstat;
static byte keyb, keyc, oldkeyb; // gevalideerde toets (flank), ingedrukte toets, oude toets
static float tempi, tempc, tempfast, tempslow;
static byte mm, hh, ss;
static int ikachel, okachelt, okachels;
sleeptime = lasttime + 995 - millis();
if (sleeptime > 0) delay(sleeptime);
lasttime = millis();
// READING TEMPERATURE //
// ------------------- //
delay(100);
analogtemp = analogRead(ROOMTEMP);
if (analogtemp > oanalogtemp && tempfast < tempslow) tempslow = tempfast;
else if (analogtemp < oanalogtemp && tempfast > tempslow) tempslow = tempfast;
oanalogtemp = showif(131, analogtemp, oanalogtemp);
tempi = analogtemp * 0.031;
if (starting) {
tempc = tempi; tempfast = tempi; tempslow = tempi;
}
fl = true;
tempfast = tempfast * 0.9 + tempi * 0.1;
ii = tempfast * 100;
oii = showif(137, ii, oii);
tempslow = tempslow * 0.99 + tempi * 0.01; // + 0.005;
tempc = tempfast * 5 - tempslow * 4;
jj = tempc * 100;
ojj = showif(143, jj, ojj);
fl = false;
oaskedtemp = showif(231, askedtemp, oaskedtemp);
// READING KEYBOARD //
// ---------------- //
showleds(false);
keyc = 8;
for (i = 0; i < 8; i++) {
digitalWrite(i + KEYA, 1);
delay(1);
j = analogRead(KEYB);
digitalWrite(i + KEYA, 0);
if (j > 150) {
keyc = i;
break;
}
}
showleds(true);
if (keyc == oldkeyb) // toets blijf ingedrukt, geen flank
keyb = 8;
else
keyb = keyc;
if (!servomenu) { // MAIN MENU KEYBOARD PROCESSING
if (countdown < 5) leds[7] = false; // end tboost
if (keyb < 3) { // set temperature
askedtemp = ap[keyb];
servoreq = sp[keyb] + (askedtemp - tempslow) * 10.;
if (servoreq > 180) servoreq = 180;
if (servoreq < 0) servoreq = 0;
clearleds(keyb);
countdown = countdownstart;
automode = true;
if (setpos(148)) Ser.print("Auto ");
}
else if (keyb == 3 || keyb == 4) {
if (automode && leds[3] == false && leds[4] == false) {
leds[keyb] = true;
countdown += countdownstart / 2;
if (keyb == 3) {
askedtemp++; servoreq += PMservoreq;
}
else {
askedtemp--; servoreq -= PMservoreq;
}
}
}
else if (keyb == 6) { // servomenu
servomenu = true;
saveleds();
leds[6] = true;
if (setpos(148)) Ser.print("Servo");
}
else if (keyb == 7) { // tboost
servoreq += TBservoreq; // funktie kan meerdere keren aangeroepen worden
countdown += countdownstart / 4;
leds[7] = true;
}
}
else { // SERVO MENU KEYBOARD PROCESSING
if (keyb == 5) {
automode = false;
if (setpos(148)) Ser.print("Manua");
leds[5] = true;
}
if (keyb == 6) { // exit menu
for (i = 0; i < 3; i++) {
if (sp[i] != qp[i]) {
EEPROM.write(i, sp[i]); qp[i] = sp[i];
}
}
if (qcount != countdownstart) {
EEPROM.write(3, countdownstart); qcount = countdownstart;
}
restoreleds();
if(setpos(148)) Ser.print("Main ");
}
else if (keyb == 3 || keyb == 4) incrdecr(keyb);
else if (keyb != 8) clearleds(keyb);
}
// Keyboard processing done
oldkeyb = keyc;
// CALCULATE SERVO MOTOR POSITION //
// ------------------------------ //
kacheltemp = analogRead(KACHELTEMP);
if (starting) {
kachelslow = kacheltemp;
starting = false;
}
kachelslow = kacheltemp * 0.05 + kachelslow * 0.95;
ikachel = kachelslow;
stijgend = kacheltemp > kachelslow + 2;
dalend = kacheltemp < kachelslow - 2;
if (setpos(202)) {
if (stijgend) Ser.print("/");
if (dalend) Ser.write(92);
else Ser.print(" ");
okachelt = showif(201, kacheltemp, okachelt);
okachels = showif(206, ikachel, okachels);
}
if (!stijgend && !dalend) {
stijgend = kacheltemp > 320;
dalend = kacheltemp < 280;
if (setpos(202)) {
if (stijgend) Ser.print("+");
if (dalend) Ser.print("-");
}
}
if (automode && !servomenu) {
leds[5] = blinker && stijgend;
leds[6] = blinker && dalend;
counter--;
if (counter <= 0) {
counter = countdown * 3 + 5;
if (countdown > 0) {
countdown--;
showval(193, countdown);
}
if (askedtemp < tempc - 0.1 && servoreq > 0 && !dalend)
servoreq--;
else if (askedtemp > tempc + 0.1 && servoreq < 180 && !stijgend)
servoreq++;
}
showval(196, counter);
blinker = !blinker;
}
ss = (millis()/1000) % 60;
showval(167, ss);
if (ss == 0) {
mm++;
if (mm > 59) {
mm = 0;
hh++;
if (hh > 23) hh = 0;
showval(147, hh);
}
showval(211, mm);
}
oservoreq = showif(226, servoreq, oservoreq);
oservopos = showif(162, servopos, oservopos);
// MOVE SERVO MOTOR //
// ---------------- //
if (servodrive) {
delay(100); svm.attach(SERVOPORT);
delay(100); svm.write(180 - servopos);
delay(300); svm.detach();
servodrive = false;
}
else if (servoreq > servopos) {
servopos++; servodrive = true;
}
else if (servoreq < servopos) {
servopos--; servodrive = true;
}
}
|