Arduino capacitor reformer

Hi guys

I've not played with my arduino ide for a while and forgotten a lot about programming it.

I am reforming some big electrolytic capacitors. I have down pat the voltage reading, the relay control and that sort of stuff, but what I cant remember how to do is make the arduino monitor stuff and act on it, in a sequence.

The sequence will be as follows:
Initialization will set pins 2 high and pin 4 low. This disables the charger, and puts the bleed resitor online. At the same time the shorting bar is lifted. That I have sorted.

First, a quick intro to reforming caps. Caps are reformed by staircasing their charge. You start at say 25v, charge them up to 25v, let them sit on 25v, and then discharge them. You then move up to 50v, and the process repeats. During the wait, if the voltage drops significantly, you fire up the charger again to top up the charge.

The main routine should call several subroutines in order, one after the other.

  1. charge the capacitors (at up to 450 volts) - there is a voltage divider calibrated to this voltage 450V = 4.50v so the arduino can read it. This is read on pin a0. During this process, the charger looks at pin a0 and compares the voltage with integer "stair" which is a voltage between 25 volts and integer maxV. When the voltage reaches (or slightly exceeds) the value of the integer stair, the charger shuts off.

  2. Wait - this routine waits for a set period of time, monitoring the voltage, and topping off the charge if it drops more than 15v below the integer stair. Wait is a long time, usually 15 minutes to half an hour. During this time the voltage has to be compared and topped off constantly. This is accomplished by energising the charger briefly to bring the voltage back up.

  3. Discharge.
    after the wait time, pin 4 is set low (and if the charger is on, pin 2 is set high first, to disable it), and the discharge resistor is put inline thanks to the relay on pin 4. This dumps the energy in a safe manner (using a screwdriver to short out the 24000mf 450v bank is NOT a good idea!). Once the voltage hits close to 0, pin 4 goes high, disconnecting the discharge resistor

These 3 repeat 4 times, then stair is increased by 25v, and the whole process begins again.
4. Saftey.
Once the rated voltage is reached, and step 1, 2, and 3 have finished for a fourth time, a saftey system activates. This A) pulls pin 4, and pin 2 and 3 high. This completely disconnects the charger from the circuit, pulls the capacitors down to 0v with the bleed resistor, and monitors the voltage. Once in a safe range (~5v or so), pin 5 goes high, which drops the shorting contactor in place. This keeps the caps at 0v. Pin 3 goes low, which changes the connector to the 12v battery that drives the charger from the charger to a "look at the arduino stoopid!" buzzer :slight_smile:

How would I do this, bearing in mind I have already worked out

A) how to drive the LCD
B) how to read a (quite lethal to an arduino, or a human) voltage on an analog pin

My code so far:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

float R1 = 100000.0;    // !! resistance of R1 !! (voltage divider);
float R2 = 4700.0;     // !! resistance of R2 !! (Voltage divider);
float maxV = 450; //sets the voltage rating of the caps
float stair = 25; //sets the step increment of voltage
float cycle = 4; //set the number of times for charge cycle at stairs current setting
float hold = 15; //set the hold time in minutes aka wait time until the
                 //arduino closes the dump resistor contacts.
float volts = 0;
float voltage = 0;
int chargepin = 2; //arduino pin for charger relay input;
int dischargepin = (4); //arduino pin to discharge resistor relay;
int dummybuzzer = (5); //arduino pin for the doofus buzzer;
int contactor = (3); //arduino pin for the shorting contactor;
int state = 0;
int timed = 0; // 100msec interval timer
int time = 0; // seconds for wait timer

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Cap Former V1.0");
  pinMode(chargepin,OUTPUT);
  pinMode(dischargepin,OUTPUT);
  pinMode(dummybuzzer,OUTPUT);
  pinMode(contactor,OUTPUT);
  digitalWrite(2,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(dummybuzzer,HIGH);
  digitalWrite(contactor,LOW);
 delay(2000);

}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  volts = analogRead(voltage);
  volts = (volts * 5) / 1024;
  volts = volts / (R2/(R1+R2))*10;
  lcd.setCursor(0,1);
  lcd.print("State ");
  if (state = 1) lcd.print("Charging");
  if (state = 2) lcd.print("Holding");
  if (state = 3) lcd.print("Discharging");
  if (state = 4) lcd.print("Finished!");
  if (state = 1) int charge ();
  if (state = 2) int wait (hold);
  if (state = 3) int discharge ();
  if (state = 4) int saftey ();
  delay (100);
}

