Executing same set of instructions for two parallel lines

Hi,

I want to write a program wherein two push buttons (or more may be used) are being used to run same set of instructions for two parallel lines.
If push button 1 is pressed, it shall complete given set of instruction for line 1, meanwhile if push button 2 is pressed before completion of line 1 cycle, it shall automatically start executing set of instructions for line 2 after completion cycle 1 of push button 1 or vice-a-versa.

Please guide in writing program.

Two parallel lines of what?

More detail of what you want to do would help

How long do you anticipate that it will take to execute the instructions for each of what you refer to as line 1 and line 2 ? What should happen if the line 1 button is pressed whilst the line 1 instructions are running ? Does the duration depend on any external factors such as response from a Web page or sensor ? What is the likely maximum number of pushbuttons and sequences of actions ? Which Arduino board are you using ?

Have you written the code to execute a set of instructions depending on a button press ? If so, please post it here

More detail of what you want to do would help

How long do you anticipate that it will take to execute the instructions for each of what you refer to as line 1 and line 2 ?
It takes few second to complete one cycle.

What should happen if the line 1 button is pressed whilst the line 1 instructions are running ?
*It should state of cycle whether is it executing as “HIGH” or not as “LOW” and display voltage. *

Does the duration depend on any external factors such as response from a Web page or sensor ?
No external sensor or web page except charging of capacitor.

What is the likely maximum number of pushbuttons and sequences of actions ?
Max three i am currently envisaging in prototype. shall be moving to other platform once sucessful.

Which Arduino board are you using ?
Arduino Uno R3
Have you written the code to execute a set of instructions depending on a button press ? If so, please post it here
Please find code

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

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

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

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

int j;
int k;
int preCharge_Fire = 10;
int preCharge_Fire_Cycle = 3;
int postCharge_Fire = 10;
int postCharge_Fire_Cycle = 2;

int delayA = 10;
int delayB = 15;
int delayL = 100;        // Safety Delay

int voltage_offset = 0;
int preChargeV = 10;     // Pre-Charge Voltage
int postChargeV = 15;    // Post-Charge Voltage
int stayON1 = 55;         //stay UP
int stayON2 = 50;         //stay UP
int stayLow = 10;         //stay LOW

int pushButton1 = 4;
int pushButton2 = 5;
float capVolt = A1;

int LINE_1_RELAY = 10;
int LINE_2_RELAY = 12;
int DISCHARGE_RELAY_PIN = 9;
int CHARGE_RELAY_PIN = 11;

int stateLINE_1_RELAY = 0;
int stateLINE_2_RELAY = 0;
int stateCHARGE_RELAY_PIN = 0;
int stateDISCHARGE_RELAY_PIN = 0;

const unsigned long eventInterval = 15;
unsigned long previousTime = 0;


void setup()
{
  Serial.begin(9600);         //  opens serial port, sets data rate to 9600 bps
  lcd.begin(LCD_COLS, LCD_ROWS);


  pinMode(capVolt, INPUT);         //  set up A1 as input pin for Capacitor Voltage (Divider Circuit)
  pinMode(pushButton1, INPUT_PULLUP); // set up as Relays Activation button
  pinMode(pushButton2, INPUT_PULLUP); // set up as Relays Activation button

  pinMode(LINE_1_RELAY, OUTPUT);    // set up Relay (5)
  pinMode(LINE_2_RELAY, OUTPUT);    // set up Relay (7)
  pinMode(CHARGE_RELAY_PIN, OUTPUT);    // set up of CHARGE_RELAY (6)
  pinMode(DISCHARGE_RELAY_PIN, OUTPUT);    // set up of DISCHARGE_RELAY (4)
}

