Arduino code to convert between two units of measure

Please help me to create code that will do some conversion between two units of measure. They will measure the same thing (length, pressure, time, currency etc.) but will convert from one unit to another.

for example:

void setup()
{
  Serial.begin(9600);
  int degC = 20;
  int degF;
  degF = degC * 9 / 5 + 32;
  Serial.println(degF); 
}

Looks like you have already solved your problem. The Arduino doesn't know anything about units. But if you know the math it can do that really easy. What you did in your example is exactly how you would do it anywhere.

talal_ja may want float or double instead of int so that the calculations work as expected for other input values.

vaj4088:
talal_ja may want float or double instead of int so that the calculations work as expected for other input values.

+1

(and use float constant like const 9.0 , not only 9)

Making the constants float would make the intent more clear but is not required because of the way that the equation is arranged and is required to be evaluated under the specifications of C/C++.

Personally I would introduce some brackets into the calculation to make it crystal clear what order the calculation was done in.

That would be nice too, as long as it doesn't introduce int truncation that is not there now.

I agree with you that the intent should be made quite clear.

Hello vaj4088

vaj4088: Making the constants float would make the intent more clear but is not required because of the way that the equation is arranged and is required to be evaluated under the specifications of C/C++.

I was talking also of "implicit" constant like in :

degF = degC * 9 / 5 + 32;

Regards, bidouilleelec

I do not understand implicit constant, but there are no parentheses so the calculation will be performed from left to right. If degC is double, then the 9 becomes 9.0, then the 5 becomes 5.0, then the 32 becomes 32.0 (and, of course, the calculations and the results are double).

I have shortcut some of the details but that is the gist of what happens.

vaj4088: I do not understand implicit constant, but there are no parentheses so the calculation will be performed from left to right. If degC is double, then the 9 becomes 9.0, then the 5 becomes 5.0, then the 32 becomes 32.0 (and, of course, the calculations and the results are double).

I have shortcut some of the details but that is the gist of what happens.

I was thinking about this kind of problem (try it) :

void setup()
{
  Serial.begin(2000000);
  int degC = 20;
  float degF;
  float degF2;

  degF   = degC * 50  * 1000 ;
  degF2 = degC * 50. * 1000. ;

  Serial.println(degF);
  Serial.println(degF2);
}



void loop() {
  // put your main code here, to run repeatedly:

}

Regards, bidouilleelec

vaj4088: ...there are no parentheses so the calculation will be performed from left to right...

For those new to Arduino programming the calculation is performed from left to right in precedence order so these two are equivalent...

  degF = degC * 9 / 5 + 32;
  degF = 32 + degC * 9 / 5;

The compiler seems them as...

  degF = ((degC * 9) / 5) + 32;
  degF = 32 + ((degC * 9) / 5);

Because the variable degC is declared int, this

  degF   = degC * 50  * 1000 ;

will be evaluated using (16-bit) int arithmetic and will have a rollover problem.

This

  degF2 = degC * 50. * 1000. ;

has no such problem because the value from the variable degC will be upgraded to a double and thus all of the arithmetic will be performed as double and then downgraded to float for assignment to degF2. There is no rollover issue here.

Note that if the variable degC had been declared float or double, even the first expression would have worked well because EVERYTHING (values and arithmetic) would have been handled as double until the assignment to the float variable degF.

vaj4088: Because the variable degC is declared int, this

  degF   = degC * 50  * 1000 ;

will be evaluated using (16-bit) int arithmetic and will have a rollover problem.

This

  degF2 = degC * 50. * 1000. ;

has no such problem because the value from the variable degC will be upgraded to a double and thus all of the arithmetic will be performed as double and then downgraded to float for assignment to degF2. There is no rollover issue here.

Note that if the variable degC had been declared float or double, even the first expression would have worked well because EVERYTHING (values and arithmetic) would have been handled as double until the assignment to the float variable degF.

Thanks but I knew that.