Recieving 3 analog inputs, then calculating result

I posted this in the Syntax & Programs, forum...but I think it should be here instead.

I'm very new to the Ardruino, but I believe I'm starting to learn the code. I have two MXP4115 pressure sensors, and one slide potentiometer. I'd like to take these inputs, capture the elapsed time between each iteration, then run a calculation to get a result. Hopefully that makes sense. Does the following code look correct?

/* Taking input from 3 analog sensors, two MPX 4115 pressure sensors, and simple slide potentiometer, to calculate an estimated result, which uses elapsed time to get a rate */

const int analogpinP1 = 1; // Analog input at pin 1 is "P1"
const int analogpinP2 = 2; // Analog input at pin 2 is "P2"
const int analogpinP3 = 3; // Analog input at pin 3 is "P3"

void setup()
{
Serial.begin(9600); // initialize serial communications at 9600 bps:
}

void loop()
{
int adcvalue1 = analogRead(analogpinP1); // read the analog in value for P1:
long pressure1 = 950 + adcvalue1 / 8.5; //convert it to milli bars

int adcvalue2 = analogRead(analogpinP2); // read the analog in value for P2:
long pressure2 = 950 + adcvalue2 / 8.5; //convert it to milli bars

int adcvalue3 = analogRead(analogpinP3); // read the analog in value for P3:
long position = 12.3 + adcvalue3 / 0.5; //convert it to inches

time = micros();
elapsedtime = micros() - time;

long result = ((pressure1 + pressure2)^(1.0/2.0))/(position^2)*elapsedtime+7.2853; // calculate result
Serial.print("\t result = ");
Serial.print(result); // print result

delay(10);
}

Hey-

First off, I dont know if you have to do anything in your setup() with the analog pins, (like for digital you do pinMode(pin,mode)?) so I won't comment on that.

In your loop method, instead of redeclaring the variables every time, why not put them above setup()?

Ex:

const int analogpinP1 = 1;  // Analog input at pin 1 is "P1"
const int analogpinP2 = 2;  // Analog input at pin 2 is "P2"
const int analogpinP3 = 3;  // Analog input at pin 3 is "P3"
int adcvalue1;
int adcvalue2;
int adcvalue3;
long pressure1;
long pressure2;
long result;


void loop()
{
adcvalue1 = analogRead(analogpinP1);
....
}

Also, I believe you need to declare time before you use it. :stuck_out_tongue:

unsigned long time;

All the math you did, (such as converting + adding, and stuff) I'll let you handle. (Remember, "( )"s are your friend!)

Also- I can't remember off the top of my head what print("\t ") will do... You might consider doing this-

Serial.print("Result = ");
Serial.println(result); //Print result, then make a new line.

Other than that, everything looks fine, syntax wise.

The only thing that you HAVE to declare, would be the variable "time". Everything else will work, but my comment about declaring those ints and longs only once, and not in the loop(), is a nice habit, as loop() runs forever. :stuck_out_tongue:

Edit: Forgot, you also need to declare elapsedTime.

Thanks for the feedback...for time, what do you mean I need to define it? What I'm trying to do is measure the time between each iteration, then use that in the calculation.

So, if pressure 1 was X, and pressure 2 was Y, and the position was at Z for a certain amount of time, that results in an answer of T in/s.

Does that make sense?

/*  Taking input from 3 analog sensors, two MPX 4115 pressure sensors, and simple slide potentiometer, to calculate an estimated result, which uses elapsed time to get a rate */

const int analogpinP1 = 1;  // Analog input at pin 1 is "P1"
const int analogpinP2 = 2;  // Analog input at pin 2 is "P2"
const int analogpinP3 = 3;  // Analog input at pin 3 is "P3"
int adcvalue1;
int adcvalue2;
int adcvalue3;
long pressure1;
long pressure2;
long position;
long result;

void setup()
{
 Serial.begin(9600);   // initialize serial communications at 9600 bps:
}

void loop()
{
 int adcvalue1 = analogRead(analogpinP1);    // read the analog in value for P1:
 long pressure1 = (950 + adcvalue1) / 8.5;  //convert it to milli bars
   
 int adcvalue2 = analogRead(analogpinP2);    // read the analog in value for P2:
 long pressure2 = (950 + adcvalue2) / 8.5;  //convert it to milli bars
   
 int adcvalue3 = analogRead(analogpinP3);    // read the analog in value for P3:
 long position = (12.3 + adcvalue3) / 0.5;  //convert it to inches
 
 time = micros();
 elapsedtime = micros() - time;
 
long result = ((pressure1 + pressure2)^(1.0/2.0))/(position^2)*elapsedtime+7.2853;   // calculate result
 Serial.print("Result = ");
 Serial.print(result);       // print result

 delay(10);        
}

Thanks for the feedback...for time, what do you mean I need to define it?

Well in the code you posted, there is no variable declaration for "time".

time = micros();
elapsedtime = micros() - time;

As is, this code is meaningless. "elapsedtime" will always be either 4 or 8 since it the subtraction executes immediately after you store the current value of micros().

If you are trying to find the time between iterations, reverse the statements.

elapsedtime = micros() - time; // how long since the last time something was stored in time
time = micros();  // store the new value

Ok, that makes sense.

Thanks...I'll load the program and give it a shot.