Hi everyone. I am using an arduino uno and the IDE v1.8.13 to compile, write and transfer.
Here is my loop() code. It basically follows a scenario, which has steps and substeps.
The idea was, once the scenario is over, it should start again but at a later stage.
And it works very well, the start of the system is done using an interrupt that sets the scenarioStarted variable from 0 to 1.
The problem happens when it reaches the end of the scenario, which is generated in the setup, then only accessed in reading. Before that, everything works very well.
At that point, it enter the if statement trying to go back to the step defined in scenarioTable.restartIndex. the scenario length is 13, the restart index is 5.
My problem is that once that step is reached, the serial output endlessly repeats this:
Current scenario index 5 out of 13 Restarting at index 5
Like it enters the if statement every time it loops. I do not understand how ? If the variables had these values, it should not even enter the if statement.
Can someone explain this to me ? What am I missing here ?
Note: the static declaration and the totalScenarioLength variable were added after, to check wether using the macro directly in the if statement was a problem (it was not).
Thanks !
static int currentScenarioIndex = 0;
int totalScenarioLength = SCENARIO_LENGTH;
void loop()
{
while (scenarioStarted != 1);
if (scenarioTable.endAt[currentScenarioIndex] == loopCounter)
currentScenarioIndex++;
if (currentScenarioIndex == totalScenarioLength)
{
currentScenarioIndex = scenarioTable.restartIndex;
loopCounter = scenarioTable.startAt[currentScenarioIndex];
Serial.print(F("Current scenario index "));
Serial.print(currentScenarioIndex, DEC);
Serial.print(F(" out of "));
Serial.print(totalScenarioLength, DEC);
Serial.print(F(" Restarting at index "));
Serial.println(scenarioTable.restartIndex, DEC);
}
else
{
doStuff(currentScenarioIndex);
loopCounter++;
}
}
edit:
update: i changed the log in the if statement to :
Serial.print(F("before change scenario index "));
Serial.print(currentScenarioIndex, DEC);
Serial.print(F(" out of "));
Serial.print(totalScenarioLength, DEC);
Serial.print(F(" Restarting at index "));
Serial.println(scenarioTable.restartIndex, DEC);
currentScenarioIndex = scenarioTable.restartIndex;
loopCounter = scenarioTable.startAt[currentScenarioIndex];
Serial.print(F("after change, scenario index "));
Serial.print(currentScenarioIndex, DEC);
Serial.print(F(" out of "));
Serial.print(totalScenarioLength, DEC);
Serial.print(F(" Restarting at index "));
Serial.println(scenarioTable.restartIndex, DEC);
and the serial enlessly shows:
before change scenario index 13 out of 13 Restarting at index 5
after change, scenario index 5 out of 13 Restarting at index 5
meaning the value is set back to 13 after the log . No idea why. There is not a single occurence of this variable in the program. Now running with the variable declared as volatile... even if it solves the problem, i'd really like to understand why it changes back to the previous value between 2 calls to the loop function.
edit:
also tried completely commenting the doStuff() call. Still the same result.