Haha, I understand Your problem, and I missed the attachment. One thought: You register running time every minute, 60 000 milliseconds. Why not register ever hour, 3600 000 or every day?...... What accurazy do You need? For service and maintanance You don't need minutes I think. 100 000 registrations…. Does that couver the lifetime of Your compressor counting in days? Yes! 273 years... You have produced an impressive code! I take my hat off. Don't let anyone cut and paste Your code when it is running well!
Fouad19764:
just wondering if any optimisation or idea to make better.
Brace yourself. It's going to be a bumpy ride...
long RunTime ;
long PRunTime = 0;
...the correct datatype is unsigned long.
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;//smoother initialisation
}
for (int PthisReading = 0; PthisReading < PnumReadings; PthisReading++) {
Preadings[PthisReading] = 0;//smoother initialisation
}
...crt0 initializes those to zero before setup (main) is called. Don't bother. There are others.
lcd.print("A.D.T.S");
...
lcd.print("Pret ");//ready
...needs more F-macro.
for (int n = 0; n <= sizeof(BUTTON_STATE); n++) {
BUTTON_STATE[n] = 0;
}
...overruns BUTTON_STATE. That's a big no-no.
for (int n = 0; n <= sizeof(RELAY_STATE); n++) {
RELAY_STATE[n] = 0;
}
...ditto.
const int START_MS = 1500;//delay time for change status delta start
...
int last_state_change = 0;
...
int last_bounce = 0;
...
int m = millis();
...are all the wrong datatype.
char buf[100];
...is useless. Git rid of it.
Looking at previous remark I wonder about this:
for (int n = 0; n <= sizeof(BUTTON_STATE); n++) {
BUTTON_STATE[n] = 0;
}
for (int n = 0; n <= sizeof(RELAY_STATE); n++) {
RELAY_STATE[n] = 0;
BUTTON_STATE[15] can be accessed from [0] to [14]. I suggest the following code:
for (int n = 0; n < sizeof(BUTTON_STATE); n++) {
BUTTON_STATE[n] = 0;
}
for (int n = 0; n < sizeof(RELAY_STATE); n++) {
RELAY_STATE[n] = 0;
millis() is of the type UNSIGNED LONG. Use that data type for all math involving millis-data.
Railroader:
I suggest the following code:
Why? It's guaranteed to be filled with zeros at that point.
@C B. Looking closer at the code You are perfectly right! I find no jumping back to Setup()…..
Thanks all for your support.
i have modified the code according to your advice exceprt for
last_button_state = 0;
bool last_button_state = false.
i did not understand how to change :
/* check buttons */
int button_1 = digitalRead(BUTTON_1);
if (button_1 != last_button_state) {
/* reset bounce timer */
last_bounce = m;
}
compressor_controller_v1.91.ino (10.3 KB)
Railroader:
I find no jumping back to Setup()…..
In that circumstance refactoring should be done. The part of setup that is initialization should be separated from the part of setup that is reinitialization. Those two parts should then be put into two functions which are called from setup. The reinitialization function can be called as needed.