Why the math operation is not working?

Hi. I tried to make a voltage meter with Arduino Nano and TV(I'm using TVOut lib). This is part of my code.

  TV.clear_screen();
  int voltage = 5/1023;
  voltage = voltage * analogRead(A0);
  TV.print(18,40,"VOLTAGE: ");
  TV.print(voltage);
  TV.print("V");
  delay(10);

I'm trying to divide number 5(because max is 5V) by 1023(because the max value that can analogRead give u is 1023) and then I multiply it by analogRead(A0).

This is the result:

And this is what I want:

If you cut 5 pies to serve 1023 people, how many whole pies does each person get?

That is what you are doing when you divide 5 by 1023. Dividing 5.0 by 1023.0 would produce a different result.

Dividing 5.0 by 1023.0 would produce a different result.

... but remember not to assign the result to an "int"

float voltage = 5.0/1023.0;
  TV.clear_screen();
  int voltage = 5.0/1023.0;
  voltage = voltage * analogRead(A0);
  TV.print(20,40,"VOLTAGE: ");
  TV.print(voltage);
  TV.print("V");
  delay(10);

still the same... :confused:

float voltage = 5.0/1023.0;

not

int voltage = 5.0/1023.0;

Thank you! Now It's working :slight_smile: