Use unsigned long from mills() in a division

westfw:
First of all, since you're using an ESP8266, you can forget about all of the comments about "int" vs "long" - on an ESP8266, int and long are both 32bits.

I suspect that since you're using a callback function, which is sort-of like an interrupt, that you need to make newDuration into a "volatile":

volatile unsigned long newDuration;

What's probably happening is that the compiler notices that newDuration is initialized to 0, and there is no code path that ever sets it, it will always be 0, so there's no need to do that actual calculation at runtime.
This could apply to any other variables modified in the callback and uses elsewhere as well.

Thanks for the advice, but even after declaring

volatile unsigned long newDuration;

at the beginning of the code it makes no difference.

Yes I forgot to mention this is a esp8266, so the code can also be tested on a D1 mini or such type of board, if anyone is interested on trying.

What values are you seeing for newDuration? It has to be at least 300 before (100*newDuration)/30000 will have a value higher than 0.

the code can also be tested on a D1 mini or such type of board, if anyone is interested on trying.

Alas, I'm about 4 libraries and an MQTT server short of being able to "try"...

VanaArdod:
Thank you, but sadly it still gives me 0 on the serial instead of the desired percentage.

unsigned long Y = ( ((unsigned long) B) *X)/A;

this should work according to the documentation, i think the cast is otherwise to the result of the multiplication, which is what we want done 32-bit (not 16-bit signed, actually we don't want it signed at all)

best is of course to declare them as unsigned long, if that is what you want the variables to hold.

i tend to use uint32_t, uint16_t etc. to declare my variables since it is more easy to what they really are.

VanaArdod:
That's what I did on the first post, but then you said this

The stuff in your Original Post is NOT a complete program. And there are two pieces of code that seem to have no relationship with each other.

...R

What values are you seeing for newDuration? It has to be at least 300 before (100*newDuration)/30000 will have a value higher than 0.

casting in Arduino C++ is much more sensitive than in normal C++

AWOL:
What do you mean?

Doesn't realize that the rules are exactly the same.

But the default integer size is different from most C/C++ installs, for the ATmega the default
size is 16 bit, casting rules use the default size.

Guys I solved it!!!

It happens that the newDuration value coming from the loop was reaching calculateLastPosition after the actual equation had time to be executed so the newDuration was still 0 every time, what I did is to add a small delay so the the newDuration could be calculated before and then execute the calculateLastPosition.