Ohhh I see! That would make sense. We never talked about that, and have only been doing trial and error with the capacitor value.
I think that's what I have been doing? I've been playing around with adjusting the number of pulses at a time, the width of the pulses, and the delay time between the pulses. After sending the pulses there is quite a long delay (over 100µs if I remember correctly), because the Arduino is reading the capacitor voltage and takes that long to do so. I feel like there is a more efficient way of going about doing this... maybe a 555 timer to generate steady pulses, and a differential amplifier to get a reading? I know if I just use a 555 timer for the pulses, the Arduino won't be able to read anything, because it can no longer read how it was with the capacitor!
Thanks again for all the input @Grumpy_Mike !
Edit:
Also, here is my code!
#define I_AM_DEBUGGING//comment to stop serial prints
//#define INVERTED_LOOP//comment to go standard... uncomment for if the signal to the loop is inverted, like when using a driving transistor
#define capPin A5
#define pulsePin A4
#define RED 4
#define GREEN 5
#define BLUE 6
long expectedSum=0; //running sum of 64 sums
long ignoreCount=0; //number of ignoreCounted sums
long ignoreAmount=120;//64;//number "ignoreCount" gets to before resetting again... for above threshold
long difference=0; //difference between sum and avgsum
unsigned long previousTime=0;
unsigned long ledUpdatePeriod=0;
int numberOfPulses = 10;//number of pulses at one time
int pulseDelay = 1;//microseconds
int pulseSeriesCount = 256;//for loop
int diff;
int multiplier = 1;//general multiplier
int originalMultiplier = 64;//original multiplier... seen as <<6
int noiseThreshold = 150;//threshold for difference seen as 'noise'
int minval=1023;
int maxval=0;
long unsigned int sum=0;
void setup() {
Serial.begin(9600);
pinMode(pulsePin, OUTPUT);
pinMode(capPin, INPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
RGBLED(0,0,0);
for (int i=RED;i<RED+3;i++){
digitalWrite(i, LOW);
delay(300);
digitalWrite(i, HIGH);
}
RGBLED(0,0,0);
delay(1000);
RGBLED(1,0,0);
#ifdef INVERTED_LOOP
digitalWrite(pulsePin, HIGH);
#else
digitalWrite(pulsePin, LOW);
#endif
}
void loop() {
for (int i=0; i<pulseSeriesCount; i++){
resetCapacitor();
applyPulses();
readCapacitor();
LEDLogic();
}
int seendifference = totaldifference();
int myledvalue = map(abs(seendifference)/multiplier,20,1000,0,255);
#ifdef I_AM_DEBUGGING
Serial.println("sum: "+String(sum)+" - myledvalue: "+String(myledvalue)+" - difference: "+String(difference/multiplier)+" - period: "+String(ledUpdatePeriod));
#endif
sum=0;
}
void RGBLED(int R, int G, int B){
digitalWrite(RED,!R);//drive low to light
digitalWrite(GREEN,!G);
digitalWrite(BLUE,!B);
}
void applyPulses(){
for (int i=0;i<numberOfPulses;i++) {
#ifndef INVERTED_LOOP
digitalWrite(pulsePin,HIGH); //take 3.5 uS
delayMicroseconds(pulseDelay);
digitalWrite(pulsePin,LOW); //take 3.5 uS
delayMicroseconds(pulseDelay);
#else
digitalWrite(pulsePin,LOW); //take 3.5 uS
delayMicroseconds(pulseDelay);
digitalWrite(pulsePin,HIGH); //take 3.5 uS
delayMicroseconds(pulseDelay);
#endif
}
}
void resetCapacitor(){
pinMode(capPin,OUTPUT);
digitalWrite(capPin,LOW);
delayMicroseconds(20);
pinMode(capPin,INPUT);
}
void readCapacitor(){
//read the charge of capacitor
int val = analogRead(capPin); //takes 13x8=104 microseconds
minval = min(val,minval);
maxval = max(val,maxval);
sum+=val;//running total
}
void LEDLogic(){
long unsigned int cTime=millis();
char buzState=0;
diff = difference/multiplier;
if (cTime<previousTime+10){
if (diff>0){
buzState=1;
}
else if(diff<0){
buzState=2;
}
}
if (cTime>previousTime+ledUpdatePeriod){
if (diff>0){
buzState=1;
}
else if (diff<0){
buzState=2;
previousTime=cTime;
}
}
if (ledUpdatePeriod>300){
buzState=0;
}
if (buzState==0){
RGBLED(1,0,0);
}
else if (buzState==1){
RGBLED(0,1,0);
}
else if (buzState==2){
RGBLED(0,0,1);
}
}
int totaldifference(){
//subtract minimum and maximum value to remove spikes
sum-=minval;
sum-=maxval;
if (expectedSum==0){
expectedSum=sum*originalMultiplier; //set expectedSum to expected value
}
long int avgsum=(expectedSum+100)/originalMultiplier; //originally 32
difference=sum-avgsum;
difference=difference*multiplier;
if (abs(difference)<noiseThreshold*multiplier){
expectedSum=expectedSum+sum-avgsum;
ignoreCount=0;
}
else {
ignoreCount++;
}
if (ignoreCount>ignoreAmount){
expectedSum=sum*originalMultiplier;
ignoreCount=0;
}
if (difference==0){
ledUpdatePeriod=1000000;
}
else {
ledUpdatePeriod=avgsum/(2*abs(difference));
}
}