When sampling from A0 with a 0-5V signal on an arduino micro with the code below I get some negative values.
int sensor = 0; sensor = abs(analogRead(A0) - 512);
77 25 -74 -58 46 113 -74 102 -91 -51 -126 47 31
When running the (apparently) mathematically equivalent code below I get exclusively positive values.
int sensor = 0; sensor = analogRead(A0); sensor = sensor - 512; sensor = abs(sensor);
I don't think this is an integer overflow as in this post because when I switch to
long sensor = 0 I get the same negative results.
What's going on here?