Wrong calculation UNO I've a joystick and a servo connected to my UNO, but for some reason my calculations keep on being wrong? I'm trying to calculate how the rotation should be of my servo (Tower Pro 9g, max value of 180 I think) with my Joystick (Keyes_SJoys, max value of 1023 on any axis) and example would be:

y = 500;
servo.write(y * 180 / 1023); // should equal to 87.97, but in reality it's 24?

#include &lt;Servo.h&gt;

Servo servo;
int d2 = 2;
int a0 = 0;

void setup() {
  Serial.begin(9600);
  servo.attach(d2);
}

void loop() {
  int y = analogRead(a0);
  Serial.print("y: "); Serial.println(y);

  servo.write(y * 180 / 1023);
  Serial.println(y * 180 / 1023);

  delay(100);
}


PS. It's a Robotale UNO; I don't know if that makes any difference or not, but.
What you are experiencing is a common pitfall.
You are probably expecting floating point calculations, but what you are writing are only integer ones.

Try with:  y * 180. / 1023.   (notice the dots).

This will work only with constants.

To make it work with anything, use a cast:  y * 180 / (float) 1023

Happy hacking.