Need help with EEPROM

Hello
I need help with this EEPROM. i need to store the value from ir receiver so when the arduino power off and on it will remember the last ir code.
The project is "universal IR decoder and Encoder" with one push button. i'm not soo good with programming. I need yours help please...THANKS!!!

The sketch:
MODERATOR EDIT - Auto formatted code and added code tags

#include <IRremote.h>

int RECV_PIN = 11;
int BUTTON_PIN = 12;
int STATUS_PIN = 13;

IRrecv irrecv(RECV_PIN);
IRsend irsend;

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(BUTTON_PIN, INPUT);
  pinMode(STATUS_PIN, OUTPUT);
}

// Storage for the recorded code
int codeType = -1; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
int codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state

// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results)
{
  codeType = results->decode_type;
  int count = results->rawlen;
  if (codeType == UNKNOWN)
  {
    Serial.println("Received unknown code, saving as raw");
    codeLen = results->rawlen - 1;
    // To store raw codes:
    // Drop first value (gap)
    // Convert from ticks to microseconds
    // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
    for (int i = 1; i <= codeLen; i++)
    {
      if (i % 2)
      {
        // Mark
        rawCodes[i - 1] = results->rawbuf[i] * USECPERTICK - MARK_EXCESS;
        Serial.print(" m");
      }
      else
      {
        // Space
        rawCodes[i - 1] = results->rawbuf[i] * USECPERTICK + MARK_EXCESS;
        Serial.print(" s");
      }
      Serial.print(rawCodes[i - 1], DEC);
    }
    Serial.println("");
  }
  else
  {
    if (codeType == NEC)
    {
      Serial.print("Received NEC: ");
      if (results->value == REPEAT)
      {
        // Don't record a NEC repeat value as that's useless.
        Serial.println("repeat; ignoring.");
        return;
      }
    }
    else if (codeType == SONY)
    {
      Serial.print("Received SONY: ");
    }
    else if (codeType == RC5)
    {
      Serial.print("Received RC5: ");
    }
    else if (codeType == RC6)
    {
      Serial.print("Received RC6: ");
    }
    else
    {
      Serial.print("Unexpected codeType ");
      Serial.print(codeType, DEC);
      Serial.println("");
    }
    Serial.println(results->value, HEX);
    codeValue = results->value;
    codeLen = results->bits;
  }
}

void sendCode(int repeat)
{
  if (codeType == NEC)
  {
    if (repeat)
    {
      irsend.sendNEC(REPEAT, codeLen);
      Serial.println("Sent NEC repeat");
    }
    else
    {
      irsend.sendNEC(codeValue, codeLen);
      Serial.print("Sent NEC ");
      Serial.println(codeValue, HEX);
    }
  }
  else if (codeType == SONY)
  {
    irsend.sendSony(codeValue, codeLen);
    Serial.print("Sent Sony ");
    Serial.println(codeValue, HEX);
  }
  else if (codeType == RC5 || codeType == RC6)
  {
    if (!repeat)
    {
      // Flip the toggle bit for a new button press
      toggle = 1 - toggle;
    }
    // Put the toggle bit into the code to send
    codeValue = codeValue & ~(1 << (codeLen - 1));
    codeValue = codeValue | (toggle << (codeLen - 1));
    if (codeType == RC5)
    {
      Serial.print("Sent RC5 ");
      Serial.println(codeValue, HEX);
      irsend.sendRC5(codeValue, codeLen);
    }
    else
    {
      irsend.sendRC6(codeValue, codeLen);
      Serial.print("Sent RC6 ");
      Serial.println(codeValue, HEX);
    }
  }
  else if (codeType == UNKNOWN /* i.e. raw */)
  {
    // Assume 38 KHz
    irsend.sendRaw(rawCodes, codeLen, 38);
    Serial.println("Sent raw");
  }
}

int lastButtonState;

void loop()
{
  // If button pressed, send the code.
  int buttonState = digitalRead(BUTTON_PIN);
  if (lastButtonState == HIGH && buttonState == LOW)
  {
    Serial.println("Released");
    irrecv.enableIRIn(); // Re-enable receiver
  }
  if (buttonState)
  {
    Serial.println("Pressed, sending");
    digitalWrite(STATUS_PIN, HIGH);
    sendCode(lastButtonState == buttonState);
    digitalWrite(STATUS_PIN, LOW);
    delay(50); // Wait a bit between retransmissions
  }
  else if (irrecv.decode(&results))
  {
    digitalWrite(STATUS_PIN, HIGH);
    storeCode(&results);
    irrecv.resume(); // resume receiver
    digitalWrite(STATUS_PIN, LOW);
  }
  lastButtonState = buttonState;
}

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

What have you tried so far ?
Have you looked at the EEPROM example sketches ?

Yes...but i don't understand. i need someone to help me with the code...

So where are you stuck ?

When you want to store a value, such as that just received from the IR receiver, you write it to EEPROM using the EEPROM.put() function. To reload the value on startup you use the EEPROM.get() function in setup() to load the previously saved value into a variable

Thank you. but i dont understand where to put the functions EEPROM.put() and EEPROM.get() in the sketch. what value i need to store. i'm not good with programing. can you copy the sketch and paste it with the EEPROM functions? its will be very helpful for me...please :slight_smile:

Which line or lines of code in the sketch receive the IR code and which variable is it stored in ?

Unfortunately, this code was designed to print the stored data out to the serial monitor (which is usually captured there as input to other IR Player programs). So, even if you added the save to EEPROM code here, this program is not designed with the logic to fetch it and use it anyways. Basically the code would need to be rewritten to reload the variables as stated in the store (serial print section) from EEPROM most likely added in the setup portion of the program based on a check if there is valid data in EEPROM.

or you could store results->rawlen bytes from results->rawbuf...

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