Variable value matches in if statement, yet serial.print shows it should not?

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.

What gets you out of that while loop?
Where is the rest of the code?

i use an interrupt commanded by a switch to change it. It works.

currentScenarioIndex is not accessed anywhere else than what is shown here.

totalScenarioLength was just defined as an alternative to using SCENARIO_LENGTH in the if statement, after seeing the problem, and is not used anywhere else either.

Did you declare the variable changed in the interrupt routine as "volatile"?

Yes.

The problem is not starting the program, that part works well.

The problem is the if statement in the loop matching when the Serial.print says it should not.

Do i need to set currentScenarioIndex as volatile too ?

What is happening to the value of loopcounter?

I don't think we can help without seeing the rest of the code. The problem is not obvious from this snippet.

The rest of the code does not matter since it is not executed.

I modified the first message with the code update and log update.

For whatever reason, currentScenarioIndex is set to 5, then back to 13 once the loop function is exited or restarts. I tried setting that variable as volatile, and it did not change anything.

It helps to know how variables are declared. It helps to know how arrays are declared. Without these details there is no way to know if you are writing past the end of arrays and causing a memory corruption (which is what I suspect).

Doesn't matter. Volatile is only needed when variables are shared with interrupts.

But it would tell us about the datatypes.

Never mind

I think I found it ... user error as always.

i added logs everywhere, including the loop that increases currentScenarioIndex, and the behavior was completely normal.

My error was that I introduced a transistion mecanism in the scenario ( the software controls servos that open shutters on something similar to a child book).
These transistions have a step count defined in the scenario structure, and it gets decreased on every loop that this scenario step is executed, until it reaches 0 .

Of course, once it reaches 0, it gets ignored ... so the program WAS restarting at ScenarionIndex 5... but all the transition steps were at 0, so it just blew through everything without controlling the servos in any way.

Now i have to find way to restore these steps...

Nice work.

FWIW I cannot tell you how many times I have been stuck on something stubborn and in the process of thinking through how to present it for the best possibility of getting help, I find one or more errors and fix myself up… and get to keep the whole matter to myself. :wink:

a7

Yeah i'd rather say how I solved it, it may help others.

In this case, do not trust yourself when saying something is not changed, and trust people saying "post the whole code" !

1 Like

Haha, if I posted every problem I solved myself before ever letting anyone know I had one I'd need my own channel. As, I am sure, would a large number of us.

But I agree, it is only right and polite to let us see the solution if we've had to slog through the whole thread… so THX.

a7

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