Relay is not holding to charge

Able to solve it

Circuit.pdf (93.9 KB)

Please follow the advice on posting code given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

Without having a clue how your relays and capacitors are wired it's impossible to help you.

Please provide a schematic, hand drawn and photographed is fine. Please provide details of everything that you are using.

Thanks.

This is your post #15. Please use code tags. Your code should look like this.

Your example code

Have a look at the How to get the best out of this forum
Regarding your code

I would recommend you have a look at the following example to learn how to never use delay again.

File → Examples → 02.Digital → BlinkWithoutDelay

The use of delay is a bad idea because it wastes processor cycles that can be used for other things. It stops you from extending your sketch without breaking everything you build so far. Your Arduino is not human and does not need a break.

Next, it is time to learn new concepts like loops. You already got a long sketch, so you are ready. :slight_smile: If you write the same code lines many times you should use a loop. There are many examples all over the internet including this forum and the Arduino IDE.

Try the Auto Format tool in the Arduino IDE to format your code. You can adjust the formatting to your taste using a formatter.conf file.

Choose better names for variables. e.g., why is Relay6 for charging, chargeRelay and dischargeRelay would not need a comment after each code line where you use it.
You can also use constant naming scheme for pins e.g., CHARGE_RELAY_PIN, DISCHARGE_RELAY_PIN. You already make use of it in other parts of your code. Just keep it consistent.

Avoid comments. If you need a comment, see if you can change the code.

If you have comments they should add information and not raise questions. e.g.

digitalWrite(Relay6, HIGH);} // Charging Capacitor to PRECHARGE VOLTAGE

This is a single line comment and does not describe what is happening in this line. If it was before the block, then it might be useful.

OP’s code in code tags

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd;
#define LCD_CHEXECTIME 2000
#define LCD_INSEXECTIME 38

int vout = 0;   // Capacitor Voltage
float vin = 0.0;    // Arduino converted voltage
float value = 0.0;  // Arduino incoming voltage

const int R2 = 9390;      // Divider Circuit Resistance
const int32_t R1 = 1000000; // Divider Circuit Resistance

const int LCD_COLS = 16;
const int LCD_ROWS = 2;

int i = 0;
int delayA = 100;
int delayB = 0;
int delayL = 500;        // Safety Delay
int delayC = 20;          // Charging Relay delay
int preChargeV = 60;     // Pre-Charge Voltage
int postChargeV = 135;    // Post-Charge Voltage
int stayON1 = 55;         //stay UP
int stayON2 = 40;         //stay UP
int stayLow = 10;         //stay LOW

int pushButton = 4;
float capVolt = A1;
int Relay6 = 11;          // Charging Relay
int Relay7 = 12;          // Firing Relay
int stateRelay6 = 0;      // Charging Relay
int stateRelay7 = 0;      // Firing Relay
int stateButton;


void setup()
{
  Serial.begin(9600);         //  opens serial port, sets data rate to 9600 bps
  lcd.begin(LCD_COLS, LCD_ROWS);
  lcd.setCursor(0, 0);
  lcd.print("Code Alpha");
  delay(delayL);
  lcd.clear();

  pinMode(capVolt, INPUT);         //  set up A0 as input pin for Capacitor Voltage (Divider Circuit)
  pinMode(pushButton, INPUT_PULLUP); // set up as Relay Activation button
  pinMode(Relay6, OUTPUT);    // set up Relay6 as Charging Relay
  pinMode(Relay7, OUTPUT);    // set up Relay7 as Firing Relay

}

void loop() {

  // read the value at digital input

  value = analogRead(capVolt);
  vin = value * 0.00488;   //  FORMULA USED TO CONVERT THE VOLTAGE
  vout = vin * (1 + (R1 / R2));
  { lcd.setCursor(0, 0);
    lcd.print("O=");                   // prints the voltage value in the LCD display
    lcd.print(vout);
    lcd.print("V");
    lcd.setCursor(9, 0);
    lcd.print("I=");                   // prints the voltage value in the LCD display
    lcd.print(vin);
    lcd.print("V");
  }
  int stateButton = digitalRead(pushButton);   // read pushbutton state at analog input

  if (stateButton == HIGH)
  { lcd.setCursor(13, 1);
    lcd.print("LOW");
    digitalWrite(Relay6, LOW);
    digitalWrite(Relay7, LOW);
    delay(delayA);
    lcd.clear();
  }


  if (stateButton == LOW && vout < preChargeV)
  { lcd.setCursor(0, 1);
    lcd.print("HIGH");
    digitalWrite(Relay6, HIGH);
  }    // Charging Capacitor to PRECHARGE VOLTAGE


  if (stateButton == LOW && vout >= preChargeV && vout < (preChargeV + 1))

  {
    digitalWrite(Relay6, LOW);    // Switch off charging Relay
    delay(delayC);


    digitalWrite(Relay7, HIGH);   //1                Discharge capacitor
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //2
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //3
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //4
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //5
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //6
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //7
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //8
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //9
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
  }



  if (stateButton == LOW && vout < postChargeV)
  {
    digitalWrite(Relay6, HIGH); // Charging Capacitor to postChargeV
  }

  if (stateButton == LOW && vout >= postChargeV && vout < (postChargeV + 1) )
  {
    digitalWrite(Relay6, LOW);    // Switch off charging Relay
    delay(delayC);

    digitalWrite(Relay7, HIGH);   //1
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //2
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //3
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //4
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //5
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //6
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //7
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //8
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    digitalWrite(Relay7, HIGH);   //9
    delay(stayON2);
    digitalWrite(Relay7, LOW);
    delay(stayLow);
    lcd.clear();
    stateButton = HIGH;
    delay(delayL);
  }

}

Without schematic and proper explanation of what you WANT the relay to do other than “holding charge”, we can’t really help you that much. How does it hold charge? Is it active LOW or HIGH (most Arduino compatible relay modules I’ve worked with is active LOW)? What’s the sequence of event you’re looking for? What’s “not holding”? The arduino dies? The relay clicks back? The cap exploded? Third if statement runs? Back to first if statement? What’s “not holding”?

Also, you’re blocking Relay6 with a metric crap ton of delays. That might cause an issue.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.