Measuring time constant with millis()

Hi everyone, I have a problem : I have to measure the time constant of RC circuit. In the theory, time period between Vmin and Vmax is equal 5T , T=RC is the time constant. So, I try to measure time between Vmax and Vmin, but I can't understand why it not work. Anyone please help me!
My code here:

float Vmax=0;
int   wait_time=5000;
float Vmin=0.2;//Vin
float preval=0;
long  period,current,t;
float V, Vmin_val,Vmax_val,Vnew;
void setup() {
  Serial.begin(115200);
}

void loop() {
   t=millis();
  if(t>wait_time){      //to ensure that the voltage value (max,min) is stable
  analogReadResolution(12);
  V=(float)analogRead(A1)*3.3/4095;
  if(V<Vmin) Vmin=V;
  if(V>Vmax) Vmax=V;
   Serial.print("----Vmax = ");
   Serial.println(Vmax);
   Serial.print("----Vmin = ");
   Serial.println(Vmin);
   Serial.print("----V = ");
   Serial.println(V);

   Vnew=(float)analogRead(A1)*3.3/4095;
   Serial.print("----Vnew = ");
   Serial.println(Vnew);
   if  (Vnew==Vmin)  { 
   preval=millis();//start count
   if  (Vnew==Vmax)  { 
   current = millis(); 
   period =(current-preval)/5;
   preval=current;}
   }
   Serial.print("----period  = ");
   Serial.println(period);
 }
}

Circuit diagram?

It is simply R in series with C.

thanhhang1411110:
It is simply R in series with C.

Connected to what ?

Please indent your code correctly, that's important to be readable.

From what I can see the key piece of logic is (after formatting readably):

    if (Vnew==Vmin)
    {
      preval = millis();//start count
      if (Vnew==Vmax)
      {
 current = millis();
 period =(current-preval)/5;
 preval = current;
      }
    }

Which makes no sense - how can Vnew both be equal to Vmin and Vmax at the same time?
Why are you setting preval and then current to millis() without any intervening time passing?

I also note that analogResolution call belongs in setup(), not loop().

One thing that's definitely wrong is testing for equality between analogRead values. ADCs have error,
you cannot rely on repeatability to the least significant bit.

What you are trying to do, measure an RC time constant, requires

  1. triggering something to start charging or discharging the capacitor.
  2. waiting for the voltage to fall/rise past a threshold

Waiting in loop() should be done with an if clause - loop is always cycling round, so all you
need is an 'if' to detect the threshold is reached/passed. That can then call the function to
calculate and report the results.

There needs to be some event to record the starting time/voltage and trigger the discharge.
I think thats what your wait_time thing is?

thanhhang1411110:
It is simply R in series with C.

That's too simple. Be precise.

I use a square wave to supply a RC circuit in order to modulate charge and discharge process of capacitor, I aim to know the value of R and C, so firstly I have to determine the time constant T of this circuit. The voltage I read in the program is the capacitor voltage.
I will try again with your suggestions. Thank you for your responses.

thanhhang1411110:
It is simply R in series with C.

Then it won't take you long to draw a diagram and share it with us! Do you want help?

This is the RC circuit that I said. In reality, I use R= 100kOhm, and C=2.2 uF, square wave frequency =1Hz.

RC_circuit.png

3a11e7ac63ef6692c29ffda25c6bdaefa044bab7.png

Thank you.
There does not seem to be anywhere on that circuit that connects the square wave to the Arduino, therefore I can't see how it would know that the capacitor has started charging.

Rather than use a square wave I think it would make more sense to connect the resistor to a digital output, which would initially be at 0, then set it high and start timing from that point.

I use the square wave to supply the circuit and use Arduino to read the capacitor voltage value, this image is the signal I display in the oscilloscope. My program is working, thank for all your supports.