RC Circuit Simulating a Pacemaker

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;
    }
  }
}

How about a schematic to provide some context about what the software is doing?

Yes!! Here's a diagram of the circuit! Thank you very much :slight_smile:

Diagrama circuito rc.jpg

Okay, you have A0 and A1 connected
to ground so they do nothing!

I would suggest you do some reading on pacemakers, they are not that simple and have a lot more smarts then you are suggesting. How are you sensing the heart? I would suggest you have a qualified doctor helping you, a mistook can kill very quickly. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil

herbschwarz:
Okay, you have A0 and A1 connected
to ground so they do nothing!

My bad at drawing the circuit diagram, A0 is connected to the nod between the emitter of the first transistor and the capacitor, while A1 is conected to the nod between the emitter of the second transistor and the second resistance

Obviously not a medical application at all. :roll_eyes:

Suspect "XY problem". What are you actually proposing to do? :grinning:

eliastuzo10:
My bad at drawing the circuit diagram, A0 is connected to the nod between the emitter of the first transistor and the capacitor, while A1 is conected to the nod between the emitter of the second transistor and the second resistance

How about re-drawing it for us, to save the mental gymnastics?

Here it is! Thank you all for your help and advices! :slight_smile:

CIRCUITORC.jpg

Paul__B:
Obviously not a medical application at all. :roll_eyes:

Suspect "XY problem". What are you actually proposing to do? :grinning:

True!! I think I fell right into that one hehe...what I want is to be able to charge and discharge the capacitor at a constant rate of about 1 full cycle each 5 seconds.

Please correct the schematic.

Show the emitter of the left transistor, and the battery voltage.

Show where the Arduino ground is connected.

I'm not using a battery, but using pin 9 in Arduino as an analogic output (code line no. 3). The ground is one of the GND pins of Arduino. And I've added the emitter of the transistor as you told me. Thank you so much for your interest!

opa.jpg

CIRCUITORC.jpg

opa.jpg

eliastuzo10:
what I want is to be able to charge and discharge the capacitor at a constant rate of about 1 full cycle each 5 seconds.

Yes, but for what purpose?

You don't need the transistors for such low currents.

A single pin can charge and discharge a capacitor, with a series resistor to limit the current.

jremington:
You don't need the transistors for such low currents.

A single pin can charge and discharge a capacitor, with a series resistor to limit the current.

I think the OP has requested a constant current for both charge and discharge. Hasn't explained why.

Paul

I think the OP has requested a constant current

I don't, and the proposed circuit won't provide a constant current.