void loop() {
  unsigned long currentTime = millis();
  if (currentTime - previousTime >= eventInterval) {
    value = analogRead(capVolt);
    vin = value * (5. / 1023.); //  FORMULA USED TO CONVERT THE VOLTAGE
    vout = (vin * (1 + (R1 / R2)));
    { lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("O=");
      lcd.print(vout);
      lcd.print("V");
      lcd.setCursor(9, 0);
      lcd.print("I=");
      lcd.print(vin);
      lcd.print("V");
      previousTime = currentTime;
    }
  }

  int stateButton1 = digitalRead(pushButton1);   // read pushbutton state
  int stateButton2 = digitalRead(pushButton2);   // read pushbutton state

  if (stateButton1 == HIGH && stateButton2 == HIGH)        // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
  {
    lcd.setCursor(13, 1);
    lcd.print("LOW");
    digitalWrite(CHARGE_RELAY_PIN, LOW);
  }

  // Pre-Charge Cycle
  {
    for (j = 1; j < preCharge_Fire_Cycle; j = j + 1)
    {
      {
        while ((stateButton1 == LOW || stateButton2 == LOW) && vout < preChargeV)      // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
        { lcd.setCursor(0, 1);
          lcd.print("HIGH");
          digitalWrite(CHARGE_RELAY_PIN, HIGH);          // Charging Capacitor to PRECHARGE VOLTAGE
          vout = analogRead(capVolt) * (5. / 1023.) * (1 + (R1 / R2));
          if (stateButton1 == LOW && stateButton2 == HIGH && vout >= preChargeV && vout <= (preChargeV + 2))    // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
          {
            digitalWrite(CHARGE_RELAY_PIN, LOW);    // Switching off charging Relay
            delay(delayB);
            digitalWrite(LINE_1_RELAY, HIGH);       // Line 1 activated
            digitalWrite(LINE_2_RELAY, LOW);        // Line 2 deactivated
            delay(delayB);
            for (k = 1; k < preCharge_Fire; k = k + 1)
            {
              digitalWrite(DISCHARGE_RELAY_PIN, HIGH);
              delay(stayON2);
              digitalWrite(DISCHARGE_RELAY_PIN, LOW);
              delay(stayLow);
            }
            digitalWrite(stateButton1, LOW);      // Setting Push button "HIGH"
            digitalWrite(stateButton2, HIGH);     // Setting Push button "LOW"
            digitalWrite(LINE_1_RELAY, HIGH);     // Setting LINE_1_RELAY "HIGH"
            digitalWrite(LINE_2_RELAY, LOW);      // Setting LINE_1_RELAY "LOW"
          }
          if (stateButton2 == LOW && vout >= preChargeV && vout <= (preChargeV + 2))   // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
          {
            digitalWrite(CHARGE_RELAY_PIN, LOW);    // Switching off charging Relay
            delay(delayB);
            digitalWrite(LINE_2_RELAY, HIGH);       // Line 2 activated
            digitalWrite(LINE_1_RELAY, LOW);        // Line 1 deactivated
            for (k = 1; k < preCharge_Fire; k = k + 1)
            {
              digitalWrite(DISCHARGE_RELAY_PIN, HIGH);
              delay(stayON2);
              digitalWrite(DISCHARGE_RELAY_PIN, LOW);
              delay(stayLow);
            }
            digitalWrite(stateButton1, HIGH);     // Setting Push button "HIGH"
            digitalWrite(stateButton2, LOW);      // Setting Push button "LOW"
            digitalWrite(LINE_2_RELAY, HIGH);     // Setting LINE_1_RELAY "HIGH"
            digitalWrite(LINE_1_RELAY, LOW);      // Setting LINE_1_RELAY "LOW"
          }
        }
      }
      vout = analogRead(capVolt) * (5. / 1023.) * (1 + (R1 / R2));
    }
  }

  digitalWrite(LINE_2_RELAY, LOW);     // Setting LINE_1_RELAY "HIGH"
  digitalWrite(LINE_1_RELAY, LOW);      // Setting LINE_1_RELAY "LOW"
}

As far as, this program is working fine for single line and I am adding new line.

I am also attaching hand drawn skeatch.

Scan.pdf (116 KB)

Please show the instructions for a single line.

pankajsharma200:
As far as, this program is working fine for single line and I am adding new line.

I am also attaching hand drawn skeatch.

Your drawing shows your Arduino operating relays. Can you please identify the exact relays you are using? All but a very few relays will require a transistor to actually switch on the relay.
Paul

