Question about store data in external eeprom 24LC256

Hello. I have connected external eeprom (I2C) 24LC256 to my hardware setup.

I would like to store last input state after power loss.

Here is my sketch:

//IR//
#include <IRremote.h>
#define irPin 11
IRrecv irrecv(irPin);
decode_results results;

//PRZYCISKI//
#define OPT1sw 18
#define OPT2sw 19
#define OPT3sw 20
#define COAXsw 21
#define USBsw 22
#define BTsw 23


//LEDy//
#define OPT1led 9
#define OPT2led 10
#define OPT3led 8
#define COAXled 31
#define USBled 14
#define BTled 30

//AK4118
#define IPS0 4
#define IPS1 3
#define ISOusb 0
#define MUXbt 1
#define MUXak 2


//RGB//
int redPin = 12;
int greenPin = 13;
int bluePin = 15;
//RGB//
int potPin_red = A7;
int potPin_green = A6;
int potPin_blue = A5;
//RGB//
int readValue_red;
int readValue_green;
int readValue_blue;
//RGB//
int writeValue_red;
int writeValue_green;
int writeValue_blue;



void setup()
{

  irrecv.enableIRIn();

  //PRZYCISKI//
  pinMode(OPT1sw, INPUT_PULLUP);
  pinMode(OPT2sw, INPUT_PULLUP);
  pinMode(OPT3sw, INPUT_PULLUP);
  pinMode(COAXsw, INPUT_PULLUP);
  pinMode(USBsw, INPUT_PULLUP);
  pinMode(BTsw, INPUT_PULLUP);

  //LEDy//
  pinMode(OPT1led, OUTPUT);
  pinMode(OPT2led, OUTPUT);
  pinMode(OPT3led, OUTPUT);
  pinMode(COAXled, OUTPUT);
  pinMode(USBled, OUTPUT);
  pinMode(BTled, OUTPUT);

  //MUXy//
  pinMode(MUXak, OUTPUT);
  pinMode(MUXbt, OUTPUT);

  //IZOLATOR//
  pinMode(ISOusb, OUTPUT);

  //AK4118//
  pinMode(IPS0, OUTPUT);
  pinMode(IPS1, OUTPUT);

  //RGB//
  pinMode(potPin_red, INPUT);
  pinMode(potPin_green, INPUT);
  pinMode(potPin_blue, INPUT);
  //RGB//
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);

  digitalWrite(IPS0, HIGH);
  digitalWrite(IPS1, HIGH);
  digitalWrite(ISOusb, LOW);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, LOW);
  digitalWrite(USBled, HIGH);
  digitalWrite(OPT1led, LOW);
  digitalWrite(OPT2led, LOW);
  digitalWrite(OPT3led, LOW);
  digitalWrite(COAXled, LOW);
  digitalWrite(BTled, LOW);

}

void ledsoff()
{
  digitalWrite(OPT1led, LOW);
  digitalWrite(OPT2led, LOW);
  digitalWrite(OPT3led, LOW);
  analogWrite(COAXled, LOW);
  digitalWrite(USBled, LOW);
  analogWrite(BTled, LOW);
}



void opt1source()
{
  digitalWrite(IPS0, LOW);
  digitalWrite(IPS1, LOW);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
}


void opt2source()
{
  digitalWrite(IPS0, HIGH);
  digitalWrite(IPS1, LOW);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
}

void opt3source()
{
  digitalWrite(IPS0, LOW);
  digitalWrite(IPS1, HIGH);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
}

void coaxsource()
{
  digitalWrite(IPS0, HIGH);
  digitalWrite(IPS1, HIGH);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
}

void btsource()
{
  digitalWrite(IPS0, HIGH);
  digitalWrite(IPS1, HIGH);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, HIGH);
  digitalWrite(MUXak, LOW);
}


void usbsource()
{
  digitalWrite(IPS0, HIGH);
  digitalWrite(IPS1, HIGH);
  digitalWrite(ISOusb, LOW);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, LOW);
}



void loop()
{

  //RGB//
  analogWrite(redPin,  analogRead(potPin_red) / 4);
  analogWrite(greenPin, analogRead(potPin_green) / 4);
  analogWrite(bluePin, analogRead(potPin_blue) / 4);



  //ZRODLA//
  //OPT1//
  if (digitalRead(OPT1sw) == LOW)
  {
    ledsoff();
    opt1source();
    digitalWrite(OPT1led, HIGH);
  }

  //OPT2//
  if (digitalRead(OPT2sw) == LOW)
  {
    ledsoff();
    opt2source();
    digitalWrite(OPT2led, HIGH);
  }

  //OPT3//
  if (digitalRead(OPT3sw) == LOW)
  {
    ledsoff();
    opt3source();
    digitalWrite(OPT3led, HIGH);
  }

  //COAX//
  if (digitalRead(COAXsw) == LOW)
  {
    ledsoff();
    coaxsource();
    digitalWrite(COAXled, HIGH);
  }

  //BT//
  if (digitalRead(BTsw) == LOW)
  {
    ledsoff();
    btsource();
    digitalWrite(BTled, HIGH);
  }


  //USB//
  if (digitalRead(USBsw) == LOW)
  {
    ledsoff();
    usbsource();
    digitalWrite(USBled, HIGH);
  }

  

  if (irrecv.decode(&results))
  {
    switch (results.value)
    {
      case 0x801:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
        break;
      case 0x01:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
        break;
   
      case 0x802:  // OPT2
        ledsoff();
        opt2source();
        digitalWrite(OPT2led, HIGH);
        break;
      case 0x02:  // OPT2
        ledsoff();
        opt2source();
        digitalWrite(OPT2led, HIGH);
        break;
        
      case 0x803:  // OPT3
        ledsoff();
        opt3source();
        digitalWrite(OPT3led, HIGH);
        break;
      case 0x03:  // OPT3
        ledsoff();
        opt3source();
        digitalWrite(OPT3led, HIGH);
        break;

      case 0x804:  // COAX
        ledsoff();
        coaxsource();
        digitalWrite(COAXled, HIGH);
        break;
      case 0x04:  // COAX
        ledsoff();
        coaxsource();
        digitalWrite(COAXled, HIGH);
        break;
        
      case 0x805:  // USB
        ledsoff();
        usbsource();
        digitalWrite(USBled, HIGH);
        break;
      case 0x05:  // USB
        ledsoff();
        usbsource();
        digitalWrite(USBled, HIGH);
        break;
        
      case 0x806:  // BT
        ledsoff();
        btsource();
        digitalWrite(BTled, HIGH);
        break;
      case 0x06:  // BT
        ledsoff();
        btsource();
        digitalWrite(BTled, HIGH);
        break;
        
    }
    irrecv.resume();
  }
  
}

