Pages: [1]   Go Down
Author Topic: converting feet to metres with decimal points  (Read 746 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.(stat.txt,READ_WRITE);
myfile.write ("0.01ft  or 0.03mtr")
myfile.close();
}

i have hundreds of these, can you help! smiley
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12940
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
...im trying to convert feet to metres on a button press...

In what increments?  0.1 feet per button press?
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey, thanks for the reply,
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




Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

meters = feet / 3.28

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

Offline Offline
Newbie
*
Karma: 1
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!


Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12940
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

...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:
float feet;

void loop( void )
{
...
  feet = count / 10.0;
...
}

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12940
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Next, calculcate meters...

Code:
meters = feet / 3.28

float 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:
void loop( void )
{
...
  // Update count when the button is pressed
  feet = count / 10.0;
  meters = feet / 3.28;
...
}
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12940
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Offline Offline
Newbie
*
Karma: 1
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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



Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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


Code:
// lcd example
// clear the screen and position cursor
lcd.write("Ft ");    // write the first part
lcd.write(feet);     // the variable feet found with count / 10
lcd.write(" M "); 
lcd.write(meters);   // meters = feet / 3.28

Hope it helps.
Logged

Pages: [1]   Go Up
Jump to: