Multiple external EEPROMs

Hi,
I used 4 pcf8574 ics to sense the function and according to the sensed data it will give serial print and 4 leds on or off according to the output relevancy.
I want to get a reference when power is gone, So that I can remember the last state and that state conditions.
So I used external EEPROM (multiple) as a solution.
But after I wrote the code I didnt get any output serial value or led value during the power on. So I expect your help for this situation.
Many thanks in advance!!

#define ARDUINO_H
#include <PCF8574.h>
#include <Wire.h>

#define INTERRUPTED_PIN1 18


// Set i2c address
PCF8574 pcf8574_1(0x20);
#define PCF8574_1 0x20
#define EEPROM_I2C_ADDRESS0 0x20

PCF8574 pcf8574_2(0x24);
#define PCF8574_2 0X24


PCF8574 pcf8574_3(0x22);
#define PCF8574_3 0X22


PCF8574 pcf8574_4(0x26);
#define PCF8574_4 0X26



PCF8574::DigitalInput d1 = pcf8574_1.digitalReadAll();
int P1_array[8] = {d1.p0, d1.p1, d1.p2, d1.p3, d1.p4, d1.p5, d1.p6, d1.p7};

PCF8574::DigitalInput d2 = pcf8574_2.digitalReadAll();
int P2_array[8] = {d2.p0, d2.p1, d2.p2, d2.p3, d2.p4, d2.p5, d2.p6, d2.p7};

PCF8574::DigitalInput d3 = pcf8574_3.digitalReadAll();
int P3_array[8] = {d3.p0, d3.p1, d3.p2, d3.p3, d3.p4, d3.p5, d3.p6, d3.p7};

PCF8574::DigitalInput d4 = pcf8574_4.digitalReadAll();
int P4_array[8] = {d4.p0, d4.p1, d4.p2, d4.p3, d4.p4, d4.p5, d4.p6, d4.p7};


int OutPins[4] = {13, 12, 11, 10}; //led pin
int s;//Array element's parameter of OutPins[] array
char PCF_PINS[8] = {P0, P1, P2, P3, P4, P5, P6, P7};


// Function interrupt
volatile bool keyPressed1 = false;
volatile bool keyPressed2 = false;

void keyPressedOnPCF8574() {
  //  Serial.println("keyPressedOnPCF8574");
  if ( digitalRead(INTERRUPTED_PIN1) == HIGH) {
    keyPressed1 = true;
  } else {
    keyPressed2 = true;
  }
}

void writeAddress(int i2cAddress, int address, byte val)
{
  Wire.beginTransmission(i2cAddress);
  Wire.write((int)(address >> 8));   // MSB
  Wire.write((int)(address & 0xFF)); // LSB


  Wire.write(val);
  Wire.endTransmission();

  delay(5);
}

byte readAddress(byte i2cAddress, int address)
{
  byte rData = 0xFF;

  Wire.beginTransmission(i2cAddress);

  Wire.write((int)(address >> 8));   // MSB
  Wire.write((int)(address & 0xFF)); // LSB
  Wire.endTransmission();

  Wire.requestFrom(i2cAddress, 1);

  rData =  Wire.read();

  return rData;
}


void setup()
{

  Serial.begin(9600);
  Wire.begin();
  pinMode(INTERRUPTED_PIN1, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(INTERRUPTED_PIN1), keyPressedOnPCF8574, CHANGE);

  //set pinMode to INPUT
  for (byte i = 0; i < 8; i++) {
    pcf8574_1.pinMode(PCF_PINS[i], INPUT);
    pcf8574_2.pinMode(PCF_PINS[i], INPUT);
    pcf8574_3.pinMode(PCF_PINS[i], INPUT);
    pcf8574_4.pinMode(PCF_PINS[i], INPUT);
  }

  pcf8574_1.begin();
  pcf8574_2.begin();
  pcf8574_3.begin();
  pcf8574_4.begin();
 
  int address = 0;

  // Write and read from chip 0
  writeAddress(PCF8574_1, address, P1_array[8] );
  byte readVal = readAddress(PCF8574_1, address);
  Serial.print("The returned value is ");
  Serial.println(readVal);

  // Write and read from chip 1
  writeAddress(PCF8574_2, address, P2_array[8] );
  readVal = readAddress(PCF8574_2, address);
  Serial.print("The returned value is ");
  Serial.println(readVal);

  // Write and read from chip 2
  writeAddress(PCF8574_3,  address, P3_array[8] );
  readVal = readAddress(PCF8574_3, address);
  Serial.print("The returned value is ");
  Serial.println(readVal);


  // Write and read from chip 3
  writeAddress(PCF8574_4, address, P4_array[8] );
  readVal = readAddress(PCF8574_4, address);
  Serial.print("The returned value is ");
  Serial.println(readVal);

  setupOutPins();//set the pinMode() function to LEDs
  digitalWrite ( OutPins[0], 1);

}

