Skip to content

Commit 5c5e933

Browse files
committed
working - cycleDisplayNew didn't work well so adapting cycleDisplay to it slowly. Crashes on dim state.
1 parent c68c0f2 commit 5c5e933

File tree

1 file changed

+165
-27
lines changed

1 file changed

+165
-27
lines changed

sixtube_lm/sixtube_lm.ino

Lines changed: 165 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -990,12 +990,43 @@ byte binOutB[4] = {6,7,8,9};
990990
//3 pins out to anode channel switches
991991
byte anodes[3] = {11,12,13};
992992

993-
float fadeMax = 5.0f;
994-
float fadeStep = 1.0f;
995993
int displayLast[6]={11,11,11,11,11,11}; //What is currently being displayed. We slowly fade away from this.
996-
float displayNextFade[6]={0.0f,0.0f,0.0f,0.0f,0.0f,0.0f}; //Fading in displayNext values
997-
float displayLastFade[6]={8.0f,8.0f,8.0f,8.0f,8.0f,8.0f}; //Fading out displayLast values
998-
unsigned long setStartLast = 0; //to control flashing
994+
995+
//ms
996+
/*
997+
------ ------ ------
998+
full transition, let's say is 200ms
999+
transition start is 10000ms
1000+
cycle = 6 (normal) or 3 (dim)
1001+
mils diff at 1st cycle is 0; nextDur = ((diff*(6-1))/200)+1 = 1; lastDur = (6-nextDur) = 5;
1002+
mils diff at 2nd cycle is 10; nextDur = 1....
1003+
3rd - 20
1004+
4th - 30
1005+
5th - 40; nextDur = 2.... lastDur = 4......
1006+
1007+
1st - 0 - /0!!!!!!!!
1008+
1009+
at 6ms per digit, 1ms change each time, 3 pairs of digits = 75ms transition.
1010+
=====- =====- =====-
1011+
====-- ====-- ====--
1012+
===--- ===--- ===---
1013+
==---- ==---- ==----
1014+
=----- =----- =-----
1015+
at 4ms per digit, 4x4x3 = 48ms
1016+
at 3ms per digit, 3x3x3 = 27ms
1017+
at 6ms 6x6x3 = 108ms
1018+
*/
1019+
const char fadeDur = 6; //each multiplexed pair of digits appears for this amount of time: partly next digit, partly last digit, partly dim (if applicable)
1020+
const char dimDur = 4; //half of fadeDur for half brightness? don't go over fadeDur-2
1021+
char fadeNextDur = 0; //Fading in displayNext values --TODO put back at 0
1022+
char fadeLastDur = 6; //Fading out displayLast values
1023+
unsigned long fadeStartLast = 0; //when the last digit fade was started
1024+
byte cycleStage = 0; //Which stage of the multiplexing cycle we're in
1025+
unsigned long cycleLast = 0; //when the last stage was started
1026+
char cycleDelay = 0; //how long until the next stage starts - set from fadeNextDur and fadeLastDur
1027+
unsigned long setStartLast = 0; //to control flashing during start
1028+
1029+
word outputCounter = 0;
9991030

