IR reciever and transmitter with EEPROM

Hi, I am trying to build something similar to one of the features on a flipper zero, the feature I am trying to recreate is the one where you can read an IR code save it, and then transmit a copy. I am using an IR receiver and an IR transmitter paired with 5 buttons with different purposes (these purposes are described in the code), a potentiometer (used to go through 255 addresses of EEPROM), and lastly as a form of interface an LCD with I2C. I have a picture of the wiring diagram (the red LED is an IR transmitter) below so that it is easier to understand, for the record the code published is not finished as I focused on one issue at a time. Now for the actual problem, the issue with all of this is that I haven't found a way to properly save the HEX codes on EEPROM. This is a problem because I cannot transmit the codes if they are not stored on the EEPROM. what would be best is that when you press the button connected to pin 10 the receiver collects codes and then by pressing the button connected to pin 12 the code is saved to EEPROM. I want to make some sort of basic UI on my LCD so that the code is also displayed there which does work, but the issue arises when trying to save the values to EEPROM as the data I am trying to store has two parts results.value and HEX. From what I have found it is not possible to store two values on EEPROM address so this would make getting the codes from EEPROM and transmitting them not possible using my method. Is there any way of doing this differently? Any helpful response would be greatly appreciated.

PS sorry if this question has been badly explained i find this topic very complex so please ask if you found something unclear.

#include <LiquidCrystal_I2C.h>
#include <IRremote.h>

const int RECV_PIN = 7;

IRrecv irrecv(RECV_PIN);
IRsend irsend;

decode_results results;

LiquidCrystal_I2C lcd(0x27,20,4);

int potVal;
int currentCode;
int readCodeEEPROM;


void setup()
{
  pinMode(13, INPUT_PULLUP); //button for clearing displayed code
  pinMode(12, INPUT_PULLUP); //button for saving codes
  pinMode(11, INPUT_PULLUP); //button for sending chosen code                 #
  pinMode(10, INPUT_PULLUP); //button for recieving codes                     #
  pinMode(8, INPUT_PULLUP); //button to clear selected eeprom


  pinMode(9, OUTPUT); //IR led
  pinMode(2, INPUT); //IR reciever


  lcd.init();
  lcd.backlight();


  irrecv.enableIRIn();


  Serial.begin(9600);
}

void loop()
{
  potVal = analogRead(A1);
  potVal = map(potVal, 0, 1023, 255, 0);


  readCodeEEPROM = EEPROM.read(potVal);


  lcd.setCursor(16, 0);
  lcd.print(potVal);


  if (irrecv.decode(&results) && digitalRead(10) == LOW) { //if ir codes recieved
      lcd.setCursor(1, 1);
      lcd.print("Code Recieved");
      lcd.setCursor(1, 0);
      lcd.print(results.value, HEX);
      Serial.println(results.value, HEX);
  }

  if (digitalRead(12) == LOW) { //saving ir codes and hex
    EEPROM.write(potVal, currentCode);
  }

  if (digitalRead(11) == LOW) { //sending ir codes
    irsend.sendNEC(readCodeEEPROM, 32);
    delay(30);
  }
}

EEPROM.write(potVal, currentCode);

One immediate problem is the the write() function saves a single byte to EEPROM but potVal is a 2 byte int

I thought that with the EEPROM.write function, the first parameter was the address to which the 2nd parameter is saved in EEPROM

If I had the wrong idea how could I fix this?

My apologies, you are, of course, correct, but currentCode is also an int so you still have a problem

Consider using EEPROM.put() instead of write()

Alright i will read up on that function

Would it be possible to just use EEPROM.put(potVal, currentCode)?

That is the correct syntax but be careful with the address that you use.

For instance, if you are saving an int then 2 bytes will be saved so the address of the next variable to be saved needs to be at least 2 bytes further on than the previous one and not just the next address

I have not looked at your code in detail but you might consider saving the code values in an array and using the put() function to save the whole array at once. Whether this is useful I will leave up to you

Thanks a lot for the help i have updated some of the code, but if I stumble on further problems with saving codes to the EEPROM I will try out your idea with arrays. Again thank you.

#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>

const int RECV_PIN = 7;

IRrecv irrecv(RECV_PIN);
IRsend irsend;

decode_results results;

LiquidCrystal_I2C lcd(0x27,20,4);

int potVal;
int currentCode;
int readCodeEEPROM;


void setup()
{
  pinMode(13, INPUT_PULLUP); //button for clearing displayed code
  pinMode(12, INPUT_PULLUP); //button for saving codes
  pinMode(11, INPUT_PULLUP); //button for sending chosen code                 #
  pinMode(10, INPUT_PULLUP); //button for recieving codes                     #
  pinMode(8, INPUT_PULLUP); //button to clear selected eeprom


  pinMode(9, OUTPUT); //IR led
  pinMode(2, INPUT); //IR reciever


  lcd.init();
  lcd.backlight();


  irrecv.enableIRIn();


  Serial.begin(9600);
}

void loop()
{
  potVal = analogRead(A1);
  potVal = map(potVal, 0, 1023, 255, 0);


  readCodeEEPROM = EEPROM.read(potVal);



  lcd.setCursor(16, 0);
  lcd.print(potVal);


  if (irrecv.decode(&results) && digitalRead(10) == LOW) { //if ir codes recieved
    lcd.setCursor(1, 1);
    lcd.print("Code Recieved");
    delay(1000);
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print(results.value, HEX);
    Serial.println(results.value, HEX);
  } else {
    lcd.setCursor(1, 0);
    lcd.print(readCodeEEPROM);
  }

  if (digitalRead(12) == LOW) { //saving ir codes and hex
    EEPROM.put(potVal, currentCode);
  }

  if (digitalRead(11) == LOW) { //sending ir codes
    irsend.sendNEC(readCodeEEPROM, 32);
    delay(30);
  }
}

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