Please find sketch for single line

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

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

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

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

int j;
int k;
int preCharge_Fire = 10;
int preCharge_Fire_Cycle = 6;
int postCharge_Fire = 10;
int postCharge_Fire_Cycle = 2;

int delayA = 10;
int delayB = 15;
int delayL = 100;        // Safety Delay

float voltage_offset = 0.0;
float preChargeV = 60.0;     // Pre-Charge Voltage
float postChargeV = 130.0;    // Post-Charge Voltage
int stayON1 = 55;         //stay UP
int stayON2 = 50;         //stay UP
int stayLow = 10;         //stay LOW

int pushButton = 4;
float capVolt = A1;

int CHARGE_RELAY_PIN = 11;
int DISCHARGE_RELAY_PIN = 9;

void setup()
{
  Serial.begin(9600);         //  opens serial port, sets data rate to 9600 bps
  lcd.begin(LCD_COLS, LCD_ROWS);


  pinMode(capVolt, INPUT);         //  set up A1 as input pin for Capacitor Voltage (Divider Circuit)
  pinMode(pushButton, INPUT_PULLUP); // set up as Relays Activation button
    pinMode(CHARGE_RELAY_PIN, OUTPUT);    // set up of CHARGE_RELAY
    pinMode(DISCHARGE_RELAY_PIN, OUTPUT);    // set up of DISCHARGE_RELAY
  }

void loop() {

  // read the value at digital input

  value = analogRead(capVolt);
  vin = value * (5. / 1023.); //  FORMULA USED TO CONVERT THE VOLTAGE
  vout = (vin * (1 + (R1 / R2)));
  { lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("O=");
    lcd.print(vout);
    lcd.print("V");
    lcd.setCursor(9, 0);
    lcd.print("I=");
    lcd.print(vin);
    lcd.print("V");
    delay(delayA);
  }
  int stateButton = digitalRead(pushButton);   // read pushbutton state at analog input

  if (stateButton == HIGH)        // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
  {
    lcd.setCursor(13, 1);
    lcd.print("LOW");
    digitalWrite(CHARGE_RELAY_PIN, LOW);
  }

  // Pre-Charge Cycle
  {
    for (j = 1; j < preCharge_Fire_Cycle; j = j + 1)
    {
      {
        while (stateButton == LOW && vout < preChargeV)      // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
        { lcd.setCursor(0, 1);
          lcd.print("HIGH");
          digitalWrite(CHARGE_RELAY_PIN, HIGH);          // Charging Capacitor to PRECHARGE VOLTAGE
          
          vout = analogRead(capVolt) * (5. / 1023.) * (1 + (R1 / R2));
          
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("O=");
          lcd.print(vout);
          lcd.print("V");
          lcd.setCursor(9, 0);
          lcd.print("I=");
          lcd.print(vin);
          lcd.print("V");

         
          if (vout >= preChargeV && vout <= (preChargeV + 1))    // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
          {
            digitalWrite(CHARGE_RELAY_PIN, LOW);    // Switching off charging Relay
            delay(delayB);
            for (k = 1; k < preCharge_Fire; k = k + 1)
            {
              digitalWrite(DISCHARGE_RELAY_PIN, HIGH);
              delay(stayON2);
              digitalWrite(DISCHARGE_RELAY_PIN, LOW);
              delay(stayLow);
            }
            delay(delayA);
          }
        }
      }
      digitalWrite(stateButton, LOW);
      vout = analogRead(capVolt) * (5. / 1023.) * (1 + (R1 / R2));
    }
  }
}

Dear Paul,

I am using relay module (8 relay) hence no additional transister.
Regards

Your sketch for a single line uses 'while' loops and 'delay()'. There are two ways to respond to a second button while that sketch is running:

  1. Re-write the sketch as a state machine. That does everything in little steps and, between steps, the buttons can be checked.

  2. Put the two buttons on interrupt pins and use a "FALLING" interrupt to set a global flag that indicates that the button was pressed. When a line completes, check the flags to see if another line needs to run. The Arduino UNO has two external interrupt pins: 2 and 3.

