Can i put more then one big numbers in EEPOM?

Hi, I programed remote control blinds and all works grate but i need to add new remote code manually so i have opportunity to learn EEPROM.

My question is can i add more then one remote ID to EEPROM?

Remote1 ID 111111
Remote2 ID 222222

I did googling and i think i need to use put() and get() if i want to add numbers bigger then 255 but i don't know if i can add 2 or more.
So EEPROM.put write 11011001 00000011 1 (111111) to 3 byte and then i can add next ID (222222)?

Exemple:

If (button1 == HIGHT)
{
 EEPROM.put(0, 111111);
 EEPROM.put(4, 111111);
}

And to read it back:

 EEPROM.get(0, Remote1);
EEPROM.get(4, Remote2);

Serial.println(Remote1);
Serial.println(Remote2);

I could not find simple example of code how to use put() and get(), in https://www.arduino.cc/en/Reference/EEPROMPut is very complicated code which I don't understand yet because I learn coding so can someone correct me if i'm wrong and give some tips how to do this properly.

i don't know if i can add 2 or more

Of course you can

Each variable type requires a fixed number of bytes in which to store its value so you must allow space in the EEPROM when using put()

ie, an int takes 2 bytes so you can do

EEPROM.put(0, int1);  //store the int in bytes 0 and 1 of the EEPROM
EEPROM.put(2, int1);  //store the int in bytes 2 and 3 of the EEPROM

As long as the number of bytes between each storage location is large enough then you are OK and data for one variable will not overwrite data for another but you must manage the destination address yourself

When using EEPROM.get() the compiler knows how many bytes to read to get back the data type specified, but it is up to you to specify the address at which the data was stored

1. Basic commands to perform read/write with EEPROM are:

(1) EEPROM.write(0x1010, 0x23);   //23 would be writtemn into location 0x1010.
(2)  byte x = EEPROM.read(0x1010);  //[color=red]edit[/color]data from EEPROM location 0x1010 would be copied to variable x

2. Command to store 111111 decimal.

1111111 decimal = 1B207 in hex;  
==> long x1 = 111111;  // = 0x0001B207 ==> 32-bit long

EEPROM.put(0x1010, x1); //4 consecutive memory locations: 0x1010, 0x1011, 0x1012, 0x1013 would be occupied; lower byte will reside in lower location.

3. Command to store 222222 decimal

==> long x2 = 222222;  //0x0003640E
EEPROM.put(0x1014, x2);             //locations: 0x1014, 0x1015, 0x1016, 0x1017 would be occupied

Great! I will try it today.
Thank You all for replay!

GolamMostafa:
1. Basic commands to perform read/write with EEPROM are:

(1) EEPROM.write(0x1010, 0x23);   //23 would be writtemn into location 0x1010.

(2) byte x = EEPROM(0x1010); //data from EEPROM location 0x1010 would be copied to variable x

Do you ever proofread?

TheMemberFormerlyKnownAsAWOL:
Do you ever proofread?

There is a good justification for having AWOL as a part of Forum Name:

byte x = EEPROM(0x1010);

==> byte x = EEPROM.read(0x1010);
==> byte x = EEPROM.read(0x1010);

Is there any reason not to use put() and get() with byte variables, particularly as put() does not write to the EEPROM if the value has not changed

Arduino UNO experiments indicate that the following two blocks of codes are equivalent in terms of "results' and "execution times".

byte x = EEPROM.read(0x1010); //[color=red]edit[/color]EEPTOM.read(0x1010);

and

byte x;
EEPROM.get(0x1010, x);

Do you have any other observation, please share.

byte x = EEPTOM.read(0x1010);Not a very thorough experiment then.

UKHeliBob:
Of course you can

All works as it should, i did add 2 remotes IDs to EEPROM 0 and EEPROM 4 and it save them!

put() and get () is perfect way to do this become put() works like update() and don't overwrites information so it save write cycles on EEPROM and more important for me it is very easy to use, just put(4, 118317) and 118317 is saved and i'm done, no need to split it or disassemble and reassemble.

Thank you for confirmation. My confusion was that when i look for easy way to do this, put() and get () was beryl mention like 1 post on it and 20 with some very complicated answers like:

1111111 decimal = 1B207 in hex;...

where i just needed to

EEPROM.put(0, 1111111 );

and I'm done, and everything works!

SERIAL MONITOR:

Remote1: 118317
Remote2: 82943
RemoteNr: 118317
Channel 1
Button Up

UKHeliBob all works with put()/get() but when i try to add "Channel" to save along site Remote ID i had some problems.

