counting the number of charge/discharge cycles over a period of time

I have an arduino UNO which is charging and discharging a capacitor, and outputting a reading of voltage and time, showing the capacitor charging up and then discharging.
I want to enable a counter to count each charge and discharge cycle, i.e. one full cycle will be when the capacitor fully charges up and then fully discharges.

This is the code i currently have:

#define analogPin      0          // analog pin for measuring capacitor voltage
#define chargePin      13         // pin to charge the capacitor - connected to one end of the charging resistor
#define dischargePin   11         // pin to discharge the capacitor
#define resistorValue  998000.0F   // change this to whatever resistor value you are using
                                 // F formatter tells compliler it's a floating point value

unsigned long Time;
unsigned long elapsedTime;
float microFarads;                // floating point variable to preserve precision, make calculations
float nanoFarads;

int chargeStateCounter = 0;
int chargeState = 0;
int lastChargeState = 0;

void setup(){
 pinMode(chargePin, OUTPUT);     // set chargePin to output
 digitalWrite(chargePin, LOW);  

 Serial.begin(9600);             // initialize serial transmission for debugging
}

void loop(){
 digitalWrite(chargePin, HIGH);  // set chargePin HIGH and capacitor charging
 
 chargeState = digitalRead(analogPin);
 if (chargeState != lastChargeState) {
   if (chargeState == HIGH) {
     chargeStateCounter++;
     Serial.println("charged");
     Serial.print("charge cycle: ");
     Serial.println(chargeStateCounter);}
     else{
       Serial.println("discharge");
     }
 }
 lastChargeState = chargeState;
     
     
 

 while(analogRead(analogPin) < 511){       // 647 is 63.2% of 1023, which corresponds to full-scale voltage 
  Time = millis();
  float voltage= ((analogRead(analogPin)/1023.0)*5.0);
 
Serial.print(Time);       // print the value to serial port
 Serial.print(" mS    ");  // print units and carriage 
 Serial.print(voltage);
 Serial.println( " V");
}
 

 /* dicharge the capacitor  */
 digitalWrite(chargePin, LOW);             // set charge pin to  LOW 
 pinMode(dischargePin, OUTPUT);            // set discharge pin to output 
 digitalWrite(dischargePin, LOW);          // set discharge pin LOW 
 while(analogRead(analogPin) > 0){
 Time = millis();
  float voltage= ((analogRead(analogPin)/1023.0)*5.0);
 
Serial.print(Time);       // print the value to serial port
 Serial.print(" mS    ");  // print units and carriage 
 Serial.print(voltage);
 Serial.println( " V");  // wait until capacitor is completely discharged
 }

 pinMode(dischargePin, INPUT);            // set discharge pin back to input
}

Would anyone be able to tell me where i am going wrong, and maybe point me in the right direction?
Thanks in advance!

Seanm1234:
I have an arduino UNO which is charging and discharging a capacitor, and outputting a reading of voltage and time, showing the capacitor charging up and then discharging.
I want to enable a counter to count each charge and discharge cycle, i.e. one full cycle will be when the capacitor fully charges up and then fully discharges.

Would anyone be able to tell me where i am going wrong, and maybe point me in the right direction?
Thanks in advance!

You should format your code using the “code” button (the one that looks like a scroll of paper with <> on top).

Secondly, you never said WHAT is not working. Without building your exact circuit and then running your code (which nobody is going to do), there is no way to see what’s wrong.

What do you WANT it to do and what is it ACTUALLY doing (or not doing)?

Apologies,

So i did run the code for the circuit for the capacitance meter test as per http://arduino.cc/en/Tutorial/CapacitanceMeter

currently, the serial monitor shows the time in milliseconds and then the voltage at each time. It also prints at the beginning " charged charge cycle:1"

I am trying to get the monitor to tell me when it is charged and discharged, and also count the number of cycles for maybe 100 cycles. I would like it to state charge cycle: 1, charge cycle:2,......., Charge cycle: 50, and so on each time the capacitor actually charges. which would be when voltage in A0 is 0V?