code cleanup, better regulation

This commit is contained in:
Michael Wesemann 2019-02-21 10:34:25 +01:00
parent 54f10c9cca
commit 8137ef5e47

154
stir.ino
View File

@ -1,4 +1,4 @@
/////////////////////////////////////////////////////////////////////////////// Stir Control (mwx'2019, v1.2.4)
/////////////////////////////////////////////////////////////////////////////// Stir Control (mwx'2019, v1.2.5)
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
@ -9,10 +9,7 @@
int SPEEDINC = 50; // speed increment
int FANMIN = 200; // fan minimum speed
int FANMAX = 1000; // fan maximum speed
int RINTERVAL = 3000; // regulation interval
int RDELAY = 2000; // regulation delay on changes
int FANMAX = 1600; // fan maximum speed
int CATCHSTOP = 20000; // catch stop period
@ -26,6 +23,10 @@ int CLK = 5;
int DT = 6; // dt on KY-040 encoder
int SW = 4; // sw on KY-040 encoder
int RINTERVAL = 5000; // regulation interval
int RDELAY = 2000; // regulation delay on changes
int RTOL = 8; // regulation tolerance
int SAVETAG = 1004; // save tag
LiquidCrystal_I2C lcd(0x27,16,2); // LCD display (connect to SDA/SCL)
@ -34,14 +35,14 @@ int v0,b0,r0,v1,b1,r1,r,fanstate0,fanstate1;
long rpmcount0,rpmcount1; // rpm counter
double rpm0,rpm1; // rpm
long speedts,dt,rts,swts,bts,ox,savets,catchts0,catchts1,stop0,stop1,b0ts,b1ts; // timing
int bdelay,bprocess,enclast,encval,swmode; // button processing
char form[8],out[32],M[8];String str; // string buffer
int OK,SAVE,LOCK,bstate0,bstate1,btime0,btime1,catch0,catch1,ctime0,ctime1,mode;
long sts,rts,swts,bts,ox,savets,catchts0,catchts1,stop0,stop1,b0ts,b1ts; // timing
int bdelay,bprocess,enclast,encval,swmode,mode; // button/encoder processing
int OK,SAVE,LOCK,bstate0,bstate1,btime0,btime1,catch0,catch1,ctime0,ctime1; // operation state
char form[8],out[64];String str; // string buffer
void setup() { ////////////////////////////////////////////////////////////////////////////////////////// SETUP
rpmcount0=0;rpmcount1=0;rpm0=0;rpm1=0;bprocess=0;speedts=0;r0=0;r1=0;swmode=2;
rpmcount0=0;rpmcount1=0;rpm0=0;rpm1=0;bprocess=0;r0=0;r1=0;swmode=2;
Serial.begin(9600); // start serial
@ -61,22 +62,18 @@ void setup() { /////////////////////////////////////////////////////////////////
SAVE=0; // load/initialize settings
if (eer(0)!=SAVETAG) {
v0=300;v1=300;b0=700;b1=700;btime0=30;btime1=30;catch0=0;catch1=0;ctime0=120;ctime1=120;
eew(0,SAVETAG);
save();
eew(0,SAVETAG);save();
} else {
v0=eer(1);v1=eer(2);b0=eer(3);b1=eer(4);btime0=eer(5);btime1=eer(6);
catch0=eer(7);catch1=eer(8);ctime0=eer(9);ctime1=eer(10);
}
pinMode(PWM0,OUTPUT);pinMode(PWM1,OUTPUT); // set pin modes
pinMode(CLK,INPUT);pinMode(DT,INPUT);pinMode(SW,INPUT);
pinMode(PWM0,OUTPUT);pinMode(PWM1,OUTPUT); // set PWM pins
pinMode(CLK,INPUT);pinMode(DT,INPUT);pinMode(SW,INPUT); // set KY-040 pins
enclast=digitalRead(CLK); // get encoder state
fanstate0=0;fanstate1=0;OCR1A=0;OCR1B=0; // turn fans off
rts=MS;swts=MS;bts=MS;savets=MS;catchts0=MS;catchts1=MS;stop0=MS;stop1=MS; // set timer
sts=MS;rts=MS;swts=MS;bts=MS;savets=MS;catchts0=MS;catchts1=MS;stop0=MS;stop1=MS; // set timer
bstate0=0;bstate1=0;mode=0;updatelcd();updatespeed();updatemarker();LOCK=0; // set initial states
}
@ -136,14 +133,14 @@ void loop() { //////////////////////////////////////////////////////////////////
}
if (str.substring(0,5)=="btime") { // btime <fan0> <fan1> (value: 0-60)
btime0=sstr(str,':',1).toInt();if (btime0<0) btime0=0;if (btime0>60) btime0=60;
btime1=sstr(str,':',2).toInt();if (btime1<0) btime1=0;if (btime1>60) btime1=60;
btime0=sstr(str,':',1).toInt();btime0=cut(btime0,0,60);
btime1=sstr(str,':',2).toInt();btime1=cut(btime1,0,60);
SAVE++;OK=1;updatelcd();updatespeed();serinfo();
}
if (str.substring(0,5)=="ctime") { // ctime <fan0> <fan1> (value: 60-240)
ctime0=sstr(str,':',1).toInt();if (ctime0<60) ctime0=60;if (ctime0>240) ctime0=240;
ctime1=sstr(str,':',2).toInt();if (ctime1<60) ctime1=60;if (ctime1>240) ctime1=240;
ctime0=sstr(str,':',1).toInt();ctime0=cut(ctime0,60,240);
ctime1=sstr(str,':',2).toInt();ctime1=cut(ctime1,60,240);;
SAVE++;OK=1;updatelcd();updatespeed();serinfo();
}
@ -152,27 +149,22 @@ void loop() { //////////////////////////////////////////////////////////////////
if (MS-rts>RINTERVAL) { ////////////////////////////////////////////////////////////////////////// regulation
rpm0=rpmcount0/2/((MS-speedts)/1000.0)*60.0;
rpm1=rpmcount1/2/((MS-speedts)/1000.0)*60.0;
rpm0=rpmcount0/((MS-sts)/1000.0)*30.0;
rpm1=rpmcount1/((MS-sts)/1000.0)*30.0;
speedts=MS;rpmcount0=0;rpmcount1=0;
sts=MS;rpmcount0=0;rpmcount1=0;
if (abs(v0-rpm0)>8) {
ox=1;if (abs(v0-rpm0)>20) ox=3;if (abs(v0-rpm0)>50) ox=5;
if (!fanstate0) r0=0;
else {
if (bstate0) r=b0; else r=v0;
if (r>rpm0) {;r0+=ox;if (r0>40) r0=40;}
if (r<rpm0) {;r0-=ox;if (r0<-40) r0=-40;}
}
if (abs(v1-rpm1)>8) {
ox=1;if (abs(v1-rpm1)>20) ox=3;if (abs(v1-rpm1)>50) ox=5;
if (bstate1) r=b1; else r=v1;
if (r>rpm1) {;r1+=ox;if (r1>40) r1=40;}
if (r<rpm1) {;r1-=ox;if (r1<-40) r1=-40;}
if (abs(r-rpm0)>RTOL) r0+=(r-rpm0)/10.0+1.0;
}
if (!fanstate0) r0=0;
if (!fanstate1) r1=0;
else {
if (bstate1) r=b1; else r=v1;
if (abs(r-rpm1)>RTOL) r1+=(r-rpm1)/10.0+1.0;
}
updatePWM();updatespeed();rts=MS;
}
@ -267,14 +259,14 @@ void loop() { //////////////////////////////////////////////////////////////////
if (swmode==0 && mode==1) {;updatePWM();r0=0;rts=MS+RDELAY;} // apply boost speed change 0
if (swmode==1 && mode==1) {;updatePWM();r1=0;rts=MS+RDELAY;} // apply boost speed change 1
if (swmode==0 && mode==2) {;if (btime0<0) btime0=0;if (btime0>60) btime0=60;} // check boost time 0
if (swmode==1 && mode==2) {;if (btime1<0) btime1=0;if (btime1>60) btime1=60;} // check boost time 1
if (swmode==0 && mode==2) btime0=cut(btime0,0,60); // check boost time 0
if (swmode==1 && mode==2) btime1=cut(btime1,0,60); // check boost time 1
if (swmode==0 && mode==3) {;if (catch0<0) catch0=1;if (catch0>1) catch0=0;catchts0=MS;} // check catch 0
if (swmode==1 && mode==3) {;if (catch1<0) catch1=0;if (catch1>1) catch1=0;catchts1=MS;} // check catch 1
if (swmode==0 && mode==3) {;catch0=cut(catch0,0,1);catchts0=MS;} // check catch 0
if (swmode==1 && mode==3) {;catch1=cut(catch1,0,1);catchts1=MS;} // check catch 1
if (swmode==0 && mode==4) {;if (ctime0<60) ctime0=60;if (ctime0>240) ctime0=240;} // check boost time 0
if (swmode==1 && mode==4) {;if (ctime1<60) ctime1=60;if (ctime1>240) ctime1=240;} // check boost time 1
if (swmode==0 && mode==4) ctime0=cut(ctime0,60,240); // check catch time 0
if (swmode==1 && mode==4) ctime1=cut(ctime1,60,240); // check catch time 1
if (swmode==2) {;if (mode<0) mode=4;if (mode>4) mode=0;} // check menu mode
@ -290,36 +282,26 @@ void loop() { //////////////////////////////////////////////////////////////////
void updatelcd() { ///////////////////////////////////////////////////////////////////////////////// update LCD
if (mode==0) {
slcd(1,1,5,"SPEED");
ilcd(7,1,-4,int(v0));
ilcd(12,1,-4,int(v1));
slcd(1,1,5,"SPEED");ilcd(7,1,-4,int(v0));ilcd(12,1,-4,int(v1));
}
if (mode==1) {
slcd(1,1,5,"BOOST");
ilcd(7,1,-4,int(b0));
ilcd(12,1,-4,int(b1));
slcd(1,1,5,"BOOST");ilcd(7,1,-4,int(b0));ilcd(12,1,-4,int(b1));
}
if (mode==2) {
slcd(1,1,5,"BTIME");
ilcd(7,1,-4,int(btime0));ilcd(12,1,-4,int(btime1));
slcd(1,1,5,"BTIME");ilcd(7,1,-4,int(btime0));ilcd(12,1,-4,int(btime1));
}
if (mode==3) {
slcd(1,1,5,"CATCH");
if (catch0==0) slcd(7,1,-4,"OFF");
else slcd(7,1,-3,"ON");
if (catch1==0) slcd(12,1,-4,"OFF");
else slcd(12,1,-3,"ON");
if (catch0==0) slcd(7,1,-4,"OFF"); else slcd(7,1,-3,"ON");
if (catch1==0) slcd(12,1,-4,"OFF"); else slcd(12,1,-3,"ON");
}
if (mode==4) {
slcd(1,1,5,"CTIME");
ilcd(7,1,-4,int(ctime0));ilcd(12,1,-4,int(ctime1));
slcd(1,1,5,"CTIME");ilcd(7,1,-4,int(ctime0));ilcd(12,1,-4,int(ctime1));
}
}
void updatemarker() { /////////////////////////////////////////////////////////////////// update current marker
slcd(0,1,1," ");
slcd(6,1,1," ");
slcd(11,1,1," ");
slcd(0,1,1," ");slcd(6,1,1," ");slcd(11,1,1," ");
if (!LOCK) {
if (swmode==0) slcd(6,1,1,">");
if (swmode==1) slcd(11,1,1,">");
@ -337,7 +319,7 @@ void updatespeed() { ///////////////////////////////////////////////////////////
if (bstate0) {
ilcd(1,0,2, (((long)btime0*60000)-(MS-(long)b0ts))/1000/60+1);
} else slcd(7,0,1," ");
ilcd(7,0,-4,round(rpm0/5.0)*5);
ilcd(7,0,-4,round(rpm0));
}
}
else slcd(7,0,-4,"OFF");
@ -349,7 +331,7 @@ void updatespeed() { ///////////////////////////////////////////////////////////
if (bstate1) {
ilcd(4,0,2, (((long)btime1*60000)-(MS-(long)b1ts))/1000/60+1);
} else slcd(12,0,1," ");
ilcd(12,0,-4,round(rpm1/5.0)*5);
ilcd(12,0,-4,round(rpm1));
}
}
else slcd(12,0,-4,"OFF");
@ -376,47 +358,27 @@ void slcd(int x,int y, int l,String s) { ///////////////////////////////////////
}
void updatePWM() { ///////////////////////////////////////////////////////////////////// update PWM (fan speed)
if (v0<FANMIN) v0=FANMIN;if (v0>FANMAX) v0=FANMAX;
if (v1<FANMIN) v1=FANMIN;if (v1>FANMAX) v1=FANMAX;
if (b0<FANMIN) b0=FANMIN;if (b0>FANMAX) b0=FANMAX;
if (b1<FANMIN) b1=FANMIN;if (b1>FANMAX) b1=FANMAX;
v0=cut(v0,FANMIN,FANMAX);v1=cut(v1,FANMIN,FANMAX);
b0=cut(b0,FANMIN,FANMAX);b1=cut(b1,FANMIN,FANMAX);
double v;
if (fanstate0) {
if (MS<stop0) {
OCR1A=0;
} else {
if (bstate0) {
v=b0/(FANMAX/320.0)+r0;if (v<0) v=0;if (v>320) v=320;
OCR1A=v;
} else {
v=v0/(FANMAX/320.0)+r0;if (v<0) v=0;if (v>320) v=320;
OCR1A=v;
}
}
} else OCR1A=0;
if (fanstate1) {
if (MS<stop1) {
OCR1B=0;
} else {
if (bstate1) {
v=b1/(FANMAX/320.0)+r1;if (v<0) v=0;if (v>320) v=320;
OCR1B=v;
} else {
v=v1/(FANMAX/320.0)+r1;if (v<0) v=0;if (v>320) v=320;
OCR1B=v;
}
}
} else OCR1B=0;
if (!fanstate0 || MS<stop0) OCR1A=0;
else {
if (bstate0) r=b0; else r=v0;
OCR1A=cut(r/(FANMAX/320.0)+r0,0,320);
}
if (!fanstate1 || MS<stop1) OCR1B=0;
else {
if (bstate1) r=b1; else r=v1;
OCR1B=cut(r/(FANMAX/320.0)+r1,0,320);
}
}
void rpmint0() {;rpmcount0++;} ///////////////////////////////////////////////////////////////// rpm interrupts
void rpmint1() {;rpmcount1++;}
double cut(double v,double min,double max) {;return v>max?max:v<min?min:v>max?max:v;} ///// cut values to limit
int eer(int adr) {;return EEPROM.read(adr*2)+EEPROM.read(adr*2+1)*256;} /////////////////////////// read EEPROM
void eew(int adr, int val) {;EEPROM.write(adr*2,val%256);EEPROM.write(adr*2+1,val/256);} /////// save to EEPROM
void save() { /////////////////////////////////////////////////////////////////////////////////// save settings