Go Down

### Topic: converting feet to metres with decimal points (Read 2249 times)previous topic - next topic

#### phillmybuttons

##### Jan 18, 2013, 12:26 am
Hi all,
first time poster but long time reader and user
anyway i have a problem with math, im trying to convert feet to metres on a button press. at the minute i have an array counting from 0.0ft to 10.6 ft and the corresponding metre code in a switch case like the example, the switch case points to a function under as void one(); (see the example).

now the obvious problem straight away is 12 inches times 10 feet = a lot of switch cases and a bloated code. but does make it easy writing to sd card and lcd screen,

I tried several ways to count up but none work, i dont know enough about arrays and bits to even start a code, i have tried examples and studying the process of it but it just doesnt click?

I know there is a simple way to count up through this range without hundreds of lines of code. can anybody help me with this? i cant post my code as it keeps changing forms and its way to long but an example of my switch case is as follows

//example
switch (count){  //my generic switch code, counts up with button press
case 1:
one(); //points to below example shortened
break;
}
//void one as example, excuse the mistakes its from memory and does work
void one(){
lcd.clear();
lcd.setCursor(0,0);
lcd.write ("0.1ft or 0.03 mtr);
myfile.write ("0.01ft  or 0.03mtr")
myfile.close();
}

i have hundreds of these, can you help!

#1
##### Jan 18, 2013, 12:30 am
Quote
...im trying to convert feet to metres on a button press...

In what increments?  0.1 feet per button press?

#### phillmybuttons

#2
##### Jan 18, 2013, 12:41 am
yes, my void functions are 0.1,0.2,0.3 etcand the relevant conversion for metres,

im having difficulty with the converting stage.

so for example;

inch = 1;
feet = inch times 12
metre = 3.28 feet
//i have no idea if this idea would work, obviously implemeted correctly?

if (upbutton pressed == high){
count up 1
write to lcd
}

if (downbuttonpressed == high){
count down 1
write to lcd
}

if (enterbuttonpressed == high){
write to sd card the conversion

am i missing a really simple statement?

i also have a rtc through ic2 to write the datestamp

#### Jimmy60

#3
##### Jan 18, 2013, 01:32 am
meters = feet / 3.28

Have you noticed any consistent mathematical relationship as you increment through the values?

#### phillmybuttons

#4
##### Jan 18, 2013, 02:07 am
but how does that count up?
metre =feet /3.28
but..

how do i use that statement to produce results as well as convert between the two in a button press? i thought seeing as its climbing through an array of sorts, i would need seperate lcd prints and sd writes?

i currently have,
turn on,
feet or metre //buttonpress
count up and show on lcd //buttonpress
count down and show on lcd //buttonpress
press enter-save to sd and show on lcd //buttonpress

id like a variable to be able to use anywhere like

int magical = 0;
magical = count;
count = inch;
inch = 1.00;
feet = inch / 12;
metre / feet/3.28

**insert magical conversion code here**

count up in metres or feet

ive been on this for ages and cannot find a soluton within my knowledge and heavy googling just helped confuse me more on bits and thisnumber++thatnumber,++this number?!?!?!?

im not an ignorant person and i love a challenge but i can not figure this out!

#5
##### Jan 18, 2013, 02:18 am
...counting from 0.0ft to 10.6 ft ...
//example
switch (count){  //my generic switch code, counts up with button press

When count = 0, feet = 0.  When count = 1, feet = 0.1.  When count = 2, feet = 0.2.  Time for our first expression...

Code: [Select]
`float feet;void loop( void ){...  feet = count / 10.0;...}`

Whenever you change count, recalculate feet using the expression above.

#6
##### Jan 18, 2013, 02:21 am

Next, calculcate meters...

Code: [Select]
`meters = feet / 3.28float meters;void loop( void ){...  meters = feet / 3.28;...}`

Because meters depends on feet (which depends on count) the expressions have to be performed in order...

Code: [Select]
`void loop( void ){...  // Update count when the button is pressed  feet = count / 10.0;  meters = feet / 3.28;...}`

#7
##### Jan 18, 2013, 02:24 am

At this point, you will need to format the output; create the string "0.1ft or 0.03 mtr" # where the "0.1" is replaced with feet converted to a string and "0.03" is meters converted to a string.

# Note: "mtr" is not the correct abbreviation for meter.

#### phillmybuttons

#8
##### Jan 18, 2013, 02:43 am
thanks for the detailed reply, i wish i understood it :/

so if i have a float, all i seem to be doing with the count number is dividing it?

and do i convert the "string" to an val? or dec, so lcd.write ("feet,VAL,metre,VAL1); ?

it seems i cant get around the math behind this? or the logic of how n it works,

i know this seems a bit of a pain especially since you gave such a detailed answer  but would you be able to explain it? briefly is fine, I just dont how dividing it so many times yields a conversion?
thankyou

its cool if you cant, it its late to be fair,thanks

#### Jimmy60

#9
##### Jan 18, 2013, 03:48 am

thanks for the detailed reply, i wish i understood it :/

Maybe it will make sense in the morning.

so if i have a float, all i seem to be doing with the count number is dividing it?

Yes. Each count increments by one. Dividing that by ten gives you feet. Meters = feet / 3.28

and do i convert the "string" to an val? or dec, so lcd.write ("feet,VAL,metre,VAL1); ?

No. Split your lcd.writes See the example in the code block below.

it seems i cant get around the math behind this? or the logic of how n it works,

i know this seems a bit of a pain especially since you gave such a detailed answer  but would you be able to explain it? briefly is fine, I just dont how dividing it so many times yields a conversion?
thankyou

its cool if you cant, it its late to be fair,thanks

Code: [Select]
`// lcd example// clear the screen and position cursorlcd.write("Ft ");    // write the first partlcd.write(feet);     // the variable feet found with count / 10lcd.write(" M ");  lcd.write(meters);   // meters = feet / 3.28`

Hope it helps.

Go Up