horse power calculator

Looking for help writing a code to calculate and display a horse power calculation. This is the formula I need to use

(analog1*200) * (analog2"*80) /700 =???? analog1 is 0-5 VDC... analog2 is mVdc.

The display does not match the math. its over double what it should be.

I used the example sketch for "hello world" to wire lcd

I built two variable voltage circuits to control the analog inputs.

Here's my code. Please help, I'm new to coding but not new to electrical/electronic theory/practice

/*
Trying to calculate horse power and display on lcd

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
  int  voltPin = A1;
  int mvoltPin = A7;
  int c = 200;
  int d = 80;
  int e = 700;
  float voltValue = 0;
  float mvoltValue = 0;
  float sum = ((voltValue*c*mvoltValue*d)/e);
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Horse Power");
}


void loop()
{

  //(voltValue*c)*(mvoltValue*d)/e;
  
 // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0,1);
  // horse power calculation
voltValue = analogRead (voltPin);
mvoltValue = analogRead (mvoltPin);
  lcd.print (voltValue*c)*(mvoltValue*d)/e;
  delay(1700);

return;
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

lcd.print (voltValue*c)*(mvoltValue*d)/e;The compiler didn't like that line, did it?

Actually it didn't give me any errors

Then you posted the wrong program. Post the entire program, using code tags.

If you don't know what those are, read and follow the directions in the "How to use this forum" post.

LandonW:
Actually it didn't give me any errors

I do believe you may be correct, but it still isn't semantically correct.

I do believe you may be correct, but it still isn't semantically correct.

Hmmm...

I don't have access to an Arduino to test, but perhaps this line:

lcd.print (voltValue*c)*(mvoltValue*d)/e;

is equivalent to multiplying the return value from lcd.print() by something, and ignoring the result:

((mvoltValue*d)/e)*lcd.print (voltValue*c);

jremington:
Hmmm...

I don't have access to an Arduino to test, but perhaps this line:

lcd.print (voltValue*c)*(mvoltValue*d)/e;

is equivalent to multiplying the return value from lcd.print() by something, and ignoring the result:

((mvoltValue*d)/e)*lcd.print (voltValue*c);

Yup...that's what I read second time I looked at it.
Syntax correct, flakey semantics.

I changed to the

((mvoltValue*d)/e)lcd.print (voltValuec);

still no change

LandonW:
I changed to the

((mvoltValue*d)/e)lcd.print (voltValuec);

still no change

No surprise there.

LandonW:
I changed to the

((mvoltValue*d)/e)lcd.print (voltValuec);

still no change

Funnest thing I have read all year. :slight_smile: :slight_smile: :slight_smile:

You rearrange something that is wrong into something that is equivalent and are surprised that it is still wrong.

Now if you still don't understand it is wrong after all you have been told, then say what you don't understand about what you have been told.

any clue on how to make it calculate correctly??

The calculation is probably correct, it's just that you're not calculating what you think you are.lcd.print ((voltValue*c)*(mvoltValue*d)/e);

I've tried writing that way too. still doesn't add up like it should or like it does when done on a calculator

Throw in some float casts and see how things develop

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Hi,
Try this;

/*
  Trying to calculate horse power and display on lcd

  http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int  voltPin = A1;
int mvoltPin = A7;
float c = 200.0;
float d = 80.0;
float e = 700.0;
int voltValue;
int mvoltValue;
float sum;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Horse Power");
}


void loop()
{

  //(voltValue*c)*(mvoltValue*d)/e;

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  lcd.print("        ");
  lcd.setCursor(0, 1);
  // horse power calculation
  voltValue = analogRead (voltPin);
  mvoltValue = analogRead (mvoltPin);
  sum = (((float)voltValue * c * (float)mvoltValue * d) / e);
  lcd.print (sum);
  delay(1700);
}

It compiles but I haven't run it.

Do not forget your analog values mvoltValue and voltValue will be from 0 to 1023, not the voltage on the input pin.

Tom... :slight_smile:

I'm trying to make calculations off of two analog inputs and for some reason the lcd print is way off.

my equation in (x200)(y*80)/700=? x = 0-5vdc y = mVdc

horsePWR_calculator.ino (1.88 KB)

And how big might y get? What types are x and y, and what type are you placing the result in? An Arduino's 'int' can only be as large as 32,767. After that, things get "interesting". You may need to define things as 'long', or do some strategic type-casting in your formula.

For those playing on phones and such:

/* 
 *  im using the arduino mega..
Trying to calculate horse power and display on lcd and turn on LED's depending on 
final calculation.

the forumla that i need is (x*200)*(y*80)/700

x<=5VDC  y=mVdc (both are on analog inputs)

for example (4vdc*200)*(32mVdc*80) / 700 = 3000

i built two variable voltage circuits to control analog inputs.
im using 4vdc and 70mVdc. the math shows about 6400. 
however the lcd is showing over 170,000
   
 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
  int ledG = 28;
  int ledY = 30;
  int ledR = 32;
  int voltPin = A1;
  int mvoltPin = A7;
  int c = 200;
  int d = 80;
  int e = 700;
  float voltValue= 0;
  float mvoltValue= 0;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  
  // Print a message to the LCD.
    lcd.print("    Welcome");
  delay(5000);


}



void loop()
{
/*  This note section is a copy paste area only, 
 *   its for different codes that i've tried.
  //(voltValue*c)*(mvoltValue*d)/(e); 
  */
   // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  
  lcd.print("Horse Power"); 
 // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0,1);
  // horse power calculation
voltValue = analogRead (voltPin);
mvoltValue = analogRead (mvoltPin);


  lcd.print (voltValue*c)*(mvoltValue*d)/(e);

  delay(1700);


  pinMode (ledG, OUTPUT);
  pinMode (ledY, OUTPUT);
  pinMode (ledR, OUTPUT);
  
  
if 
  ( ((mvoltPin*d*voltPin*c)/e) >=2900);
  digitalWrite (ledG , HIGH);

if 
  ( ((mvoltPin*d*voltPin*c)/e) >=3050);
  digitalWrite (ledY , HIGH);

if
  ( ((mvoltPin*d*voltPin*c)/e) >=3150);
  digitalWrite (ledR , HIGH);
  

return;
}

analogRead() returns a value from 0-1023. You haven't converted x and y to the "real world" values that you are expecting them to be before doing your maths.

jaholmes:
And how big might y get? What types are x and y, and what type are you placing the result in? An Arduino's 'int' can only be as large as 32,767. After that, things get "interesting". You may need to define things as 'long', or do some strategic type-casting in your formula.

They are floats. It's right there in the code posted.

He's already asked this in horse power calculator - Programming Questions - Arduino Forum

Pete