Thanks for your responce

I tried incorporating ineterrupt for single line but not able to understand why is it not executing. If I am using void loop it is working fine at pin 2. please review and guide

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

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

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

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

int j;
int k;
int preCharge_Fire = 10;
int preCharge_Fire_Cycle = 6;
int postCharge_Fire = 10;
int postCharge_Fire_Cycle = 2;

int delayA = 30;
int delayB = 15;
int delayL = 100;        // Safety Delay

float voltage_offset = 0.0;
float preChargeV = 10.0;     // Pre-Charge Voltage
float postChargeV = 15.0;    // Post-Charge Voltage
int stayON2 = 50;         //stay UP
int stayLow = 10;         //stay LOW

int pushButton1 = 2;
float capVolt = A1;

const int DISCHARGE_RELAY_PIN = 9;
const int LINE_1_RELAY = 10;
const int CHARGE_RELAY_PIN = 11;

volatile int stateButton1 = 0;
int stateLINE_1_RELAY = 0;
int stateCHARGE_RELAY_PIN = 0;
int stateDISCHARGE_RELAY_PIN = 0;

void setup()
{
  Serial.begin(9600);         //  opens serial port, sets data rate to 9600 bps
  lcd.begin(LCD_COLS, LCD_ROWS);

  pinMode(capVolt, INPUT);         //  set up A1 as input pin for Capacitor Voltage (Divider Circuit)
  pinMode(pushButton1, INPUT_PULLUP); // set up as Relays Activation button
  pinMode(CHARGE_RELAY_PIN, OUTPUT);    // set up of CHARGE_RELAY
  pinMode(DISCHARGE_RELAY_PIN, OUTPUT);    // set up of DISCHARGE_RELAY
  pinMode(LINE_1_RELAY, OUTPUT);

  attachInterrupt(2, LINE_1, FALLING);
}

void loop() {
  // read the value at analog input
  value = analogRead(capVolt);
  vin = value * (5. / 1023.); //  FORMULA USED TO CONVERT THE VOLTAGE
  vout = (vin * (1 + (R1 / R2)));
  { lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("O=");
    lcd.print(vout);
    lcd.print("V");
    lcd.setCursor(9, 0);
    lcd.print("I=");
    lcd.print(vin);
    lcd.print("V");
    delay(delayA);

    int stateButton1 = digitalRead(pushButton1);

    if (stateButton1 == HIGH)        // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
    {
      lcd.setCursor(13, 1);
      lcd.print("LOW");
      delay(delayA);
      digitalWrite(CHARGE_RELAY_PIN, LOW);
    }
  }
}
void LINE_1()           //ISR function excutes when push button at pinD2 is pressed

// Pre-Charge Cycle
{
  stateButton1 = digitalRead(pushButton1);
  if (vout < preChargeV)
  {
    digitalWrite(CHARGE_RELAY_PIN, HIGH);          // Charging Capacitor to PRECHARGE VOLTAGE
    vout = analogRead(capVolt) * (5. / 1023.) * (1 + (R1 / R2));
  }
  while (vout >= preChargeV && vout <= (preChargeV + 1))    // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
  {
    digitalWrite(CHARGE_RELAY_PIN, LOW);    // Switching off charging Relay
    for (k = 1; k < preCharge_Fire; k = k + 1)
    {
      digitalWrite(DISCHARGE_RELAY_PIN, HIGH);
      delayMicroseconds(stayON2);
      digitalWrite(DISCHARGE_RELAY_PIN, LOW);
      delayMicroseconds(stayLow);
    }
  }
}
  attachInterrupt(0, LINE_1, FALLING);

See the reference for attachInterupt

for (k = 1; k < preCharge_Fire; k = k + 1)
    {
      digitalWrite(DISCHARGE_RELAY_PIN, HIGH);
      delayMicroseconds(stayON2);
      digitalWrite(DISCHARGE_RELAY_PIN, LOW);
      delayMicroseconds(stayLow);
    }

This isn’t in the spirit of keeping ISRs short.

