Hello! I'm trying to make an RC circuit on which the charge and discharge of the capacitor is cyclic, so it can later be used as a model for a pacemaker. With the code below, I use a control sistem to make the charge of the capacitor never be over 0.50V and used a state machine to create the cycle on which it charges and discharges at the same rate. I'm also using two transistors 2n2222 to work as switches to control the charge and discharge of the capacitor. However, in the serial monitor I can see how the first 1-3 charge/discharge cycles are normal. However on the 4th cycle the charging takes four times as much as the first 3 charging cycles, then cycles 3(discharge), and 4-5 cycle normally. The same repeats over and over. I don't know if anyone around here can help me out.
#define periodo1 500
int sensorPin = A0, sensorPin2 = A1, sensorValue2 = 0, sensorValue = 0, outputValue = 0, output, ecarga, edescarga;
const int analogOutPin = 9;
float y1, m1 = 0.019, m2 = 0.0048, x2 = 0, x1 = 0, u, e_1 = 0, u_1, Ki = 0.0478049748027266, Kp = 10.0630688194805;
double e, tiempoTranscurrido, Vref, Vref2, y2;
unsigned long time_1 = 0, tiempoAnterior, tiempoActual;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Vref = 0.5;
Vref2 = 0.1;
analogWrite(analogOutPin, 0);
delay(1000);
digitalWrite(13, HIGH);
ecarga = 1;
edescarga = 0;
}
void loop() {
// put your main code here, to run repeatedly:
tiempoActual = millis();
tiempoActual = tiempoActual / 1000;
tiempoTranscurrido = tiempoActual - tiempoAnterior;
if (millis() >= time_1 + periodo1) {
time_1 = periodo1;
if (y2 <= Vref && ecarga == 1 && edescarga == 0 || e > 0.009) {
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
sensorValue = analogRead(sensorPin);//se lee la palabra de conversión del voltaje en el capacitor
y2 = m2 * sensorValue; //se convierte a voltaje la palabra de conversión
sensorValue2 = analogRead(sensorPin2);
y1 = m2 * sensorValue2;
e = Vref - y2;//se calcula el error
u = (Kp + Ki) * e - Kp * e_1 + u_1; //ley de control
output = u / m1;//se convierte voltaje a palabra de conversion
analogWrite(analogOutPin, output);//se escribe en el pin de saluda
Serial.print(Vref);
Serial.print("\t");
Serial.print(y2);
Serial.print("\t");
Serial.print(y1);
Serial.print("\t");
Serial.println(tiempoTranscurrido);
e_1 = e;
u_1 = u;
delay(50);
ecarga = 1;
edescarga = 0;
}
if (y2 < Vref && ecarga == 0 && edescarga == 1 || e < 0.009) {
sensorValue = analogRead(sensorPin);//se lee la palabra de conversión del voltaje en el capacitor
y2 = m2 * sensorValue; //se convierte a voltaje la palabra de conversión
sensorValue2 = analogRead(sensorPin2);
y1 = m2 * sensorValue2;
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
Serial.print(Vref);
Serial.print("\t");
Serial.print(y2);
Serial.print("\t");
Serial.print(y1);
Serial.print("\t");
Serial.println(tiempoTranscurrido);
e_1 = e;
u_1 = u;
delay(50);
ecarga = 0;
edescarga = 1;
}
if (y2 <= 0.10 && ecarga == 0 && edescarga == 1) {
sensorValue = analogRead(sensorPin);//se lee la palabra de conversión del voltaje en el capacitor
y2 = m2 * sensorValue; //se convierte a voltaje la palabra de conversión
sensorValue2 = analogRead(sensorPin2);
y1 = m2 * sensorValue2;
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
Serial.print(Vref);
Serial.print("\t");
Serial.print(y2);
Serial.print("\t");
Serial.print(y1);
Serial.print("\t");
Serial.println(tiempoTranscurrido);
e_1 = e;
u_1 = u;
delay(50);
ecarga = 1;
edescarga = 0;
}
}
}