void setupOutPins() {
  for (s = 0; s < 4; s++) {
    pinMode (OutPins[s], OUTPUT);
    delayMicroseconds (100);
  }
}


void loop()
{
  function1();
  function2();
  function3();
  function4();
 

}

void function1() {

PCF8574::DigitalInput d1 = pcf8574_1.digitalReadAll();
int P1_array[] = {d1.p0, d1.p1, d1.p2, d1.p3, d1.p4, d1.p5, d1.p6, d1.p7};

  if (keyPressed1) {

    Serial.print("READ VALUE FROM PCF P1: ");
    for (int j = 0; j < 8; j++) {
      Serial.print(P1_array[j]);
    }
    Serial.println(" ");

    if (d1.p6 == HIGH && d1.p7 == LOW) {
      digitalWrite(OutPins[1], 1);
    } else {
      if (d1.p6 == LOW) {
        digitalWrite(OutPins[1], 0);
      }
    }

    keyPressed1 = false;
  }
  if (keyPressed2) {
    Serial.println(" ");
    keyPressed2 = false;
  }

}

void function2() {

  if (keyPressed1) {

    Serial.print("READ VALUE FROM PCF P2: ");
    for (int j = 0; j < 8; j++) {
      Serial.print(P2_array[j]);
    }
    Serial.println(" ");

    if (d2.p1 == HIGH ) {
      digitalWrite(OutPins[0], 0);
    } else {
      if (d2.p1 == LOW && d2.p0 == HIGH) {
        digitalWrite(OutPins[0], 1);
      }
    }

    if (d2.p6 == HIGH && d2.p7 == LOW) {
      digitalWrite(OutPins[2], 1);
    } else {
      if (d2.p6 == LOW) {
        digitalWrite(OutPins[2], 0);
      }
    }

    keyPressed1 = false;
  }
  if (keyPressed2) {
    Serial.println(" ");
    keyPressed2 = false;
  }
}

void function3() {

  if (keyPressed1) {

    Serial.print("READ VALUE FROM PCF P3: ");
    for (int j = 0; j < 8; j++) {
      Serial.print(P3_array[j]);
    }
    Serial.println(" ");

    if (d3.p1 == HIGH) {
      digitalWrite(OutPins[1], 0);
    } else {
      if (d3.p1 == LOW && d3.p0 == HIGH) {
        digitalWrite(OutPins[1], 1);
      }
    }

    if (d3.p6 == HIGH && d3.p7 == LOW) {
      digitalWrite(OutPins[3], 1);
    } else {
      if (d3.p6 == LOW) {
        digitalWrite(OutPins[3], 0);
      }
    }

    keyPressed1 = false;
  }
  if (keyPressed2) {
    Serial.println(" ");
    keyPressed2 = false;
  }

}


void function4() {

  if (keyPressed1) {

    Serial.print("READ VALUE FROM PCF P4: ");
    for (int j = 0; j < 8; j++) {
      Serial.print(P4_array[j]);
    }
    Serial.println(" ");

    if (d4.p1 == HIGH) {
      digitalWrite(OutPins[2], 0);
    } else {
      if (d4.p1 == LOW && d4.p0 == HIGH) {
        digitalWrite(OutPins[2], 1);
      }
    }

    if (d4.p6 == HIGH && d4.p7 == LOW) {
      digitalWrite(OutPins[4], 1);
    } else {
      if (d4.p6 == LOW) {
        digitalWrite(OutPins[4], 0);
      }
    }

    keyPressed1 = false;
  }
  if (keyPressed2) {
    Serial.println(" ");
    keyPressed2 = false;
  }
}