And note the use of digitalPinToInterrupt.

You forgot that I said “use a “FALLING” interrupt to set a global flag that indicates that the button was pressed”. I tried to modify your sketch to do it right but I don’t have the hd44780 library so I could not compile it with the changes. Good luck.

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


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


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


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


int j;
int k;
int preCharge_Fire = 10;
int preCharge_Fire_Cycle = 6;
int postCharge_Fire = 10;
int postCharge_Fire_Cycle = 2;


int delayA = 30;
int delayB = 15;
int delayL = 100;        // Safety Delay


float voltage_offset = 0.0;
float preChargeV = 10.0;     // Pre-Charge Voltage
float postChargeV = 15.0;    // Post-Charge Voltage
int stayON2 = 50;         //stay UP
int stayLow = 10;         //stay LOW


const int Button1Pin = 2;
const int Button2Pin = 3;


boolean Button1Pressed;
boolean Button2Pressed;


const int capVoltPin = A1;


const int DISCHARGE_RELAY_PIN = 9;
const int LINE_1_RELAY = 10;
const int CHARGE_RELAY_PIN = 11;


int stateLINE_1_RELAY = 0;
int stateCHARGE_RELAY_PIN = 0;
int stateDISCHARGE_RELAY_PIN = 0;


void setup()
{
  Serial.begin(9600);         //  opens serial port, sets data rate to 9600 bps
  lcd.begin(LCD_COLS, LCD_ROWS);


  // pinMode(capVolt, INPUT);         //  set up A1 as input pin for Capacitor Voltage (Divider Circuit)


  pinMode(CHARGE_RELAY_PIN, OUTPUT);    // set up of CHARGE_RELAY
  pinMode(DISCHARGE_RELAY_PIN, OUTPUT);    // set up of DISCHARGE_RELAY
  pinMode(LINE_1_RELAY, OUTPUT);


  pinMode(Button1Pin, INPUT_PULLUP); // set up as Relays Activation button
  pinMode(Button2Pin, INPUT_PULLUP); // set up as Relays Activation button
  attachInterrupt(digitalPinToInterrupt(Button1Pin), ButtonOne, FALLING);
  attachInterrupt(digitalPinToInterrupt(Button2Pin), ButtonTwo, FALLING);
}


void ButtonOne()
{
  Button1Pressed = true;
}


void ButtonTwo()
{
  Button2Pressed = true;
}


void loop()
{
  if (Button1Pressed)
  {
    Button1Pressed = false; // Reset for next press
    LINE_1();
  }


  if (Button2Pressed)
  {
    Button2Pressed = false; // Reset for next press
    LINE_2();
  }
}


void LINE_1()
{
  // read the value at analog input
  value = analogRead(capVoltPin);
  vin = value * (5. / 1023.); //  FORMULA USED TO CONVERT THE VOLTAGE
  vout = (vin * (1 + (R1 / R2)));


  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("O=");
  lcd.print(vout);
  lcd.print("V");
  lcd.setCursor(9, 0);
  lcd.print("I=");
  lcd.print(vin);
  lcd.print("V");




  // Pre-Charge Cycle
  if (vout < preChargeV)
  {
    digitalWrite(CHARGE_RELAY_PIN, HIGH);          // Charging Capacitor to PRECHARGE VOLTAGE
    vout = analogRead(capVoltPin) * (5. / 1023.) * (1 + (R1 / R2));
  }


  while (vout >= preChargeV && vout <= (preChargeV + 1))
  {
    digitalWrite(CHARGE_RELAY_PIN, LOW);    // Switching off charging Relay
    for (k = 1; k < preCharge_Fire; k = k + 1)
    {
      digitalWrite(DISCHARGE_RELAY_PIN, HIGH);
      delayMicroseconds(stayON2);
      digitalWrite(DISCHARGE_RELAY_PIN, LOW);
      delayMicroseconds(stayLow);
    }
  }
}


void LINE_2()
{
}

Thanks John for your responce ,

I modified code as per your guidance and compiled it. It is not reading buttons. I didi necessary changes in relay operation sequence and attached below. Please guide

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

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


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


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


