N83:
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?
More helpfully (I hope), here are a few comments and questions:
/* 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.
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).
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?
long result = ((pressure1 + pressure2)^(1.0/2.0))/(position^2)*elapsedtime+7.2853; // calculate result
```<COLOR color="darkgreen">Is "^" a valid operator? There is a [[power function](http://arduino.cc/en/Reference/Pow).
Why use "(1.0/2.0)" instead of "0.5"?
Better yet, why not use the [[square](http://arduino.cc/en/Reference/Sq) and [[square root](http://arduino.cc/en/Reference/Sqrt) functions?</COLOR>
Serial.print("\t result = ");
Serial.print(result); // print result
delay(10);
}
<COLOR color="darkgreen">As the [[delay](http://arduino.cc/en/Reference/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</COLOR>](http://arduino.cc/en/Reference/Delay)](http://arduino.cc/en/Reference/Sqrt)](http://arduino.cc/en/Reference/Sq)](http://arduino.cc/en/Reference/Pow)