You overflow the int data type. The signed value can only be -32768 to +32767. See int docs
Your formula is all int , and the compiler will not guess that you might need a larger intermediate value. Multiplication by 180 is the red flag. (2000-1250) * 180 = 135000 = arrow
To understand the math, divide the formula into separate operations, as shown in the test program below. This is what the compiler does for you.
Run the program below and you will see a crash. Immediately after the out value reaches 45, the intermediate value overflows and the formula breaks.
in: 1040 out: 39 t0: -210 t1: 27736 t2: 39 in: 1048 out: 41 t0: -202 t1: 29176 t2: 41 in: 1056 out: 43 t0: -194 t1: 30616 t2: 43 in: 1064 out: 45 t0: -186 t1: 32056 t2: 45 in: 1072 out: -45 t0: -178 t1: -32040 t2: -45 in: 1080 out: -43 t0: -170 t1: -30600 t2: -43
Use this program below as a test fixture. Change the data types to use unsigned int, and you can draw the output as you need.
int ch3; int degree; void setup() { ch3 = 1000; Serial.begin(9600); } void loop() { int t0, t1, t2; degree = ((ch3-1250)* 180)/700; t0 = ch3 - 1250; t1 = t0 * 180; t2 = t1 / 700; Serial.print("in: "); Serial.print(ch3); Serial.print(" out: "); Serial.print(degree); Serial.print(" t0: "); Serial.print(t0); Serial.print(" t1: "); Serial.print(t1); Serial.print(" t2: "); Serial.println(t2); ch3 += 8; if(ch3 > 2400) { ch3 = 1000; } delay(100); }
source share