Why am I getting negative numbers?

Type int can hold values of up to between -32767 and 32768 you are adding 50 values of up to 1023 which can overrun 32768 and push the value into the negative range of the variable. If you read the reference to the arduino language it explains what 'long' type is. It can hold a much larger number before it overflows in simple terms. You could get away with 'unsigned int' which cannot give negative values and doesn't overflow until it gets past 65535.

1 Like