# dimensional analysis

Hello everyone: How do I write code that will do dimensional analysis of weights? I am practicing inputing and reading data over the serial port, so I thought it would be neat to display a weight in pounds, kilograms, grams, microgram, milligram etc.

I can not seem to find a guide on the necessary code, just a lot of stuff about using a load cell to measure weight.

I have no coding experience at all, so I am trying to create little "exercises".

Thanks.

Show us what you have so far. Be sure to read Nick Gammon's post at the top of this Forum on the proper was to post source code using code tags.

I essentially would like to return weight answers in more than one unit. Height, too.

``````String Name;
String Sex;
int Age;
float Height;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
Serial.println ("Hello Earthling, I have some questions.");
Serial.println("");
delay(2000);

// put your main code here, to run repeatedly:
Serial.println("What is your name, fleshling?"); // prompt user name.
while (Serial.available()==0){}                      // wait for user input.

Serial.println("");

Serial.println("Are you a boy human or a girl human?"); //Prompt user sex.
while (Serial.available()==0){}                        //Wait for user sex.
Serial.println("");

Serial.println("How many of your moons are you?");    //prompt user age.
while(Serial.available()==0){}
Age = Serial.parseInt();
Serial.println("");

Serial.println("How tall are you?");
while(Serial.available()==0){}
Height = Serial.parseFloat();
Serial.println("");

Serial.print("Greetings, ");
Serial.println (Name);
Serial.print("You have the genitals of a ");
Serial.print(Sex);
Serial.println(" human.");
Serial.print("You are ");
Serial.print(Age);
Serial.println(" moons old,");
Serial.print("and you are ");
Serial.print(Height);
Serial.println(" feet tall.");
delay(2000);
Serial.println("It has been nice getting to know you.");
Serial.println("");
delay(2000);
``````

You don't need dimensional analysis for this, just some conversions. For those, you just create some conversion constants like

``````#define CENTIMETERSPERINCH (2.54)
``````

then when you have a length in inches, you can write:

``````Serial.print(height*CENTIMETERSPERINCH);
Serial.println(" cm.");
``````

aarg:
You don't need dimensional analysis for this, just some conversions. For those, you just create some conversion constants like

``````#define CENTIMETERSPERINCH (2.54)
``````

then when you have a length in inches, you can write:

``````Serial.print(height*CENTIMETERSPERINCH);
``````

Serial.println(" cm.");

Does #define CENTIMETERSPERINCH (2.54); get declared as a global variable, or does it go into the relative clause in the void loop? Sorry if this seems an elementary question. Thanks for the help.

timberbite:
Does #define CENTIMETERSPERINCH (2.54); get declared as a global variable, or does it go into the relative clause in the void loop? Sorry if this seems an elementary question. Thanks for the help.

Place it before any code. An alternative that also works is:

``````const float CENTIMETERSPERINCH = 2.54;
``````

What you are doing is just math. The fact that the math is about units is not something that the processor or compiler care about. To them, you're just multiplying or dividing numbers. No special code needed, just multiply or divide like you would any other number.

Thanks everyone. I got it working.