int charge (){
do
{
digitalWrite(chargepin,LOW);
volts = analogRead(voltage);
  volts = (volts * 5) / 1024;
  volts = volts / (R2/(R1+R2))*10;
  lcd.setCursor(1,8);
  lcd.print(volts);
  lcd.print(" V");
 } 
 while (volts<stair);
digitalWrite(chargepin,HIGH);
}

int wait (){;
timed = 0;
time = 0;
do
{
  volts = analogRead(voltage);
  volts = (volts * 5) / 1024;
  volts = volts / (R2/(R1+R2))*10;
  lcd.setCursor(1,8);
  lcd.print(volts);
  lcd.print(" V");
  if (volts < stair - 15) digitalWrite(chargepin,LOW);
  if (volts > stair) digitalWrite(chargepin,HIGH);
  delay (100);
  time=time+100;
 if (time = 1000) timed=timed + 1;
 if (time = 1000) time =0;
}
while (timed < hold / 60);
}

int discharge (){
digitalWrite(dischargepin,HIGH);
do
{
  volts = analogRead(voltage);
  volts = (volts * 5) / 1024;
  volts = volts / (R2/(R1+R2))*10;
  lcd.setCursor(1,8);
  lcd.print(volts);
  lcd.print(" V");
  delay (100);
if (volts < 5) digitalWrite(dischargepin,HIGH);
}
while (volts < 5);
}

I am totally lost with the rest of it - subroutines would be a bright idea, and just step through main onces each finishes (charge, hold, discharge, saftey. Infact, when saftey is called, it can put the arduino to idle mode provided the relay board pins stay in their current state.)

Really all i need to know is how to follow the steps in sequence, waiting for each one to complete, before starting the next, and finishing at the end with an idle state. Kind of like a steeple chase horse race - the horses start at the starting gate, go around the track, jumping the barrier things, and ending up at the finish line. The jumps could be compared to our steps, the starting line void setup, and the finish line step 4, which is only called once the program has run its course.

Ah I have rediscovered "do while" and I think I have it. Revised code in first post :slight_smile:

  if (state = 1) int charge ();
  if (state = 2) int wait (hold);
  if (state = 3) int discharge ();
  if (state = 4) int saftey ();

isn't right. Are you expecting to call some functions rather than declare them?

Your code sounds like it can simply be linear and not need a state variable (or are you doing something else in the sketch at the same time?)

MarkT:

  if (state = 1) int charge ();

if (state = 2) int wait (hold);
  if (state = 3) int discharge ();
  if (state = 4) int saftey ();




isn't right. Are you expecting to call some functions rather than declare them?

Your code sounds like it can simply be linear and not need a state variable (or are you doing something else in the sketch at the same time?)

Not only that, but they should all be the equality operator ('==') not the assignment operator ('=').

I guess it could run in a linear fashion, but I dont want it to charge any higher than maxV - doing so could turn the capacitors into little bombs.

I have made stacks of revisions to my code since I posted (not updated in post 1) and am now ready to test. Of course I'll be operating the charger, bleeder etc manually to see if the arduino does as its told. At least with subroutines (functions) I can steal them for other projects. Even the voltage readout is now a function, handy for other electronics projects that utilize an arduino, and use a voltage divider and an optocoupler.

can you post the revised code,so that we can see what revisions have you made.Have you tested it??