Strange compiler behavior

Hi,
I’ve a strange behavior from compiler. I’ve a skecth (in attachments) with a problem (a variable seems to have a wrong value) and, for diagnostic, I’ve placed a Serial.print of the variable.
When the skect has the Serial.print line the variable works fine, without Serial.print the variable assume undeterminated value (probably 1, I’m not sure about this).

Below the section with the problem:

Variazione = (Setpoint - MotorSpeedCorrente)/3;
Serial.print(Variazione);
Output_tmp = Output + Variazione;
Output = constrain(Output_tmp,0,255); // mi accerto che l’output sia compreso nell’intervallo ammesso
Output = avviolento(); // eseguo un avvio lento per evitare l’impuntamento dell’alimentatore
analogWrite(PinMotore, int(Output));

affected variable is Variazione. It is declared as Int, to truncate the result of the division in the first line

Anyone has idea about this strange problem?

Thanks

Carlo

tornio_motore.ino (17.9 KB)

Serial.print() can act as a very short delay(). It would be interesting to see if delay(1) works in place of Serial,print().

But why, is another matter.

...R

But why, is another matter.

One that the snippet will never reveal.

OP: Post ALL of your code. It is more likely that you are stepping on that variable somewhere, by writing beyond the end of an array, for example. Adding the Serial.print() moves code around a bit, so you step on something else instead.

@PaulS, look closely Paul! Clean your glasses or what, he did post all the code. Granted, code tags would have been nice. Italian section as well but he did post it :wink:

@carloroma63, a small tip, not all variables need to be global. This can even lead to some unexpected behavior (not saying it's the case here!). Try to declare a variable where you need it. And pass variables to other functions if you need it there as well.

septillion:
@PaulS, look closely Paul! Clean your glasses or what, he did post all the code. Granted, code tags would have been nice. Italian section as well but he did post it :wink:

@carloroma63, a small tip, not all variables need to be global. This can even lead to some unexpected behavior (not saying it's the case here!). Try to declare a variable where you need it. And pass variables to other functions if you need it there as well.

This morning "Code" tag was "hidden" to my eyes.... next time I'll use it. In English section there are more chance to have a response...

However, I'll try this evening some change to code. I'll keep you update :slight_smile:

In this moment, code is in a "non optimized" version, I'm still studing how to do some things, because hardware (it's a CNC lathe machine) do not work exactly as expected and code should correct these problems.

When logic will be OK, I will optimize the code: remove some variable not used, set correct variable scope, etc etc.

Thanks

Carlo

carloroma63:
When logic will be OK, I will optimize the code: remove some variable not used, set correct variable scope, etc etc.

Why wait? The point of those things is to make it easier for you to get your code working and to reduce the possibility of making mistakes. Waiting until the end of the process means you lose those benefits.

carloroma63:
When logic will be OK, I will optimize the code: remove some variable not used, set correct variable scope, etc etc.

If you are like me that will never happen. There will some other new project that gets your attention. Do it NOW.

...R