Executing same set of instructions for two parallel lines

I corrected it but it doesn't resulted in anything. Attached is corrected 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 = 6;
int postCharge_Fire = 10;
int postCharge_Fire_Cycle = 2;

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;

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 ==  LOW)                 // Note:  Arduino is recognising "LOW" as "Button pressed" and vice-a-versa.
  {
    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 ==  LOW)             // Note:  Arduino is recognising "LOW" as "Button pressed" and vice-a-versa.
  {
    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");
  lcd.setCursor(13, 1);

  if (Button1Pressed)
  {
    if (Button1Pressed = HIGH) // Reset for next press LINE_1(). Note:  Arduino is recognising "LOW" as "Button pressed" and vice-a-versa.
    {
      digitalWrite(CHARGE_RELAY_PIN, LOW);
      lcd.setCursor(7, 1);
      lcd.print("BaBaBa");
      delay(200);
    }
  }

  if (Button2Pressed)
  {
    if (Button2Pressed = HIGH) // Reset for next press LINE_2().Note:  Arduino is recognising "LOW" as "Button pressed" and vice-a-versa.
    {
      digitalWrite(CHARGE_RELAY_PIN, LOW);
    }
    lcd.setCursor(0, 1);
    lcd.print("HaHaHa");
    delay(100);
  }
}
if (Button1Pressed = HIGH)

Wrong, unless you meant to unconditionally set Button1Pressed to HIGH, that is

You have WAY too much code in your Interrupt Service Routines. All they need to do is set a flag so all they should do is set a flag. You should then test the flags in loop() to see which 'line' (if any) should run.

void ButtonOne()
{ Button1Pressed = digitalRead(Button1Pin);
  if (Button1Pressed ==  LOW)                 // Note:  Arduino is recognising "LOW" as "Button pressed" and vice-a-versa.
  {
    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 ==  LOW)             // Note:  Arduino is recognising "LOW" as "Button pressed" and vice-a-versa.
  {
    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);
}

Should be:

void ButtonOne()
{
  Button1Pressed = true;
}

void ButtonTwo()
{
  Button2Pressed = true;
}

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