SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By N83
#116549
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);
}
By esklar81
#116553
N83 wrote: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?
Why don't you ask the IDE? :wink:

More helpfully (I hope), here are a few comments and questions:
Code: Select all
/*  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"
So far, so good, but you haven't declared the rest of your variables. Even if you don't want to assign an initial value, you still need to allocate memory. Unless you have a particular reason for having a variable be local to a function, I encourage you to declare all the variables globally in the header.
Code: Select all
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
To avoid unintended consequences of automated selection of order of execution, you should use parentheses in the calculations. Even if you've entered them so that the default order is the order you want, it's much easier for us humans (some of whom you have asked for help) if you provide parentheses so we can tell if you meant (a+b)/c or a+(b/c).
Code: Select all
 time = micros();
 elapsedtime = micros() - time;
What time are you trying to measure? This appears to measure the time between adjacent instructions, which doesn't have any apparent physical meaning. I don't know what you meant by "the elapsed time between each iteration", as "between" requires two objects and you've provided only one. Did you mean between each iteration of the sequence (3 reads, calculate, write) and the next or between the readings of the two sensors, or something else?
Code: Select all
long result = ((pressure1 + pressure2)^(1.0/2.0))/(position^2)*elapsedtime+7.2853;   // calculate result
Is "^" a valid operator? There is a power function.
Why use "(1.0/2.0)" instead of "0.5"?
Better yet, why not use the square and square root functions?
Code: Select all
 Serial.print("\t result = ");
 Serial.print(result);       // print result
 delay(10);        
}
As the delay function's argument is in milliseconds, this is likely to result in a blur. The human eye can't distinguish images separated by less than about 70 milliseconds.

Have Fun,
Eric
By N83
#116556
Thanks for that feedback..thats a HUGE help.

What I'm trying to do with time is capture the amount of time elapsed between each calculation of "result". So, time elapsed between each iteration of the sequence. So, if pressure 1 was X, 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. This rate will change with each calculation of the "result", so the time taken to run through the sequence is needed. Hope that makes sense. What do you think?
Code: Select all
/*  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 time;
long elapsedtime;
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 = (Pow((pressure1 + pressure2),0.5))/((position^2)*elapsedtime)+7.2853;   // calculate result
 Serial.print("Result = ");
 Serial.print(result);       // print result

 delay(100);        
}