I rewrote the code. its a little different than whats in the paper. I know its going to have to change to work in my project. as it is this isn't working on the scope or with serial display. I know its likely a simple problem I'm just missing.
thanks, red
// capacitor based TDS measurement
// pin 10 C+ -750K resistor---|----------|
// | |
// 3.3 nF cap 5.6 M to simulate Rx
// | Resistor
// pin 9 C- --750K resistor---| |
// |
// pin 8 EC -----------------------------|
int capPos = 10; //C+
int capNeg = 9; //C_
int EC = 8; //EC
unsigned int timesThruAverage; //times thru "still high while loop"
void setup(){
Serial.begin(9600);
}
void loop (){
timesThruAverage = getTdsAverage(); //goto measuring function
Serial.println (timesThruAverage); //display average times thru loop
delay(1000);
}
unsigned int getTdsAverage(){
unsigned int timesThru = 0;
unsigned int setDelay = 0;
unsigned int runningTotal = 0;
for(int i;i<100;i++){ //get a average of 100 times thru still high while loop
//charge cap
pinMode (capPos,OUTPUT);
pinMode (capNeg, OUTPUT);
pinMode (EC, INPUT);
digitalWrite (capPos, HIGH);
digitalWrite (capNeg, LOW);
delayMicroseconds(100);
//let cap drain thru Rx
pinMode (capPos,INPUT); //set C+ to input to keep voltage from grounding a discharging thru this output pin
pinMode (EC, OUTPUT);
digitalWrite (EC, LOW);
//test to see how many times still high loop is HIGH before it goes low
while (capPos == HIGH){ //still high loop
timesThru += 1;
delayMicroseconds (10);
}
setDelay = timesThru * 10; //set delays down the line so freq has even phases
//fully discharge cap
pinMode (capPos,OUTPUT);
pinMode (capNeg, OUTPUT);
pinMode (EC, INPUT);
digitalWrite (capPos, LOW);
digitalWrite (capNeg, LOW);
delayMicroseconds (setDelay);
//the unmeasured half of ac phase
pinMode (EC, OUTPUT);
digitalWrite (EC, HIGH);
delayMicroseconds(setDelay);
runningTotal += timesThru;
}
timesThru = runningTotal / 100;
return timesThru;
}