When i try to save "Remote1" and "Channel1" (Check script below) using put() where Channel1 is 1-15 i got 2050 nr when using put() but when i use write() and read() on Channel1, all did work (1-15 on serial print).
So my question is: Is there point where i should not use put()? or that was some mistake i did in other part of code and put() and write() should work?

Don't work:

if (buttonState1 == HIGH)
  {
    if (i > 0)
    {
      EEPROM.put(0, RemoteNr);
      delay(100);
      EEPROM.put(8, Channel);
    }

Did work:

if (buttonState1 == HIGH)
  {
    if (i > 0)
    {
      EEPROM.put(0, RemoteNr);
      delay(100);
      EEPROM.write(8, Channel);
    }

P.S. I will try ti use update() insted of write()

TheMemberFormerlyKnownAsAWOL:

byte x = EEPTOM.read(0x1010);

Not a very thorough experiment then.

#include<EEPROM.h>
void setup()
{
  Serial.begin(115200);
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;
  EEPROM.write(0x1010, 0x23);
  byte y;
  TCCR1B = 0x01;  //TC1 ON with prescaler-1
  //EEPROM.get(0x1010, y); //54691
  y = EEPROM.read(0x1010);   //54709
  TCCR1B =  0;     //TC1 is OFF
  Serial.println(TCNT1);  //read counts
  //Serial.println(y, HEX);
}

void loop()
{

}

Ageing and certainly ageing. Serious warning for me that I should be thinking twice before I decide to go out for recreation in the park with my 20-yrs old mild-autistic non-vocal son who will be able to return home, I am not sure, if I forget him.

QrokPL:
[...] some very complicated answers like:

1111111 decimal = 1B207 in hex;...

Though complicated apparently, yet it explains why put() method is needed to store the given value in the EEPROM.

GolamMostafa:
Though complicated apparently, yet it explains why put() method is needed to store the given value in the EEPROM.

I think is apparent from my firs post I have very low understanding of Arduino coding and i need as simple answer as it can be for me to get interested in continuation quest to learn basics, if you did write "use EEPROM.put(0, 1111111); I would understand that, "1111111 decimal = 1B207 in hex;..." i did not.
In some time i probably want to learn how put() work but for now i need to know how to use it.

But Thank you for you time!

QrokPL:
I think is apparent from my firs post I have very low understanding of Arduino coding and i need as simple answer as it can be for me to get interested in continuation quest to learn basics, if you did write "use EEPROM.put(0, 1111111); I would understand that, "1111111 decimal = 1B207 in hex;..." i did not.
In some time i probably want to learn how put() work but for now i need to know how to use it.

To us (the human beings), the decimal numbers are formed using symbols: 0, 1, ..., 8, 9. When we assign/associate/declare a variable with a decimal number like the following, the number is stored in computer memory in "bit form/binary".

 x1 = 111111;   //decimal number

When the above decimal number is converted into binary, we get: 1 1011 0010 0000 0111. This binary number can be represented in compact form (for the shake of convenience) using hexadecimal base where each 4-bit are added together and is given assigned one of these symbols: 0, 1, ..., 9, A (10), B(11), C(12), D(13), E(14), and F(15). Thus, we have: 1 B 2 0 7 ==> 1B207 against 1 1011 0010 0000 0111.

Now, we need a memory space large enough to hold this bit form: 1 1011 0010 0000 0111; where, we have 17-bit. In computer architecture, the data chunk goes by 8-bit; therefore, we have: 0001 1011 0010 0000 0111 which is 20-bit.

In Arduino UNO/C Language, we can ask for a memory space of 8-bit (byte) or 16-bit (int) or 32-bit (long) or 64-bit (long long). There is no option to ask for 20-bit space. So, we come up with this declaration:

long x1 = 111111;

Is hexadecimal take less bit then decimals?
111111 take 11011001 00000011 1 witch is 3 byte in EEPROM, how meany byte 111111 will take when i convert it to 1B207?

Or hexadecimal is only useful when i don't want to write 11011001000000111 just use 1B207 but i never use Binary number in my code i only need to know how much bits my number will take so i know how much memory i need to allocate or for byte, int, long...

For me is hard to understand how hexadecimal will help me in normal low skill programming because i never need to use it in my programming. But thank you for explanation now i get how hexadecimal work i just need very simple useful example where i would use hexadecimal instead of decimal to fully get it.

What i understand, if there was no put() i would need to use 1B207 to write() 111111 to EEPROM without allocating every bit separately?

For me is hard to understand how hexadecimal will help me in normal low skill programming because i never need to use it in my programming

You never need to use it but it is convenient in some circumstances

For instance, I can visualise the bit pattern of say 0x98 as 0b10011000 because the bit patterns of 9 and 8 are easy to remember or work out in your head. When the numbers get larger then hexadecimal comes into its own even more. Would you rather deal with 0b1101111100101001 or 0xDF29, especially when typing in the string of 0s and 1s. Of course, you could just use 57129 in decimal but that gives you no clue as to the pattern of bits in the number

Of course when, say counting the number of people passing a sensor the decimal number is what you want but at other times the bit pattern may be important. It's horses for courses

Thank you for all help, i did accomplished what i wanted and learn some things abut EEPROM thanks to you!

I wanted to add to my remote controlled blinds ability to save up to 5 remotes ID and corresponding channels by pressing combinations of buttons witch are on the box, clear EEPROM memory if needed and all works as it should despite my very low ability to code!

I will post script here so maybe someone can point bad thinks and how to fix them or if someone want to see how i did it (probably everything is not how it should be coded but it works):

#include <EEPROM.h>

int rxPin = 2; // Input of 433 MHz receiver
int LED1 = 3; // LED for testing instead of servo commands

int buttonState1 = 0;
int buttonState2 = 0;

int button1 = 4;
int button2 = 5;

byte lastButtonState = HIGH;
byte buttonCounter = 0;

//Remotes ID
unsigned long Remote1;
unsigned long Remote2;
unsigned long Remote3;
unsigned long Remote4;
unsigned long Remote5;

//Channels:
byte Channel1;
byte Channel2;
byte Channel3;
byte Channel4;
byte Channel5;

// Buttons:
int UP = 3;
int Stop = 10;
int Down = 8;

void setup()

{
  pinMode(rxPin, INPUT); // Input of 433 MHz receiver

  Serial.begin(19200);
  Serial.println ("Is on!");

  pinMode(13, OUTPUT);
  pinMode(LED1, OUTPUT);

  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
}

void loop()
{
  /////////////////////////////////////////////// read EEPROM

  EEPROM.get(0, Remote1);
  Channel1 = EEPROM.read(4);
  EEPROM.get(5, Remote2);
  Channel2 = EEPROM.read(9);
  EEPROM.get(10, Remote3);
  Channel3 = EEPROM.read(14);
  EEPROM.get(15, Remote4);
  Channel4 = EEPROM.read(19);
  EEPROM.get(20, Remote5);
  Channel5 = EEPROM.read(24);

  ///////////////////////////////////////////////Remote signal decoding

  int i = 0;
  unsigned long t = 0;
  byte newBit = 0;
  byte bit = 0;
  unsigned long RemoteNr = 0;
  unsigned long Channel = 0;
  unsigned long Button = 0;
  byte prevBit = 0;

  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);


  while (i < 40)
  {
    t = pulseIn(rxPin, LOW, 1000000);

    if (t > 620 && t < 700)
    { bit = 0;
    }
    else if (t > 300 && t < 450)
    { bit = 1;
    }
    else
    {
      i = 0;
      break;
    }


    if (i >= 0 && i <= 17)
    {
      RemoteNr <<= 1;
      RemoteNr |= prevBit;
    }
    else if (i >= 18 && i <= 21)
    {
      Channel <<= 1;
      Channel |= prevBit;
    }
    else if (i >= 22 && i <= 25)
    {
      Button <<= 1;
      Button |= prevBit;
    }

    prevBit = bit;
    ++i;
    //Serial.print(prevBit); //Biniary print
  }


  if (i > 0)
  {
    printResult(RemoteNr, Channel, Button);
  }

  ///////////////////////////////////////////////////////////////////////////////////Save Remote ID and Channel to EEPROM

  checkButton(); //go to void checkButton()

  if (buttonState1 == HIGH) //If button 1 is pressed...
  {
    if (buttonCounter == 1)// ...and button 2 i clicked once...
    {
      if (i > 0) // ...and remote button is click...
      {
        delay(10);
        EEPROM.put(0, RemoteNr); // ...save remote ID and channel to EEPROM.
        delay(50);
        EEPROM.write(4, Channel);
        delay(50);
        buttonCounter = 0; //Clear button pressed count
      }
    }
    if (buttonCounter == 2)
    {
      if (i > 0)
      {
        delay(10);
        EEPROM.put(5, RemoteNr);
        delay(50);
        EEPROM.write(9, Channel);
        delay(50);
        buttonCounter = 0;
      }
    }
    if (buttonCounter == 3)
    {
      if (i > 0)
      {
        delay(10);
        EEPROM.put(10, RemoteNr);
        delay(50);
        EEPROM.write(14, Channel);
        delay(50);
        buttonCounter = 0;
      }
    }
    if (buttonCounter == 4)
    {
      if (i > 0)
      {
        delay(10);
        EEPROM.put(15, RemoteNr);
        delay(50);
        EEPROM.write(19, Channel);
        delay(50);
        buttonCounter = 0;
      }
    }
    if (buttonCounter == 5)
    {
      if (i > 0)
      {
        delay(10);
        EEPROM.put(20, RemoteNr);
        delay(50);
        EEPROM.write(24, Channel);
        delay(50);
        buttonCounter = 0;
      }
    }
  }

  if ((RemoteNr == Remote1 && Channel == Channel1) ||
      (RemoteNr == Remote2 && Channel == Channel2) ||
      (RemoteNr == Remote3 && Channel == Channel3) ||
      (RemoteNr == Remote4 && Channel == Channel4) ||
      (RemoteNr == Remote5 && Channel == Channel5))
  {
    if (UP)
    {
      digitalWrite(LED1, HIGH);
      delay(500);
      digitalWrite(LED1, LOW);
    }
    else if (Stop)
    {
      digitalWrite(LED1, HIGH);
      delay(500);
      digitalWrite(LED1, LOW);
    }
    else if (Down)
    {
      digitalWrite(LED1, HIGH);
      delay(500);
      digitalWrite(LED1, LOW);
    }

  }
  if (buttonCounter == 10) //If button 1 is pressed and button 2 is clicked ten times clear EEPROM memory
  {
    for (int i = 0 ; i < EEPROM.length() ; i++)
    {
      EEPROM.write(i, 255);
    }
  }
}

///////////////////////////////////////////////////////////////////////////////////Count button presses

void checkButton()
{
  if (buttonState1 == HIGH)
  {
    buttonState2 = digitalRead(button2);

    if (lastButtonState != buttonState2)
    {
      lastButtonState = buttonState2;
      if (buttonState2 == HIGH)
      {
        buttonCounter++;
        Serial.println(buttonCounter);
      }
    }
  }

  if (buttonState1 == LOW) //Clear button pressed count if button 1 is not press
  {
    buttonCounter = 0;
  }
}

///////////////////////////////////////////////////////////////////////////////////Print info to Serial Monitor

void printResult(unsigned long RemoteNr, unsigned long Channel, unsigned long Button)
{

  Serial.print("Remote1: ");
  Serial.println(Remote1);
  Serial.print("Remote2: ");
  Serial.println(Remote2);
  Serial.print("Remote3: ");
  Serial.println(Remote3);
  Serial.print("Remote4: ");
  Serial.println(Remote4);
  Serial.print("Remote5: ");
  Serial.println(Remote5);

  //RemoteNr
  Serial.print("RemoteNr: ");
  Serial.println(RemoteNr);

  //Channel;
  Serial.print("Channel ");
  switch (Channel) {
    case 8:
      Serial.println("1");
      break;
    case 4:
      Serial.println("2");
      break;
    case 12:
      Serial.println("3");
      break;
    case 2:
      Serial.println("4");
      break;
    case 10:
      Serial.println("5");
      break;
    case 6:
      Serial.println("6");
      break;
    case 17:
      Serial.println("7");
      break;
    case 1:
      Serial.println("8");
      break;
    case 9:
      Serial.println("9");
      break;
    case 5:
      Serial.println("10");
      break;
    case 13:
      Serial.println("11");
      break;
    case 3:
      Serial.println("12");
      break;
    case 11:
      Serial.println("13");
      break;
    case 7:
      Serial.println("14");
      break;
    case 15:
      Serial.println("All");
      break;
  }

  //Button
  Serial.print("Button ");
  if (Button == 3) {
    Serial.println("Up");
  }
  else if (Button == 10) {
    Serial.println("Stop");
  }
  else if (Button == 8) {
    Serial.println("Down");
  }
  Serial.println();
}

Serial Monitor output:

Remote1: 118317
Remote2: 118317
Remote3: 4294967295
Remote4: 4294967295
Remote5: 4294967295
RemoteNr: 82943
Channel 4
Button Stop

Arrays would make your code shorter and easier to maintain - there seem to be too many magic numbers too.

TheMemberFormerlyKnownAsAWOL:
Arrays would make your code shorter and easier to maintain - there seem to be too many magic numbers too.

Ok so next step, learn what is Array and try to implement in to this code :slight_smile: