tds code can u please check and correct this
code: [select]
#define cplus a0 // pin charge capacitor - connected 1 end of charging resistor
#define cminus 6 // pin discharge capacitor
#define ec 13
//#define capvalue 0.003545f // change whatever capacitor value in uf
#define capvalue 0.003f // change whatever capacitor value in uf
#define highthresh 682//648
#define lowthresh 341
double time_error_factor_plus=0;
double time_error_factor_minus=0;
unsigned long starttime;
unsigned long elapsedtimeplus;
unsigned long elapsedtimeminus;
long dischargetimeplus[10];
long dischargetimeminus[10];
double timesumplus;
double timesumminus;
double resplusvalue;
double resminusvalue;
double timeavgplus;
double timeavgminus;
double avgtds;
double tdsplus;
double tdsminus;
//
//float kiloohm; // floating point variable preserve precision, make calculations
//float ohm;
double electricconductance;
int val = 0; // variable store read value
double a[10];
double rminus[10];
int i=0;
double total=0;
double total_min=0;
double avg=0;
float avg1=0;
double rminavg;
double tds;
double x;
double y;
void setup()
{
pinmode(cplus,output);
pinmode(cminus,output);
pinmode(ec,output);
serial.begin(9600);
serial.println(" ........tds measurement........ "); // print units , carriage return
}
char * float2s(float f)
{
return float2s(f, 2);
}
char * float2s(float f, unsigned int digits)
{
int index = 0;
static char s[16]; // buffer build string representation
// handle sign
if (f < 0.0)
{
s[index++] = '-';
f = -f;
}
// handle infinite values
if (isinf(f))
{
strcpy(&s[index], "inf");
return s;
}
// handle not number
if (isnan(f))
{
strcpy(&s[index], "nan");
return s;
}
// max digits
if (digits > 6) digits = 6;
long multiplier = pow(10, digits); // fix int => long
int exponent = int(log10(f));
float g = f / pow(10, exponent);
if ((g < 1.0) && (g != 0.0))
{
g *= 10;
exponent--;
}
long whole = long(g); // single digit
long part = long((g-whole)*multiplier); // # digits
char format[16];
sprintf(format, "%%ld.%%0%dlde%%+d", digits);
sprintf(&s[index], format, whole, part, exponent);
return s;
}
// ************************************************************************
// main loop
// ************************************************************************
void loop()
{
// step 1: +ve charge cycle
digitalwrite(ec, low);
digitalwrite(cplus, high); // set chargepin high , capacitor charging
digitalwrite(cminus, low);
pinmode(ec, input); // set chargepin input
pinmode(cplus,output);
pinmode(cminus,output);
// delay(1);
//step 2: +ve mesurement cycle
digitalwrite(ec, low);
digitalwrite(cplus, high); // set chargepin high , capacitor charging
digitalwrite(cminus, low);
pinmode(cplus,input);
pinmode(ec, output); // set chargepin input
starttime = micros();
while(analogread(cplus) > lowthresh)
{
}
dischargetimeplus[i]= micros() - starttime;
// serial.print("dischargetimeplus[");
// serial.print(i);
// serial.print("]: ");
// serial.println(dischargetimeplus[i] );
//step 3 : discharge cap completely
pinmode(ec, input); // set chargepin input
digitalwrite(cplus, high); // set chargepin high , capacitor charging
digitalwrite(cminus, high);
pinmode(cplus, output); // set chargepin output
pinmode(cminus, output); // set chargepin output
// delay(1);
//step 4 : charge -ve
pinmode(ec, input); // set chargepin input
digitalwrite(cplus, low); // set chargepin high , capacitor charging
digitalwrite(cminus, high);
pinmode(cplus, output); // set chargepin output
pinmode(cminus, output); // set chargepin output
// delay(1);
//step 5 : measure time period
pinmode(cplus, input); // set chargepin output
digitalwrite(ec, high);
digitalwrite(cminus, low);
pinmode(ec, output); // set chargepin input
starttime = micros();
while(analogread(cplus) < highthresh)
{ // 682 2/3 of 1023, corresponds full-scale voltage
}
dischargetimeminus[i]= micros() - starttime;
// serial.print("dischargetimeminus[");
// serial.print(i);
// serial.print("]: ");
// serial.println(dischargetimeminus[i] );
//step 6
digitalwrite(cplus, low); // set chargepin high , capacitor charging
digitalwrite(cminus, low);
digitalwrite(ec, high);
pinmode(ec, input); // set chargepin input
pinmode(cplus, output); // set chargepin output
pinmode(cminus, output); // set chargepin output
// delay(1);
timesumplus = timesumplus + dischargetimeplus[i];
timesumminus = timesumminus + dischargetimeminus[i];
// serial.print("timesumplus: ");
// serial.println(timesumplus);
// serial.print("timesumminus: ");
// serial.println(timesumminus);
if(i==9)
{
timeavgplus = (timesumplus / 10) - time_error_factor_plus;
serial.print("timeavgplus: ");
serial.println(timeavgplus);
// serial.print("error_correction_plus: ");
// serial.println(error_correction_plus);
resplusvalue = (timeavgplus / capvalue);
x=resplusvalue-10000;
serial.print("res+ : ");
serial.print(float2s(x,6));
serial.println(" ohms");
electricconductance=1/resplusvalue;
serial.print("ec+ : ");
serial.print(float2s(electricconductance, 2));
serial.println(" siemens");
tdsplus = (0.5* electricconductance*1000000);
serial.print("tds+ : ");
serial.print(float2s(tdsplus,2));
serial.println(" usiemens");
timeavgminus = (timesumminus / 10) - time_error_factor_minus;
serial.print("timeavgminus: ");
serial.println(timeavgminus);
resminusvalue = (timeavgminus /capvalue);
y=resminusvalue-10000;
serial.print("res- : ");
serial.print(float2s(y,6));
serial.println(" ohms");
electricconductance=1/resminusvalue;
serial.print("ec- : ");
serial.print(float2s(electricconductance, 2));
serial.println(" siemens");
tdsminus = (0.5* electricconductance*1000000);
serial.print("tds- : ");
serial.print(float2s(tdsminus,2));
serial.println(" usiemens/cm");
serial.println(" ");
timesumplus = 0;
timesumminus = 0;
avgtds = (tdsplus+tdsminus)/2;
serial.print("tds : ");
serial.print(float2s(avgtds,2));
serial.println(" usiemens/cm");
serial.println("------------------------");
i=0;
}
else
{
i++;
}
}
moderator edit: code tags. why difficult?
do notice code turned italic halfway through? that's why need put in code tags.
presumably there wrong makes think needs correcting?? need describe what's wrong. what's supposed do? what's doing?
presumably there wrong makes think needs correcting?? need describe what's wrong. what's supposed do? what's doing?
Arduino Forum > Using Arduino > Project Guidance > tds code can u please check and correct this
arduino
Comments
Post a Comment