trilife:
Case in point, PaulRB was correct in his original answer to this post that arrays, classes and objects are beyond my current capabilities!! good eye Paul!
What I'm trying to do:
- I have several analog pressure sensors, reading different parts of my system
- they all have certain parameters I need to use to condition the signal: sample size for averaging, calibration hi/lo points, alarm conditions.
- some of these parameters are fixed, others will need to be saved into EEPROM when first installed (currently set during compile), but then stay put.
after these pressures have been read and conditioned with those parameters, they get displayed, or emailed.
Currently, within my "scope of experience", I have to run 4 segments of code in the main loop, do the conditioning once for every pressure transducer... This is working well, but I'm afraid that once I start calibrating and saving stuff into EEPROM, this code turns into spaghetti, since I will have to do everything 4 time (or more, as the project evolves).
This is why I was trying to set up a set of parameters associated with an analog Input (array, struct, tuple, whatever), which I can pass to a certain function for processing.
As I'm writing this, a potential solution comes to mind:
I can define one the Pressure Structure, declare 4 of them, rainWater, poolFilter, etc.
the function float readPressure(analogInput) will have a series of if statments, testing analogInput, and use the according data in the rainWater, poolFilter etc structures to return a conditioned reading.
This should work.
What I was trying to do though, was to pass the entire instance of Pressure say rainWater to the function and have it do the conditioning.
//Structures for Pressure sensors
byte rainTank = 7;
byte poolFilt = 8;
struct Pressure {
byte analogChannel;
float samples;
int calZero;
int cal200;
int volZero;
int vol200;
int unitAdjust;
};
//Variables and Constants for WATER TANK
Pressure rainWater = {rainTank, 100, 86, 289, 0, 25560, 1000};
//Variables and Constants for POOL FILTER
Pressure poolFilter = {poolFilt, 100, 50, 250, 0, 800, 1};
//Instance for the readPressure function
Pressure myConditioningStructure;
float pressureValue7 = 0; //Water Tank sensor
float oldValue7 = pressureValue7; //used to clean the screen
#define RAINAREA 128250 // Rain tank = 570 x 225cm = 128,250 cm2 = 12.825 m2
#define TANKMAX 190 //190cm height from sensor to lip of window that already should trigger an alarm,
//the overflow lies below that.
#define RAINSENSORHEIGHT 9 //height of sensor from bottom of tank. reference only
#define MAXVOLRAIN (RAINAREA * TANKMAX)
//Global Variables for Conductivity Sensor
float oldEC; //old value to refresh screen
//-------------------SETUP STARTS HERE-------------------------
void setup(void) {
Serial.begin(115200);
Serial.println(analogRead(rainWater.analogChannel));
}
//read Pressure
float readPressure(struct myConditioningStructure){
long int sensorValue7 = 0;
for (int sampleCount = 0; sampleCount < myConditioningStructure.samples; sampleCount++) {
sensorValue7 = sensorValue7 + analogRead(myAnalogChannel);
delay (1);
}
pressureValue7 = float(sensorValue7) / myConditioningStructure.samples;
//pressureValue7 = map(pressureValue7, Pressure.tankZero, Pressure.tank200, Pressure.volZero, Pressure.vol200);
pressureValue7 = pressureValue7 / myConditionindStructure.unitAdjust;
return pressureValue7;
}
In essence, how do I pass the data inside the rainWater instance Pressure to the myConditioningStructure?
Stumped!
Thanks for bearing with me guys. 40 years ago, I was doing assembler programming for the COP800. things have changed!!!
Guys, Ignore the above!! It must have been late last night. This morning, after several cups of coffee, I figured it out, I think. I declared an instance of myConditioningStructure and discovered some typos. It now compiles and returns data...
//Structures for Pressure sensors
byte rainTank = 7;
byte poolFilt = 8;
struct Pressure {
byte analogChannel;
float samples;
int calZero;
int cal200;
int volZero;
int vol200;
int unitAdjust;
};
//Variables and Constants for WATER TANK
Pressure rainWater = {rainTank, 100, 86, 289, 0, 25560, 1000};
//Variables and Constants for POOL FILTER
Pressure poolFilter = {poolFilt, 100, 50, 250, 0, 800, 1};
//
Pressure myConditioningStructure = {0,0,0,0,0,0,0};
float pressureValue7 = 0; //Water Tank sensor
float oldValue7 = pressureValue7; //used to clean the screen
#define RAINAREA 128250 // Rain tank = 570 x 225cm = 128,250 cm2 = 12.825 m2
#define TANKMAX 190 //190cm height from sensor to lip of window that already should trigger an alarm,
//the overflow lies below that.
#define RAINSENSORHEIGHT 9 //height of sensor from bottom of tank. reference only
#define MAXVOLRAIN (RAINAREA * TANKMAX)
//Global Variables for Conductivity Sensor
float oldEC; //old value to refresh screen
//-------------------SETUP STARTS HERE-------------------------
void setup(void) {
Serial.begin(115200);
Serial.println(analogRead(rainWater.analogChannel));
}
//-------------------LOOP STARTS HERE----------------------
void loop(void) {
Serial.print(rainWater.analogChannel);
Serial.print(" ");
Serial.println(analogRead(rainWater.analogChannel));
Serial.print(poolFilter.analogChannel);
Serial.print(" ");
Serial.println(analogRead(poolFilter.analogChannel));
//Read and convert Tank Pressure
Serial.print("from function");
Serial.print(rainWater.analogChannel);
Serial.print(" :");
Serial.println(readPressure(rainWater));
//Read and convert Tank Pressure
Serial.print("from function");
Serial.print(poolFilter.analogChannel);
Serial.print(" :");
Serial.println(readPressure(poolFilter));
// pressureValue7 = readPressure(rainWater);
delay(1000); //-----------------------Change to Millis()
}
//read Pressure
float readPressure(Pressure myConditioningStructure){
long int sensorValue7 = 0;
for (int sampleCount = 0; sampleCount < myConditioningStructure.samples; sampleCount++) {
sensorValue7 = sensorValue7 + analogRead(myConditioningStructure.analogChannel);
delay (1);
}
pressureValue7 = float(sensorValue7) / myConditioningStructure.samples;
pressureValue7 = map(pressureValue7, myConditioningStructure.calZero, myConditioningStructure.cal200, myConditioningStructure.volZero, myConditioningStructure.vol200);
pressureValue7 = constrain(pressureValue7, myConditioningStructure.volZero, myConditioningStructure.vol200);
pressureValue7 = pressureValue7 / myConditioningStructure.unitAdjust;
return pressureValue7;
}
thanks for your help. Case closed