Air Flow Equation

Friends I'm using an arduino UNO, to adapt an air flow sensor to a certain standard, the conversion formula is: y = (a+bx)/(1+cx+dx2), at where:

a = 0.25299904
b = 1.4363284
c = 0.23501546
d = -0.026938141

With this I would have approximately the following answer:

X=Vin Y=Vout
0 1
1 1.5
2 2
3 3.5
4 3.75
5 5

I used the code:

float A = 0.25299904;
float B = 1.4363284;
float C = 0.23501546;
float D = -0.026938141;
void setup() {
pinMode(3,OUTPUT);
}
void loop() {
float Vin= analogRead(A1);
float Vout = (A+(BVin))/(1+(C Vin)+(D*pow(Vin,2))); // function y=(a+bx)/(1+cx+dx2)
analogWrite(3, Vout /4);
}

But not run, could anyone tell me what my mistake is?

glmcosta:
But not run

That's not a problem description... Do you get an error? Or do you get the wrong answer? And how do you know because all I see you do is an analogWrite()...

Please define "not run". What happens?

A better, safer and faster way to write this:

float Vout = (A+(B*Vin))/(1+(C* Vin)+(D*pow(Vin,2)));  // function y=(a+bx)/(1+cx+dx2)

is this:

float Vout = (A+(B*Vin))/(1+(C* Vin)+(D*Vin*Vin));  // function y=(a+bx)/(1+cx+dx2)

Analog read gives you a value between 0 and 1023, corresponding to a voltage between 0 and 5 (assuming VRef is 5v). If your equation assumes that Vin is a voltage, you'll need to convert it

float Vin= analogRead(A1) / 1023.0 * 5.0;

The pow function should never be used for simple squaring, its way slower and less accurate (unless
you are lucky and the implementation is smart for integer powers).

You need scaling on analogWrite() as well.

I've tried everything but it does not work, I believe the problem is in the simulator I'm using, "https://circuits.io", I'm going to set up the circuit and test it.

Fist, you still haven't told us what is actually wrong?

If you tried "everything" what may that be?

And yeah, simulators suck, buy an Arduino. They are cheap enough.

I tried in Arduino, and the answer was the same as the simulator, something happens that does not allow the “Vout”, save the expression: Vout = (A+(BVin))/(1+(C Vin)+(Dpow(Vin,2))); or (A+(BVin))/(1+(C* Vin)+(DVinVin))

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

Instead of trying to work it all out one one line, break the equation down into sections and work them out, then combine.
That way you can check your results and find where the bugs are.

Tom.... :slight_smile:

Hi,
What values are you expecting to get out of analogWrite?
You can only have 0 to 255 integer.

Where did you get the equation and what are the units of X and Y.

Tom.. :slight_smile: