Simple math doing my head in - when is an int treated like an int?

An int can represent any integer value between -32768 and +32767

Anything outside this range wraps around.

130*255/1023. Think about how that sum works.

130 * 255 = 33150

That's outside the amount that an int will store.

You should either use an "unsigned int" that can store between 0 and 65535, or a "long" which can store considerably more (we're talking billions here).