int j;
int k;
int preCharge_Fire = 10;
int preCharge_Fire_Cycle = 6;
int postCharge_Fire = 10;
int postCharge_Fire_Cycle = 2;


int delayA = 30;
int delayB = 15;
int delayL = 100;        // Safety Delay


float voltage_offset = 0.0;
float preChargeV = 10.0;     // Pre-Charge Voltage
float postChargeV = 15.0;    // Post-Charge Voltage
int stayON2 = 50;         //stay UP
int stayLow = 10;         //stay LOW


const int Button1Pin = 2;
const int Button2Pin = 3;


boolean Button1Pressed;
boolean Button2Pressed;


const int capVoltPin = A1;


const int DISCHARGE_RELAY_PIN = 9;
const int LINE_1_RELAY_PIN = 10;
const int CHARGE_RELAY_PIN = 11;
const int LINE_2_RELAY_PIN = 12;

int stateLINE_1_RELAY = 0;
int stateLINE_2_RELAY = 0;
int stateCHARGE_RELAY_PIN = 0;
int stateDISCHARGE_RELAY_PIN = 0;


void setup()
{
  Serial.begin(9600);         //  opens serial port, sets data rate to 9600 bps
  lcd.begin(LCD_COLS, LCD_ROWS);


  pinMode(capVoltPin, INPUT);         //  set up A1 as input pin for Capacitor Voltage (Divider Circuit)
  pinMode(CHARGE_RELAY_PIN, OUTPUT);    // set up of CHARGE_RELAY
  pinMode(DISCHARGE_RELAY_PIN, OUTPUT);    // set up of DISCHARGE_RELAY
  pinMode(LINE_1_RELAY_PIN, OUTPUT);
  pinMode(LINE_2_RELAY_PIN, OUTPUT);
  pinMode(Button1Pin, INPUT_PULLUP); // set up as Relays Activation button
  pinMode(Button2Pin, INPUT_PULLUP); // set up as Relays Activation button
  attachInterrupt(digitalPinToInterrupt(Button1Pin), ButtonOne, FALLING);
  attachInterrupt(digitalPinToInterrupt(Button2Pin), ButtonTwo, FALLING);
}


void ButtonOne()
{
  Button1Pressed = true;
}

void ButtonTwo()
{
  Button2Pressed = true;
}

void loop()
{
  // read the value at analog input

  value = analogRead(capVoltPin);
  vin = value * (5. / 1023.); //  FORMULA USED TO CONVERT THE VOLTAGE vout = (vin * (1 + (R1 / R2)));

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("O=");
  lcd.print(vout);
  lcd.print("V");
  lcd.setCursor(9, 0);
  lcd.print("I=");
  lcd.print(vin);
  lcd.print("V");

  if (Button1Pressed)
  {
    Button1Pressed = false; // Reset for next press LINE_1();
  }

  if (Button2Pressed)
  {
    Button2Pressed = false; // Reset for next press LINE_2();
  }
}


void LINE_1()
{
  
  // LINE 1 Pre-Charge Cycle
  
  Button1Pressed = digitalRead(Button1Pin);   // read pushbutton state at digital input

  {
    for (j = 1; j < preCharge_Fire_Cycle; j = j + 1)
    {
      {
        if (Button1Pressed = true && vout < preChargeV)      // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
        { lcd.setCursor(0, 1);
          lcd.print("HIGH");
          delay(delayA);
          digitalWrite(CHARGE_RELAY_PIN, HIGH);          // Charging Capacitor to PRECHARGE VOLTAGE
          stateCHARGE_RELAY_PIN = "HIGH";
          vout = analogRead(capVoltPin) * (5. / 1023.) * (1 + (R1 / R2));

          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("O=");
          lcd.print(vout);
          lcd.print("V");
          lcd.setCursor(9, 0);
          lcd.print("I=");
          lcd.print(vin);
          lcd.print("V");
          digitalWrite(LINE_1_RELAY_PIN, HIGH);
          stateLINE_1_RELAY = "HIGH";
          delay(delayA);

          while (vout >= preChargeV && vout <= (preChargeV + 1))    // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP
          {
            digitalWrite(CHARGE_RELAY_PIN, LOW);    // Switching off charging Relay
            stateCHARGE_RELAY_PIN = "LOW";
            delay(delayA);

            for (k = 1; k < preCharge_Fire; k = k + 1)
            {
              digitalWrite(DISCHARGE_RELAY_PIN, HIGH);
              stateDISCHARGE_RELAY_PIN = "HIGH";
              delay(stayON2);
              digitalWrite(DISCHARGE_RELAY_PIN, LOW);
              stateDISCHARGE_RELAY_PIN = "LOW";
              delay(stayLow);
            }
            delay(delayA);
          }
        }
      }
      vout = analogRead(capVoltPin) * (5. / 1023.) * (1 + (R1 / R2));
    }
  }
  delay(delayA);
  digitalWrite(LINE_1_RELAY_PIN, LOW);
  stateLINE_1_RELAY = "LOW";
}

