Uncommenting code makes program unresponsive

Hello everyone,
I am continuing with my Arduino dishwasher code. I have solved most of the bugs and implemented the resume cycle after power off function. Now I have to ask for help once again.
Why is it that, if I uncomment this part of the code in the file programFunctions.h, I can't select the program anymore? It's as if progIndex stayed stuck on 2 and it doesn't allow me to change it. Could anyone help me? I can't avoid using this function as it works very well for time counting, thanks

if (currentCycleState == cycleArray[progIndex].endPhase) {//se ti trovi nella fase finale del ciclo
    timeToDisplay = timeArray[cycleArray[progIndex].endPhase];//mostri il tempo della fase finale
  } else {
    long elapsedStageTime = millis() - stageStartTime;//calcoli da quanto tempo è iniziata la fase
    timeToDisplay = 0;
    if (elapsedStageTime <= timeArray[currentCycleState]) { // se il tempo trascorso è minore della durata della fase in corso
      for (byte i = currentCycleState + 1; i <= cycleArray[progIndex].endPhase; i++) { // sommi i tempi delle fasi successive
        timeToDisplay += timeArray[i];
      }
      timeToDisplay += timeArray[currentCycleState]; // aggiungi il tempo della fase corrente
      timeToDisplay -= elapsedStageTime; // diminuisci del tempo già trascorso nella fase corrente
    } else { // altrimenti se il tempo trascorso è maggiore della durata della fase mostri il tempo delle fasi successive
      for (byte i = currentCycleState + 1; i <= cycleArray[progIndex].endPhase; i++) { // sommi i tempi delle fasi successive
        timeToDisplay += timeArray[i];
      }
    }
  }

  if (timeToDisplay / K >= 100) {
    display.setCursor(0, 1);
  } else if (timeToDisplay / K >= 10) {
    display.setCursor(0, 2);
  } else if (timeToDisplay / K < 10) {
    display.setCursor(0, 3);
  }

  if (timeToDisplay == 99 && clearDone == false) {
    display.clear();
    clearDone = true;
  } else if (timeToDisplay == 9 && clearDone == false) {
    display.clear();
    clearDone = true;
  }

  if (timeToDisplay == 98) {
    clearDone = false;
  }
  display.setColonOn(false);
  Serial.println(timeToDisplay / K);
  display.print(timeToDisplay / K);

Arduino_Dishwasher_7SEG.zip (16.0 KB)

That's quite a large chunk of code. Maybe try narrowing it down a bit further by commenting out groups of statements and then finally individual statements.

Hello

There is a problem with this array long timeArray[27];

When you do timeArray[27] = K;//final drain, you are writing outside of the array because the last element is in 26, not 27. That is a so called "buffer overflow" and can be the cause of weird behavior or crash of the program

And here:
byte endPhase = 27;

for (byte i = currentCycleState + 1; i <= cycleArray[progIndex].endPhase; i++) { // sommi i tempi delle fasi successive
  timeToDisplay += timeArray[i];
}

For the same reason, you read from outside of the array

Never read or write outside of an array

You may have other similar errors

Thank you so much for your contribution. I simply edited timeArray[27] into timeArray[28] and the code runs perfect now. Again, thank you so much, I've spent quite a bit of time trying to figure this out

1 Like

You should mark reply #3 as the "Solution" so @guix gets credit, not your reply to the solution.

Whoops, my bad. Fixed it now

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.