Pages: [1]   Go Down
Author Topic: kph to mph  (Read 472 times)
0 Members and 1 Guest are viewing this topic.
Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2997
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As you ask, the answer is "Yes".

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.
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you michael_x, i will give that a try and yes it's positive numbers.

thanks.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8488
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just work with metres instead of kilometres (ie everything 1000x larger) and format accordingly for display.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the value am reading is kph via a cars obd and i can't change that.
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

michael_x,

Code:
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:
int temp;
temp = str[0] / 1.609;

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

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

thanks for the help.
« Last Edit: June 10, 2012, 05:45:02 am by Pavilion1984 » Logged

Pages: [1]   Go Up
Jump to: