Go Down

### Topic: kph to mph (Read 1 time)previous topic - next topic

#### Pavilion1984

##### Jun 10, 2012, 11:36 am
hi all, a little help needed.

kph / 1.609 = mph so i have seen on the internet but i don't know how i would do this in arduino. do i have to use a float?

thanks.

#### michael_x

#1
##### Jun 10, 2012, 11:56 am

However, to work with long, you could easily multiply by 10000L, add 5 for perfect rounding, and then divide by 16090
to get mph from a km/h value.

int mph = (int)((kph * 10000L + 5)/ 16090);
BTW: rounding assumes you work with positive values.

#### Pavilion1984

#2
##### Jun 10, 2012, 12:08 pm
thank you michael_x, i will give that a try and yes it's positive numbers.

thanks.

#3
##### Jun 10, 2012, 12:09 pm
Just work with metres instead of kilometres (ie everything 1000x larger) and format accordingly for display.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

#### Pavilion1984

#4
##### Jun 10, 2012, 12:21 pm
the value am reading is kph via a cars obd and i can't change that.

#### Pavilion1984

#5
##### Jun 10, 2012, 12:25 pmLast Edit: Jun 10, 2012, 12:45 pm by Pavilion1984 Reason: 1
michael_x,

Code: [Select]
`int temp;temp = ((kph * 10000L + 5)/ 16090);`

that worked fine but this also worked fine but only returned a value with no decimal. i don't want the decimal anyway.

Code: [Select]
`int temp;temp = str[0] / 1.609;`

this is what i have done now and it looks to work fine.

Code: [Select]
`    case VS:    temp = (str[0] * 10000L) / 16090L;    sprintf(PID_result, ("%d MPH"), temp);    break;`

thanks for the help.

Go Up