So. I have 6 different input > opt1;opt2;opt3;coax;bt;usb . All controlled by tact switch OPT1sw;OPT2sw;OPT3sw;COAXsw;BTsw;USBsw and with IR remote controll.

My idea is save input after changed (for example now I have opt1, press opt2sw and I have opt2. Now I need to save this to eeprom and restore to opt2 after power loss):

int lastInput = 0 ;

void loop() {
lastInput = EEPROM.read(EEPROMaddress) ;

void opt1source()
{
  digitalWrite(IPS0, LOW);
  digitalWrite(IPS1, LOW);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
  EEPROM.write(EEPROMaddress, lastInput);
}

its good way?

Or better is:

a) for tact switch:

 if (digitalRead(OPT1sw) == LOW)
  {
    ledsoff();
    opt1source();
    digitalWrite(OPT1led, HIGH);
 EEPROM.write(EEPROMaddress, lastInput);
  }

b) for IR:

    case 0x801:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
EEPROM.write(EEPROMaddress, lastInput);
        break;
      case 0x01:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
EEPROM.write(EEPROMaddress, lastInput);
        break;

which will be better? I need to put saves and read from eeprom for 6 Inputs (opt1;2;3, coax , bt , usb) (one of them after power loss)

Have a good day!

Regards

lastInput = EEPROM.read(EEPROMaddress) ;

will write to your internal EEPROM not the external.

Right.

I have read this > How to Connect a 24LC256 EEPROM to an Arduino

#include <Wire.h>    

int lastInput = 0 ;
 
#define disk1 0x50    //Address of 24LC256 eeprom chip

[b][all my code]
[/b]
void setup
{
  Wire.begin();  
 
  unsigned int address = 0;

[b][all my code]
[/b]
}


void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) 
{
  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddress >> 8));   // MSB
  Wire.send((int)(eeaddress & 0xFF)); // LSB
  Wire.send(data);
  Wire.endTransmission();
 
  delay(5);
}


byte readEEPROM(int deviceaddress, unsigned int eeaddress ) 
{
  byte rdata = 0xFF;
 
  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddress >> 8));   // MSB
  Wire.send((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) rdata = Wire.receive();
 
  return rdata;
}

[b][all my code]
[/b]

void opt1source()
{
  digitalWrite(IPS0, LOW);
  digitalWrite(IPS1, LOW);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
    writeEEPROM(disk1, address, lastInput);

}

Something like this?

Also there are delay(5) for save to eeprom, which I need change to milis.

There is a very good library for external i2c eeproms by Jack Christensen GitHub - JChristensen/extEEPROM: Arduino library to support external I2C EEPROMs.

It is available through the library manager. I will handle block writes, deals with the page boundary issue, and actively checks in 500us delay periods, for the bus being ready rather than using delay(5).

So… Its first time when I worki with eeprom/external eeprom.

#include <extEEPROM.h>    //http://github.com/JChristensen/extEEPROM/tree/dev
#include <Streaming.h>    //http://arduiniana.org/libraries/streaming/
#include <Wire.h>         //http://arduino.cc/en/Reference/Wire

//One 24LC256 EEPROMs on the bus
const uint32_t totalKBytes = 64;         //for read and write test functions
extEEPROM eep(kbits_256, 1, 64);         //device size, number of devices, page size


void setup(void)
{
    uint8_t eepStatus = eep.begin(extEEPROM::twiClock400kHz);   //go fast!
 


uint8_t chunkSize = 64;    //this can be changed, but must be a multiple of 4 since we're writing 32-bit integers
//    eeErase(chunkSize, 0, totalKBytes * 1024 - 1);
    eeWrite(chunkSize);
    eeRead(chunkSize);

    dump(0, 16);            //the first 16 bytes
    dump(32752, 32);        //across the device boundary
    dump(65520, 16);        //the last 16 bytes
}

void loop(void)
{
my code
}

this library have a lot of code which i dont understand at all and i dont know how to apply this library for my sketch .

So I need some help… Maybe somone can explain how to work with this library and solve my lastinput state to external eeprom?

this library have a lot of code which i dont understand at all and i dont know how to apply this library for my sketch .

Why do you want to write to an external EEPROM and not the internal one?

What is an "input state" in your sketch? The term isn't used there. Keep in mind that the comments are not in English, so most readers here won't understand them.

Which part of the extEEPROM example code don't you understand? There are eep.write() and eep.read() calls, what else do you need?