const int PIN_FUENTE = 8;
const int PIN_SENSOR = A0 ;
// PARAMETROS FISICOS ( IMPORTANTE : Usar valores medidos )
const float R_REAL = 9850.0; // Ohmios
const float C_REAL = 0.000102; // Faradios (102 uF)
const float TAU = R_REAL * C_REAL ;
void setup () {
Serial . begin (115200) ; // Alta velocidad
pinMode ( PIN_FUENTE , OUTPUT ) ;
digitalWrite ( PIN_FUENTE , LOW ) ; // Descarga inicial
delay (1500) ;
Serial . println (" Tiempo (s),V_Real (V),V_Teorico (V),Error (V)") ;
digitalWrite ( PIN_FUENTE , HIGH ) ; // E s c a l n unitario (t=0)
}
void loop () {
static unsigned long t_start = micros () ;
unsigned long t_now = micros () ;
float t_sec = ( t_now - t_start ) / 1000000.0;
if ( t_sec <= (5 * TAU ) ) { // Muestrear 5 constantes de tiempo
// 1. ADQUISICION
float v_adc = analogRead ( PIN_SENSOR ) * (5.0 / 1023.0) ;
// 2. CALCULO INTEGRAL ( Solucion Analitica )
// V(t) = 5 * (1 - e^( -t/RC))
float v_math = 5.0 * (1.0 - exp ( - t_sec / TAU ) ) ;
// 3. TELEMETRIA
Serial . print ( t_sec , 4) ; Serial . print (",") ;
Serial . print ( v_adc , 3) ; Serial . print (",") ;
Serial . print ( v_math , 3) ; Serial . print (",") ;
Serial . println ( abs ( v_math - v_adc ) , 3) ;
delay (10) ; // Frecuencia de muestreo aprox 100
}
}