Go Down

Topic: Handling of formula (Read 847 times) previous topic - next topic

PyrusBoy

Hi, I have to put a section in loop to find the following :
Given that I know an i/p analogue voltage on A2,A3,A4, that corrisspond to Vin, V50 and VL.
Then the following needs to be calculated:
A.....VRL22 + VXL2  =  VL2  and rearranging, then,
B......VXL2 = VL2 - VRL2
Also from a vector diagram
C.....(V50 + VRL2) + VXL2   =  Vin2
Substituting  (1) into (2) then;

D.....VRL = Vin2 - V502 - VL2/ 2 x V50

E.....RL  =  VRL  X 50  /  V50   and
F.....XL  =  VXL  X 50  /  V50

I am lost now, I have thought that the following would sort me out but it doesn't seem to be correct:
  val1 = analogRead(val1);              //Vin  reads analog input A2 into val
  val2 = analogRead(val2);              //V50  reads analog input A3 into val
  val3 = analogRead(val3);              //VL   reads analog input A4 into Val
  val4 = sq(val1);               //Vin squared
  val5 = sq(val2);               //V50 squared
  val6 = sq(val3);               //VL squared
  val7 = (2 * val2);                  //2 x V50
  val8 = (val4 - val6 - val7) / val7; //value of RL
  val9 = sq(val8);                //VRL squared
  val10 = (val3 - val8);               //VXL squared
  val11 = sqrt(val10);                 //value of XL
  lcd.setCursor(11, 2);
  lcd.print(val11);

I am an old codger at 70 and this is driving bonkers, greyer and her indoors is looking up the divorce paper's,
It's taken me two hours to write this.  The rest of my program works fine and I find it helps keep the grey matter going.
Can some kind gent save my bacon.  Thanks

Hackscribble

#1
Jul 01, 2014, 05:39 pm Last Edit: Jul 01, 2014, 05:44 pm by Hackscribble Reason: 1
Hello PyrusBoy

Although you don't say so, I assume the problem is that the value displayed on the LCD is not the answer you expect, given the input voltages?

Couple of ideas, and a request.

You could add print statements to your calculation code to print the intermediate results to serial monitor.  For example:

Code: [Select]
 val1 = analogRead(val1);              //Vin  reads analog input A2 into val
Serial.print("val1 = ");
Serial.println(val1);
 val2 = analogRead(val2);              //V50  reads analog input A3 into val
Serial.print("val2 = ");
Serial.println(val2);
etc


If any of the variables have been declared as type float or double, use the following to print them to 4 decimal places:

Code: [Select]
Serial.println(val11, 4);

You would need to include the following in the setup() function:

Code: [Select]
Serial.begin(9600); // make sure you have the same speed set in serial monitor

Second, what units do the three input voltages need to be in for your equations to work properly?  Volts?

Code: [Select]
 val1 = analogRead(val1);              //Vin  reads analog input A2 into val
 val2 = analogRead(val2);              //V50  reads analog input A3 into val
 val3 = analogRead(val3);              //VL   reads analog input A4 into Val


The value returned by analogRead() will be an integer between 0 and 1023, where 0 equates to 0V input and 1023 equates to 5V input (unless you have reconfigured the voltage reference).  

If you are expecting volts, you will need to do some arithmetic on val1 etc before you use them further.

The request is, please post your complete program.  Use code tags (the "#" button above the row of smileys).  Often, problems originate in other places in the program, for example where you declare these variables.

Regards

Ray



Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

PyrusBoy

Hi Hackscribble
I have tried to upload the whole program but it says it is over the number of characters allowed. It is 266 lines long. Any other method you can suggest?
The input is in volts 0 -5v
Thanks for your help.

Hackscribble

There is an "additional options" button below the text box where you type a post. You can attach a file that way.
Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

el_supremo

If you have declared all the variables as integer, this statement will cause problems:
Code: [Select]
val8 = (val4 - val6 - val7) / val7
The division will be done using integer arithmetic, which throws away the fractional part of the result. e.g. (10-3-4)/4 will be zero.
If that's the case you can start by declaring them to be float.

Pete

PyrusBoy

Hi,
I'll try it.

Hackscribble

Some of the analog reads in other parts of the code are multiplied by 5 and divided by either 1023 or 511.5 - I guess the difference is down to the circuit the Arduino is reading?

You will need similar scaling for val1, val2 and val3.
Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

PyrusBoy

Hi,
The divide by 511.5 is simply to indicate supply voltage, in case batteries start to fail, it is fed by a  divide by two voltage divider to keep the input to the arduino below 5 v        .  divide by 1023 was just to get something returned on the serial print.
Thanks

el_supremo

Code: [Select]
  val1 = analogRead(val1);              //Vin  reads analog input A2 into val
  val2 = analogRead(val2);              //V50  reads analog input A3 into val
  val3 = analogRead(val3);              //VL   reads analog input A4 into Val


A few comments about the code.
First a bug. Those statements (above) do not read the pins you mention in the comments. You need:
Code: [Select]
  val1 = analogRead(A2);              //Vin  reads analog input A2 into val
  val2 = analogRead(A3);              //V50  reads analog input A3 into val
  val3 = analogRead(A4);              //VL   reads analog input A4 into Val


Second, it would make your code a lot more readable if you used meaningful names for the variables. e.g. use Vin instead of val1, etc.
Third, you don't have to build the equation in pieces. For example equation D can be coded as:
Vrl = Vin*Vin - V50*V50 - VL*VL/ 2 * V50;

Pete

Go Up