void LINE_2()
{
}

Somewhere along the line you have accidentally commented out the calls to LINE_1 and LINE_2 that were in John's code.

I tried line by line to identify but wasn't able to identify.

Regards

From your code

const int Button1Pin = 2;

and

boolean Button1Pressed;

so Button1Pin is a pin number and Button1Pressed is true or false depending whether the button is pressed or not

Later in your code

  int Button1Pin = digitalRead(Button1Pressed);   // read pushbutton state at digital input

You declare a second variable named Button1Pin (why ?) and instead of reading the state of a pin you read the state of a boolean variable (why ?)

You probably meant

  Button1Pressed = digitalRead(Button1Pin);   // read pushbutton state at digital input

later you do

       if (Button1Pin = true && vout < preChargeV)      // "HIGH" is working as "LOW" and vice-a-versa with INPUT_PULLUP

but you should be testing the value of Button1Pressed

I stopped looking at that point

Thanks Bob and John for your guidance. I am new to Arduino and learning by mistakes.

I worked on code as per your guidance and is able to correct it. It is now working as per logic except the “For” loops defined in both interrut.

Inner most “For” loop might be working (not sure) because as charging reaches to desired defined voltage (10V), respective line relay operates and along with discharge relay operates (LED glows and dies).

It does not stays for the millisecond delays which have been defined.

Outer most “For” loop is not working entirly.

Please find compiled and tested code. Please guide me.

PS: it shall be noted that Arduino is recognising “false” as “Button pressed” and vice-a-versa.

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


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


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


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


int j;
int k;
int preCharge_Fire = 10;
int preCharge_Fire_Cycle = 6;
int postCharge_Fire = 10;
int postCharge_Fire_Cycle = 2;

float preChargeV = 10.0;     // Pre-Charge Voltage
float postChargeV = 15.0;    // Post-Charge Voltage
int stayON2 = 50;         //stay UP
int stayLow = 10;         //stay LOW

const int Button1Pin = 2;
const int Button2Pin = 3;

volatile boolean Button1Pressed;
volatile boolean Button2Pressed;

const int capVoltPin = A1;
const int DISCHARGE_RELAY_PIN = 9;
const int LINE_1_RELAY_PIN = 10;
const int CHARGE_RELAY_PIN = 11;
const int LINE_2_RELAY_PIN = 12;

void setup()
{
  Serial.begin(9600);         //  opens serial port, sets data rate to 9600 bps
  lcd.begin(LCD_COLS, LCD_ROWS);

  pinMode(capVoltPin, INPUT);         //  set up A1 as input pin for Capacitor Voltage (Divider Circuit)
  pinMode(CHARGE_RELAY_PIN, OUTPUT);    // set up of CHARGE_RELAY
  pinMode(DISCHARGE_RELAY_PIN, OUTPUT);    // set up of DISCHARGE_RELAY
  pinMode(LINE_1_RELAY_PIN, OUTPUT);
  pinMode(LINE_2_RELAY_PIN, OUTPUT);

  pinMode(Button1Pin, INPUT_PULLUP); // set up as Relays Activation button
  pinMode(Button2Pin, INPUT_PULLUP); // set up as Relays Activation button
  attachInterrupt(digitalPinToInterrupt(Button1Pin), ButtonOne, FALLING);
  attachInterrupt(digitalPinToInterrupt(Button2Pin), ButtonTwo, FALLING);
}