10001031
void initOutputs() {
10011032
for(byte i=0; i<4; i++) { pinMode(binOutA[i],OUTPUT); pinMode(binOutB[i],OUTPUT); }
@@ -1004,61 +1035,168 @@ void initOutputs() {
10041035
}
10051036

10061037
void cycleDisplay(){
1038+
unsigned long mils = millis();
1039+
10071040
//Other display code decides whether we should dim per function or time of day
10081041
bool dim = (displayDim==1?1:0);
10091042
//But if we're setting, decide here to dim for every other 500ms since we started setting
10101043
if(fnSetPg>0) {
1011-
if(setStartLast==0) setStartLast = millis();
1012-
dim = 1-(((millis()-setStartLast)/500)%2);
1044+
if(setStartLast==0) setStartLast = mils;
1045+
dim = 1-(((mils-setStartLast)/500)%2);
10131046
} else {
10141047
if(setStartLast>0) setStartLast=0;
10151048
}
10161049

1050+
// Loop thru and update all the arrays, and fades.
1051+
fadeLastDur = fadeDur-(dim?dimDur:0); //default value
1052+
if(readEEPROM(20,false)==0) { //fading disabled
1053+
for(byte i=0; i<6; i++) if(displayNext[i] != displayLast[i]) displayLast[i] = displayNext[i];
1054+
} else { //fading enabled
1055+
if(fadeStartLast==0) { //not fading - time to fade?
1056+
for(byte i=0; i<6; i++) if(displayNext[i] != displayLast[i]) { fadeStartLast = mils; break; }
1057+
}
1058+
if(fadeStartLast!=0) { //currently fading
1059+
fadeNextDur = (((mils-fadeStartLast)*(fadeDur-(dim?dimDur:0)-1))/(readEEPROM(20,false)*10))+1; //partial based on time since fadeStatLast and EEPROM overall digit fade setting
1060+
fadeLastDur = fadeDur - fadeNextDur;
1061+
if(fadeNextDur >= fadeDur) { //fade is over
1062+
fadeStartLast = 0;
1063+
fadeNextDur = 0;
1064+
fadeLastDur = fadeDur;
1065+
for(byte j=0; j<6; j++) displayLast[j] = displayNext[j];
1066+
} //end fade is over
1067+
} //end curently fading
1068+
} //end fading enabled
1069+
10171070
if(displayDim>0) { //if other display code says to shut off entirely, skip this part
10181071
//Anode channel 0: tubes #2 (min x10) and #5 (sec x1)
10191072
setCathodes(displayLast[2],displayLast[5]); //Via d2b decoder chip, set cathodes to old digits
10201073
digitalWrite(anodes[0], HIGH); //Turn on tubes
1021-
delay(displayLastFade[0]/(dim?4:1)); //Display for fade-out cycles
1074+
delay(fadeLastDur-(dim?dimDur:0)); //Display for fade-out cycles
10221075
setCathodes(displayNext[2],displayNext[5]); //Switch cathodes to new digits
1023-
delay(displayNextFade[0]/(dim?4:1)); //Display for fade-in cycles
1076+
delay(fadeNextDur-(dim?dimDur:0)); //Display for fade-in cycles
10241077
digitalWrite(anodes[0], LOW); //Turn off tubes
10251078

1026-
if(dim) delay(fadeMax/1.5);
1079+
if(dim) delay(dimDur);
10271080

10281081
//Anode channel 1: tubes #4 (sec x10) and #1 (hour x1)
10291082
setCathodes(displayLast[4],displayLast[1]);
10301083
digitalWrite(anodes[1], HIGH);
1031-
delay(displayLastFade[1]/(dim?4:1));
1084+
delay(fadeLastDur);
10321085
setCathodes(displayNext[4],displayNext[1]);
1033-
delay(displayNextFade[1]/(dim?4:1));
1086+
delay(fadeNextDur);
10341087
digitalWrite(anodes[1], LOW);
10351088

1036-
if(dim) delay(fadeMax/1.5);
1089+
if(dim) delay(dimDur);
10371090

10381091
//Anode channel 2: tubes #0 (hour x10) and #3 (min x1)
10391092
setCathodes(displayLast[0],displayLast[3]);
10401093
digitalWrite(anodes[2], HIGH);
1041-
delay(displayLastFade[2]/(dim?4:1));
1094+
delay(fadeLastDur);
10421095
setCathodes(displayNext[0],displayNext[3]);
1043-
delay(displayNextFade[2]/(dim?4:1));
1096+
delay(fadeNextDur);
10441097
digitalWrite(anodes[2], LOW);
10451098

1046-
if(dim) delay(fadeMax*0.75);
1099+
if(dim) delay(dimDur);
10471100
} //end if displayDim>0
10481101

1049-
// Loop thru and update all the arrays, and fades.
1050-
for( byte i = 0 ; i < 6 ; i ++ ) {
1051-
if( displayNext[i] != displayLast[i] ) {
1052-
displayNextFade[i] += fadeStep;
1053-
displayLastFade[i] -= fadeStep;
10541102

1055-
if( displayNextFade[i] >= fadeMax ){
1056-
displayNextFade[i] = 0.0f;
1057-
displayLastFade[i] = fadeMax;
1058-
displayLast[i] = displayNext[i];
1059-
}
1103+
1104+
// if(outputCounter<20) {
1105+
// Serial.print(fadeStartLast,DEC);
1106+
// Serial.print(F(" "));
1107+
// Serial.print(fadeLastDur,DEC);
1108+
// Serial.print(F(" "));
1109+
// Serial.print(fadeNextDur,DEC);
1110+
// Serial.print(F(" "));
1111+
// for(byte k=0; k<6; k++) { Serial.print(displayLast[k]); Serial.print(F(",")); }
1112+
// Serial.print(F(" "));
1113+
// for(byte l=0; l<6; l++) { Serial.print(displayNext[l]); Serial.print(F(",")); }
1114+
// Serial.println();
1115+
// outputCounter++;
1116+
// }
1117+
1118+
} //end cycleDisplay()
1119+
1120+
void cycleDisplayNew(){
1121+
unsigned long mils = millis();
1122+
1123+
if(cycleLast < mils+cycleDelay) {
1124+
cycleLast = mils;
1125+
1126+
//Other display code decides whether we should dim per function or time of day
1127+
bool dim = (displayDim==1?1:0);
1128+
//But if we're setting, decide here to dim for every other 500ms since we started setting
1129+
if(fnSetPg>0) {
1130+
if(setStartLast==0) setStartLast = mils;
1131+
dim = 1-(((mils-setStartLast)/500)%2);
1132+
} else {
1133+
if(setStartLast>0) setStartLast=0;
10601134
}
1061-
}
1135+
1136+
if(displayDim>0) { //if other display code says to shut off entirely, skip this part
1137+
1138+
switch(cycleStage){
1139+
case 0: //off and delay for dim, if applicable - also shift the delays
1140+
cycleStage++;
1141+
digitalWrite(anodes[2], LOW);
1142+
if(readEEPROM(20,false)>0) { //if fade enabled
1143+
if(fadeStartLast==0) { //If we've completely faded, check to see if new differs from old, then initiate a new fade
1144+
for(byte i=0; i<6; i++) if(displayNext[i] != displayLast[i]) { fadeStartLast = mils; break; }
1145+
}
1146+
if(fadeStartLast!=0) { //If we're working on fading
1147+
fadeNextDur = ((mils-fadeStartLast*(fadeDur-(dim?dimDur:0)-1))/(readEEPROM(20,false)*10))+1; //partial based on time since fadeStatLast and EEPROM overall digit fade setting
1148+
if(fadeNextDur >= fadeDur) { fadeNextDur = fadeDur; fadeStartLast = 0; for(byte j=0; j<6; j++) displayLast[j] = displayNext[j]; } //end of the fade
1149+
fadeLastDur = fadeDur-(dim?dimDur:0)-fadeNextDur; if(fadeLastDur > fadeDur) fadeLastDur = 0; //just in case it loops around (does this happen?)
1150+
}
1151+
} else { //no fade - we don't care about any of the last stuff, always go straight to next
1152+
fadeNextDur = fadeDur-(dim?dimDur:0);
1153+
}
1154+
if(dim) { cycleDelay = dimDur; break; } //otherwise continue
1155+
case 1: //Anode channel 0: tubes #2 (min x10) and #5 (sec x1), outgoing digits on
1156+
cycleStage++;
1157+
setCathodes(displayLast[2],displayLast[5]); //Via d2b decoder chip, set cathodes to old digits
1158+
digitalWrite(anodes[0], HIGH); //Turn on tubes
1159+
if(fadeLastDur>0) { cycleDelay = fadeLastDur; break; } //Display for fade-out cycles, if applicable //(displayLastFade[0]/(dim?4:1))
1160+
case 2: //incoming digits on
1161+
cycleStage++;
1162+
setCathodes(displayNext[2],displayNext[5]); //Switch cathodes to new digits
1163+
cycleDelay = fadeNextDur; break; //Display for fade-in cycles
1164+
case 3: //off and pause for dim
1165+
cycleStage++;
1166+
digitalWrite(anodes[0], LOW); //Turn off tubes
1167+
if(dim) { cycleDelay = dimDur; break; } //otherwise continue
1168+
case 4: //Anode channel 1: tubes #4 (sec x10) and #1 (hour x1), outgoing digits on
1169+
cycleStage++;
1170+
setCathodes(displayLast[4],displayLast[1]);
1171+
digitalWrite(anodes[1], HIGH);
1172+
if(fadeLastDur>0) { cycleDelay = fadeLastDur; break; }
1173+
case 5: //incoming digits on
1174+
cycleStage++;
1175+
setCathodes(displayNext[4],displayNext[1]);
1176+
cycleDelay = fadeNextDur; break;
1177+
case 6: //off and pause for dim
1178+
cycleStage++;
1179+
digitalWrite(anodes[1], LOW);
1180+
if(dim) { cycleDelay = dimDur; break; } //otherwise continue
1181+
case 7: //Anode channel 2: tubes #0 (hour x10) and #3 (min x1), outgoing digits on
1182+
cycleStage++;
1183+
setCathodes(displayLast[0],displayLast[3]);
1184+
digitalWrite(anodes[2], HIGH);
1185+
if(fadeLastDur>0) { cycleDelay = fadeLastDur; break; }
1186+
case 8: //incoming digits on
1187+
cycleStage=0;
1188+
setCathodes(displayNext[0],displayNext[3]);
1189+
cycleDelay = fadeNextDur; break;
1190+
default: break;
1191+
} //end switch(cycleStage)
1192+
1193+
} //end if displayDim>0
1194+
else if(fadeLastDur>0) { //if we've just shut off the display in the middle of a fade (does this ever happen?), set things up for when display comes back
1195+
fadeLastDur = 0; //force a full fade in to the new value...
1196+
for(byte k=0; k<6; k++) { displayLast[k] = 0; } //...from a blank display
1197+
} //end if fadeLastDur>0
1198+
1199+
} //end cycleLast cycle
10621200
} //end cycleDisplay()
10631201

10641202
void setCathodes(byte decValA, byte decValB){

0 commit comments

Comments
 (0)