P1_array, P2_array, P3_array and P4_array only have 8 elements numbered 0..7.

You cannot access an 8th element...

  writeAddress(PCF8574_1, address, P1_array[8] );
  writeAddress(PCF8574_2, address, P2_array[8] );
  writeAddress(PCF8574_3,  address, P3_array[8] );
  writeAddress(PCF8574_4, address, P4_array[8] );

Why use multiple EEPROMs when you only write a single byte to each?

pcbbc:
P1_array, P2_array, P3_array and P4_array only have 8 elements numbered 0..7.

You cannot access an 8th element...

  writeAddress(PCF8574_1, address, P1_array[8] );

writeAddress(PCF8574_2, address, P2_array[8] );
  writeAddress(PCF8574_3,  address, P3_array[8] );
  writeAddress(PCF8574_4, address, P4_array[8] );




Why use multiple EEPROMs when you only write a single byte to each?

You cannot access an 8th element...
It means it contains 8 elements ...indexes are from 0 to 7
Dont I need to use multiple eeproms?
I cant understand how can I do this using single eeprom
Cause I have 0x20 to 0x27 ,4 I2C addresses

Raweesha:
You cannot access an 8th element...
It means it contains 8 elements ...indexes are from 0 to 7

No it doesn't. You can't just make up syntax.

In C/C++...

P2_array[8]

...accesses and returns the 8th element of the array P2_array - pure and simple.

Dont I need to use multiple eeproms?
I cant understand how can I do this using single eeprom

Write each value to a different address.
The I2C address ('i2cAddress' = address of the I2C device) is different from the memory address of the EEPROM ('address' = which memory location in the EEPROM you are reading/writing).
You are always writing to address 0 for some reason.

int OutPins[8] = {13, 12, 11, 10}; //led pin

What are the other 4 pins? 0, 0, 0, 0?

CrossRoads:
int OutPins[8] = {13, 12, 11, 10}; //led pin

What are the other 4 pins? 0, 0, 0, 0?

Good point. And the OP is accessing elemnet 4 here...

    if (d4.p6 == HIGH && d4.p7 == LOW) {
      digitalWrite(OutPins[4], 1);
    } else {
      if (d4.p6 == LOW) {
        digitalWrite(OutPins[4], 0);
      }

CrossRoads:
int OutPins[8] = {13, 12, 11, 10}; //led pin

What are the other 4 pins? 0, 0, 0, 0?

OK now I modified

If you change your code, post the new code (the whole program) so that we can keep up.

Please don’t edit your original post and modify code. The train of the tread is completely lost when you do that.

Make a new post with the updated code.

Seems to me the only thing you changed was...

int OutPins[4]

You still haven’t fixed your array out of bounds errors...

writeAddress(PCF8574_1, address, P1_array[8] );
writeAddress(PCF8574_2, address, P2_array[8] );
writeAddress(PCF8574_3,  address, P3_array[8] );
writeAddress(PCF8574_4, address, P4_array[8] );
digitalWrite(OutPins[4], 1);
digitalWrite(OutPins[4], 0);

Also in function1() why are you creating a new copy of d1 and P1_Array when you already have global copies?

Also in your initialisation you’re are aware that this code...

PCF8574::DigitalInput d1 = pcf8574_1.digitalReadAll();
int P1_array[8] = {d1.p0, d1.p1, d1.p2, d1.p3, d1.p4, d1.p5, d1.p6, d1.p7};

..and the other code like it is capturing the instantaneous state of the inputs. The arrays will not dynamically update. If you want them to update you’ll have to write code to do it...

PCF8574::DigitalInput d1 = pcf8574_1.digitalReadAll();
array[0] = d1.p0;
array[1] = d1.p1;
....

Why not have an array of PCF8574?