I don't think that is writing right. because it doesn't work. I changed the circuit and code a bit. I hope someone can understand the help me fix the problem.
here is the new circuit and code. thanks, red
// capacitor based TDS measurement
// pin 10 C+ - 150 ohm resistor----------|------------|
// | |
// 3.3 nF cap 760 ohm to simulate Rx
// | Resistor
// pin 9 C- --------------------------------|
// |
// pin 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
DDRB = B110;
PORTB |= B100;
delayMicroseconds(50);
//let cap drain thru Rx
DDRB = B011;
PORTB = B000;
//test to see how many times still high loop is HIGH before it goes low
while (PINB & (1<<PINB2) == 1){ // while ill high loop on pin d10
timesThru += 1;
delayMicroseconds (10);
}
setDelay = timesThru * 10; //set delays down the line so feq has even phases
//fully discharge cap
DDRB = B110;
PORTB = B000;
delayMicroseconds (setDelay);
// the other side or the ac wave this side is unmeasured
DDRB = B111;
PORTB = B001;
delayMicroseconds(setDelay);
PORTB = B000;
runningTotal += timesThru;
}
timesThru = runningTotal / 100;
return timesThru;
}