void ButtonOne()

{ Button1Pressed = digitalRead(Button1Pin);
  if (Button1Pressed ==  false)
  {
    for (j = 1; j < preCharge_Fire_Cycle; j = j + 1)
    {
      vout = analogRead(capVoltPin) * (5. / 1023.) * (1 + (R1 / R2));
      while (vout < preChargeV)
      {
        digitalWrite(CHARGE_RELAY_PIN, HIGH);          // Charging Capacitor to PRECHARGE VOLTAGE
        vout = analogRead(capVoltPin) * (5. / 1023.) * (1 + (R1 / R2));
        if (vout >= preChargeV && vout <= (preChargeV + 1) )
        {
          digitalWrite(CHARGE_RELAY_PIN, LOW);    // Switching off charging Relay
          digitalWrite(LINE_1_RELAY_PIN, HIGH);
          for (k = 1; k < preCharge_Fire; k = k + 1)
          {
            digitalWrite(DISCHARGE_RELAY_PIN, HIGH);
            delayMicroseconds(stayON2);
            digitalWrite(DISCHARGE_RELAY_PIN, LOW);
            delayMicroseconds(stayLow);
          }
        }
      }
    }
  }
digitalWrite(LINE_1_RELAY_PIN, LOW);
}

void ButtonTwo()
{ Button2Pressed = digitalRead(Button2Pin);
  if (Button2Pressed ==  false)
  {
    for (j = 1; j < preCharge_Fire_Cycle; j = j + 1)
    {
      vout = analogRead(capVoltPin) * (5. / 1023.) * (1 + (R1 / R2));
      while (vout < preChargeV)
      {
        digitalWrite(CHARGE_RELAY_PIN, HIGH);          // Charging Capacitor to PRECHARGE VOLTAGE
        vout = analogRead(capVoltPin) * (5. / 1023.) * (1 + (R1 / R2));
        if (vout >= preChargeV && vout <= (preChargeV + 1) )
        {
          digitalWrite(CHARGE_RELAY_PIN, LOW);    // Switching off charging Relay
          digitalWrite(LINE_2_RELAY_PIN, HIGH);
          for (k = 1; k < preCharge_Fire; k = k + 1)
          {
            digitalWrite(DISCHARGE_RELAY_PIN, HIGH);
            delayMicroseconds(stayON2);
            digitalWrite(DISCHARGE_RELAY_PIN, LOW);
            delayMicroseconds(stayLow);
          }
        }
      }
    }
   }
digitalWrite(LINE_2_RELAY_PIN, LOW);
}

void loop()
{
  // read the value at analog input
  value = analogRead(capVoltPin);
  vin = value * (5. / 1023.); //  FORMULA USED TO CONVERT THE VOLTAGE
  vout = (vin * (1 + (R1 / R2)));

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("O=");
  lcd.print(vout);
  lcd.print("V");
  lcd.setCursor(9, 0);
  lcd.print("I=");
  lcd.print(vin);
  lcd.print("V");

  if (Button1Pressed)
  {
    if (Button1Pressed = true) // Reset for next press LINE_1();
    {
      digitalWrite(CHARGE_RELAY_PIN, LOW);
      lcd.setCursor(7, 1);
      lcd.print("BaBaBa");
      delay(200);
    }
  }

  if (Button2Pressed)
  {
    if (Button2Pressed = true) // Reset for next press LINE_2();
    {
      digitalWrite(CHARGE_RELAY_PIN, LOW);
    }
    lcd.setCursor(0, 1);
    lcd.print("HaHaHa");
    delay(100);
  }
}
if (Button1Pressed ==  false)

Perhaps you meant if (Button1Pressed == LOW)

But…if (Button1Pressed = true) Oops