From 7a265978f1fa1586731f3aad8dcd67cd1c28c6e2 Mon Sep 17 00:00:00 2001 From: Michael Wesemann Date: Tue, 8 Oct 2019 14:17:10 +0200 Subject: [PATCH] - --- README.md | 56 +++++++++++++-------------- stir.ino | 111 +++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 104 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index da7c3ed..1ca0881 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ All functions are controlled by the KY-040 encoder: * **OTIME**: turn changes switch off time in hours (the timer activates immediately, 0 = switch off disabled) * **RNVAL**: random value range (a random values in this range will be added to speed) * **Other menu options**: - * **BRGHT**: LCD brightness + * **CFG**: Configuration menu ## Display Indicators: @@ -81,33 +81,33 @@ All functions are controlled by the KY-040 encoder: ## Settings (in stir.ino) -|Value|Description| +|Value|Config Menu Name|Description| |:----|:----------| -|`SPEEDINC1`, `SPEEDINC2`, `SPEEDINC3`|increment of speed values when turning the encoder| +|`SPEEDINC1`, `SPEEDINC2`, `SPEEDINC3`|`SPI1`, `SPI2`, `SPI3`|increment of speed values when turning the encoder| |`SPEEDSTEP1`, `SPEEDSTEP2`|speed increment steps (increment changes at these values)| -|`FANMIN`|fan minimum speed (should be a value at which the fan runs safely) (rpm)| -|`FANMAX`|maximum speed of the fan (it is important that this is the real maximum value of the fan) (rpm)| -|`BOFF`|if set boost off will also turn the normal mode off| -|`BINC`|boost time increment (min)| -|`CINC`|catch time increment (min)| -|`RINC`|rise time increment (sec)| -|`OINC`|off timer increment (hour)| -|`RNDINC`|randon value increment (rpm)| -|`CATCHSTOP`|stop interval for stir fish catch function (ms)| -|`PWM0`, `PWM1`|PWM output pins for fan speed| -|`PWM2`|PWM output pin for LCD brightness control| -|`I0`, `I1`|interrupts for rpm measurement (2 and 3 for Leonardo and ProMicro / 0 and 1 for Uno)| -|`SWAPENC`|switch KY-040 encoder direxction (0/1)| -|`CLK`, `DT`, `SW`|pins for KY-040 encoder| -|`R0`, `R0`|voltage select relais pin| -|`RTHRES`|voltage switch threshold (rpm)| -|`RINTERVAL`|regulation interval (ms)| -|`RDELAY`|extra regulation delay when value changes (ms)| -|`RTOL`|regulation tolerance (rpm)| -|`RNDINTERVL`|randon value range change interval (ms)| -|`SINTERVAL`|speed measurement interval (ms)| -|`SAVERAGE`|speed measurement average| -|`SAVETAG`|parameter save identifier| -|`SAVEDELAY`|delay in seconds before parameter will be saved to EEPROM| -|`LCDB`|LCD brightness steps (10 values, 0=off, 255=max)| +|`FANMIN`||fan minimum speed (should be a value at which the fan runs safely) (rpm)| +|`FANMAX`||maximum speed of the fan (it is important that this is the real maximum value of the fan) (rpm)| +|`BOFF`||if set boost off will also turn the normal mode off| +|`BINC`||boost time increment (min)| +|`CINC`||catch time increment (min)| +|`RINC`||rise time increment (sec)| +|`OINC`||off timer increment (hour)| +|`RNDINC`||randon value increment (rpm)| +|`CATCHSTOP`||stop interval for stir fish catch function (ms)| +|`PWM0`, `PWM1`||PWM output pins for fan speed| +|`PWM2`||PWM output pin for LCD brightness control| +|`I0`, `I1`||interrupts for rpm measurement (2 and 3 for Leonardo and ProMicro / 0 and 1 for Uno)| +|`SWAPENC`||switch KY-040 encoder direxction (0/1)| +|`CLK`, `DT`, `SW`||pins for KY-040 encoder| +|`R0`, `R0`||voltage select relais pin| +|`RTHRES`||voltage switch threshold (rpm)| +|`RINTERVAL`||regulation interval (ms)| +|`RDELAY`||extra regulation delay when value changes (ms)| +|`RTOL`||regulation tolerance (rpm)| +|`RNDINTERVL`||randon value range change interval (ms)| +|`SINTERVAL`||speed measurement interval (ms)| +|`SAVERAGE`||speed measurement average| +|`SAVETAG`||parameter save identifier| +|`SAVEDELAY`||delay in seconds before parameter will be saved to EEPROM| +|`LCDB`||LCD brightness steps (10 values, 0=off, 255=max)| diff --git a/stir.ino b/stir.ino index 8ea9b53..b71024b 100644 --- a/stir.ino +++ b/stir.ino @@ -3,12 +3,11 @@ #include #define MS (long)millis() +#define NP 19 -#define SX Serial.print -#define SXN Serial.println - -String VERSION = "2.0.2"; +String VERSION = "2.1.0"; +int FANINIT = 0; // initialize fan with high voltage (0/1) int SPEEDINC1 = 20; // speed increment values int SPEEDINC2 = 50; @@ -22,14 +21,14 @@ int RNDINC = 50; int FANMIN = 200; // fan minimum speed (should be a value at which the fan runs safely) (rpm) int FANMAX = 2500; // fan maximum speed (should be the real maximum value of the fan) (rpm) -int BOFF = 0; // if set boost off will also turn the normal mode off +int BOFF = 0; // if set boost off will also turn the normal mode off (0/1) int BINC = 1; // BTIME increment (min) int CINC = 10; // CTIME increment (min) int RINC = 10; // RTIME increment (sec) int OINC = 3; // OTIME increment (hour) -long CATCHSTOP = 20000; // fish catch stop period (ms) +int CATCHSTOP = 20; // fish catch stop period (sec) int PWM0 = 9; // PWM pin for 1. fan int PWM1 = 10; // PWM pin for 2. fan @@ -38,7 +37,7 @@ int PWM2 = 11; int I0 = 0; // interrupt for fan 0 rpm signal (use 2 for Leonardo/ProMicro and 0 for Uno) int I1 = 1; // interrupt for fan 1 rpm signal (use 3 for Leonardo/ProMicro and 1 for Uno) -int SWAPENC = 1; // switch KY-040 encoder direxction (0/1) +int SWAPENC = 1; int CLK = 5; // clk on KY-040 encoder (swap clk and dt to invert direction) int DT = 6; // dt on KY-040 encoder @@ -52,14 +51,16 @@ long RINTERVAL = 5000; long RDELAY = 3000; // regulation delay on changes (ms) int RTOL = 8; // regulation tolerance (rpm) -long RNDINTERVAL = 300000; // randon value range change interval (ms) +int RNDINTERVAL = 300; // randon value range change interval (sec) long SINTERVAL = 2000; // speed measurement internval (ms) int SAVERAGE = 4; // speed measurement average -int SAVETAG = 2001; // save tag +int SAVETAG = 2015; // save tag long SAVEDELAY = 10000; // EEPROM save delay (ms) +int LGHT = 5; // LCD brightness + byte LCDB[] = {4,8,16,24,32,64,96,128,192,255}; // LCD brightness steps (10 values, 0=off, 255=max) byte aright[] = {0x00,0x08,0x0C,0x0E,0x0C,0x08,0x00,0x00}; // LCD character @@ -71,19 +72,36 @@ LiquidCrystal_I2C lcd(0x27,16,2); // L int v[2],b[2],r[2]={0};double q,rpm[2]={0},xpm[2]={0},xb[2]={0},xv[2]={0},rtime[2]; // speed and regulation long ac[2]={0},bc[2]={0}; // interrupt rpm counter long xts,sts,rts,swts,buts,savets,catts[2],stop[2],bts[2],vts[2],ots[2],rndts[2]; // timing -int bdelay,bprocess=0,enclast,encval,M=2,S=0; // button/encoder processing +int bdelay,bprocess=0,enclast,encval,M=2,S=0,C=0; // button/encoder processing int F[2],bstate[2]={0},btime[2],cat[2],ctime[2],cstate[2]={0},bclr=0,SAVE=0,LOCK=0; // operating states int ostate[2]={0},otime[2]={0}; // off timer -int brght; // LCD brightness int rnd[2]={0},rnval[2]={0};long seed; // random speed char form[8],out[20]; // string buffer - +String CF[NP];int *P[NP],Cinc[NP],Cmin[NP],Cmax[NP]; // configuration menu +int clkorg,dtorg; void setup() { ////////////////////////////////////////////////////////////////////////////////////////// SETUP - Serial.begin(9600); // start serial - - - if (SWAPENC) {;int tmp=DT;DT=CLK;CLK=tmp;} + int i; + + i=0; P[i]=&LGHT; CF[i]="LGHT"; defcon(i, 1, 0, 9 ); // configuration menu setup + i=1; P[i]=&SPEEDINC1; CF[i]="SPI1"; defcon(i, 10, 10, 500 ); + i=2; P[i]=&SPEEDINC2; CF[i]="SPI2"; defcon(i, 10, 10, 500 ); + i=3; P[i]=&SPEEDINC3; CF[i]="SPI3"; defcon(i, 10, 10, 500 ); + i=4; P[i]=&SPEEDSTEP1; CF[i]="SPS1"; defcon(i, 50, 100, 2000 ); + i=5; P[i]=&SPEEDSTEP2; CF[i]="SPS2"; defcon(i, 50, 100, 2000 ); + i=6; P[i]=&SWAPENC; CF[i]="SWEN"; defcon(i, 1, 0, 1 ); + i=7; P[i]=&RNDINC; CF[i]="RINC"; defcon(i, 1, 0, 9 ); + i=8; P[i]=&CATCHSTOP; CF[i]="CSTP"; defcon(i, 10, 0, 1000 ); + i=9; P[i]=&FANMIN; CF[i]="FMIN"; defcon(i, 50, 100, 1000 ); + i=10; P[i]=&FANMAX; CF[i]="FMAX"; defcon(i, 50, 500, 4000 ); + i=11; P[i]=&FANINIT; CF[i]="FINI"; defcon(i, 1, 0, 1 ); + i=12; P[i]=&RTHRES; CF[i]="RTHR"; defcon(i, 50, 0, 1500 ); + i=13; P[i]=&RNDINTERVAL; CF[i]="RINT"; defcon(i, 1, 0, 60 ); + i=14; P[i]=&BOFF; CF[i]="BOFF"; defcon(i, 1, 0, 1 ); + i=15; P[i]=&BINC; CF[i]="BINC"; defcon(i, 1, 1, 60 ); + i=16; P[i]=&CINC; CF[i]="CINC"; defcon(i, 1, 1, 60 ); + i=17; P[i]=&RINC; CF[i]="RINC"; defcon(i, 1, 1, 60 ); + i=18; P[i]=&OINC; CF[i]="OINC"; defcon(i, 1, 1, 60 ); lcd.init();lcd.clear();lcd.backlight(); // initialize lcd lcd.createChar(0,aright);lcd.createChar(1,aup);lcd.createChar(2,arnd); // load lcd characters @@ -100,29 +118,36 @@ void setup() { ///////////////////////////////////////////////////////////////// SAVE=0; // load/initialize settings if (eer(0)!=SAVETAG) { - for (int i=0;i<2;i++) {;v[i]=300;b[i]=700;btime[i]=30;cat[i]=0;ctime[i]=120;rtime[i]=60;F[i]=0;rnval[i]=0;} - brght=5;seed=0; + for (i=0;i<2;i++) {;v[i]=300;b[i]=700;btime[i]=30;cat[i]=0;ctime[i]=120;rtime[i]=60;F[i]=0;rnval[i]=0;} + seed=0; eew(0,SAVETAG);save(); } else { - for (int i=0;i<2;i++) { + for (i=0;i<2;i++) { v[i]=eer(1+i);b[i]=eer(3+i);btime[i]=eer(5+i);cat[i]=eer(7+i); ctime[i]=eer(9+i);F[i]=eer(11+i);rtime[i]=eer(13+i);rnval[i]=eer(15+i); } seed=eer(100)+1;eew(100,seed);randomSeed(seed); - brght=eer(101); + for (i=0;i0 && MS-savets>SAVEDELAY) {;save();SAVE=0;savets=MS;} ////////////////////// save settings if needed @@ -130,7 +155,7 @@ void loop() { ////////////////////////////////////////////////////////////////// for (int i=0;i<2;i++) { - if (MS-rndts[i]>RNDINTERVAL) { /////////////////////////////////////////////////////////////// random timer + if (MS-rndts[i]>((long)RNDINTERVAL*1000)) { ////////////////////////////////////////////////// random timer rnd[i]=int(random(0,rnval[i]+1)/10)*10;rndts[i]=MS;updatelcd(); } @@ -142,7 +167,7 @@ void loop() { ////////////////////////////////////////////////////////////////// catts[i]=MS;cstate[i]=1;F[i]=0;stop[i]=MS;updatePWM();updatespeed(); } - if (cstate[i] && MS-stop[i]>CATCHSTOP && F[i]==0) { ////////////////////////// stop cat fish and start over + if (cstate[i] && MS-stop[i]>((long)CATCHSTOP*1000) && F[i]==0) { ///////////// stop cat fish and start over cstate[i]=0;F[i]=1;vts[i]=MS;rts=MS+2000;updatePWM();updatespeed(); } @@ -194,12 +219,19 @@ void loop() { ////////////////////////////////////////////////////////////////// if (bstate[M]==0) bset(M,1); else {;bset(M,0);if (BOFF) fset(M,0);} bclr=1; } + if (S==8 && !LOCK) { // exit config mode + M=2;save();updatemarker(); + if (SWAPENC) {;DT=clkorg;CLK=dtorg;} + else {;DT=dtorg;CLK=clkorg;} + } } save();updatespeed();bdelay=0; } else if (bdelay>0 && bdelay<20 && !LOCK) { // short button press, switch: menu -> fan 0 -> fan 1 - M++;if (S==8 && M==1) M=2;if (M>2) M=0; + M++; + if (S==8 && M==2) M=0; + if (M>2) M=0; updatemarker(); bdelay=0; } @@ -213,7 +245,7 @@ void loop() { ////////////////////////////////////////////////////////////////// if(!encval){ if (digitalRead(DT) != encval) { // turn encoder clockwise - if (M==2) S++; // scroll menu + if (M==2) S++; // scroll menu else { if (S==0) v[M]=speedinc(v[M],1); // fan speed up if (S==1) b[M]=speedinc(b[M],1); // boost speed up @@ -223,10 +255,11 @@ void loop() { ////////////////////////////////////////////////////////////////// if (S==5) rtime[M]+=RINC; // rise time up if (S==6) otime[M]+=OINC; // off time up if (S==7) rnval[M]+=RNDINC; // random value - if (S==8) brght++; // increase LCD brightness + if (S==8 && M==0) C++; // increase config menu + if (S==8 && M==1) *P[C] = cut(*P[C] + Cinc[C],Cmin[C],Cmax[C]); } } else { // turn encoder counterclockwise - if (M==2) S--; // scroll menu + if (M==2) S--; // scroll menu else { if (S==0) v[M]=speedinc(v[M],0); // fan speed down if (S==1) b[M]=speedinc(b[M],0); // boost speed down @@ -236,7 +269,8 @@ void loop() { ////////////////////////////////////////////////////////////////// if (S==5) rtime[M]-=RINC; // rise time down if (S==6) otime[M]-=OINC; // off time down if (S==7) rnval[M]-=RNDINC; // random value - if (S==8) brght--; // decrease LCD brightness + if (S==8 && M==0) C--; // decrease config menu + if (S==8 && M==1) *P[C] = cut(*P[C] - Cinc[C],Cmin[C],Cmax[C]); } } @@ -250,9 +284,11 @@ void loop() { ////////////////////////////////////////////////////////////////// if (S==5) rtime[M]=cut(rtime[M],0,240); // check rise time if (S==6) oset(M,otime[M]); // check off time if (S==7) {;rnval[M]=cut(rnval[M],0,1000);updatePWM();} // apply random value change - if (S==8) lset(); // set LCD brightness + if (S==8 && C==0) lset(); // set LCD brightness } + C=cut(C,0,18); // check cfg mode + if (M==2) S=cut(S,0,8); // check menu mode SAVE++;updatelcd();delay(25); @@ -264,6 +300,10 @@ void loop() { ////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// SUPPORT +void defcon(int n, int inc, int min, int max) { ///////////////////////////////////////////// set config limits + Cinc[n]=inc; Cmin[n]=min; Cmax[n]=max; +} + int speedinc(int s,int mode) { ////////////////////////////////////////////////////// calculate speed increment if (mode==0) { if (s<=SPEEDSTEP1) return s-SPEEDINC1; @@ -279,8 +319,8 @@ int speedinc(int s,int mode) { ///////////////////////////////////////////////// } void lset() { ////////////////////////////////////////////////////////////////////////////// set LCD brightness - brght=cut(brght,0,9); - analogWrite(PWM2,LCDB[brght]); + LGHT=cut(LGHT,0,9); + analogWrite(PWM2,LCDB[LGHT]); } void oset(int n,int t) { ///////////////////////////////////////////////////////////////// set switch off timer @@ -309,6 +349,7 @@ void bset(int n,int s) { /////////////////////////////////////////////////////// } void updatelcd() { ///////////////////////////////////////////////////////////////////////////////// update LCD + for (int i=0;i<2;i++) { if (S==0) {;slcd(1,1,5,"SPEED");ilcd(7+i*5,1,-4,v[i]);} if (S==1) {;slcd(1,1,5,"BOOST");ilcd(7+i*5,1,-4,int(b[i]));} @@ -322,9 +363,9 @@ void updatelcd() { ///////////////////////////////////////////////////////////// if (S==6) {;slcd(1,1,5,"OTIME");ilcd(7+i*5,1,-4,int(otime[i]));} if (S==7) {;slcd(1,1,5,"RNVAL");ilcd(7+i*5,1,-4,int(rnval[i]));} } - if (S==8) {;slcd(1,1,5,"BRGHT");ilcd(7,1,-4,brght);slcd(7+5,1,-4," ");} + if (S==8) {;slcd(1,1,-5,"CFG");slcd(7,1,-4,CF[C]);ilcd(7+5,1,-4,*P[C]);} } - + void updatemarker() { ////////////////////////////////////////////////////////////////////// update menu marker slcd(0,1,1," ");slcd(6,1,1," ");slcd(11,1,1," "); int m[]={6,11,0};if (!LOCK) clcd(m[M],1,0); @@ -405,7 +446,7 @@ void save() { ////////////////////////////////////////////////////////////////// eew(9+i,ctime[i]);eew(11+i,F[i]);eew(13+i,rtime[i]);eew(15+i,rnval[i]); } eew(100,seed); - eew(101,brght); + for (int i=0;i