How to save data in EEPROM?

since im using the RAM here every time I turn off my arduino the number stored in the num_save will be wipe. so I need to text again just to store my number at the num_save. I want to store it in the Flash memory so that It will not be wipe after I turn off my arduino. I dont really understand how PROGMEM works. Should I just put the word PROGMEM next to the variable name? would that store the number?

char num_rcv [20];
char num_save [20];


 gsm.readSMS(smsdata, 160, num_rcv, 20)    //receiving the number

strcpy( num_save, num_rcv );        // after receiving, store the num_rcv to the num_save

you can only save data in progmem when you upload the program, not whenever

http://www.arduino.cc/en/Reference/PROGMEM

what you want is eeprom

http://arduino.cc/en/Reference/EEPROM

how many numbers I can store in each address? I want to save atleast 20 of character arrays.

jepoy12:
how many numbers I can store in each address?

One.

valued 0-255

[quote author=Nick Gammon link=topic=152977.msg1150093#msg1150093 date=1362892639]

jepoy12: how many numbers I can store in each address?

One. [/quote]

There are of course 1kByte of data that can bestored therefore then. Each being a Byte can store of course 0-255 of data.

so saying one is absolutely correct but might be slightly misleading to the quick reader. It's one per address and there are 1k addresses.

I remember that there is some function that helps you to store arbitrary data in the EEPROM overcoming the limitation to save only 1 byte at a time. Maybe some helpful and knowing perons can give this info here to you.

what are you doing that you wana store data there?

you can take this example:

/*
 * EEPROM Write
 *
 * Stores values read from analog input 0 into the EEPROM.
 * These values will stay in the EEPROM when the board is
 * turned off and may be retrieved later by another sketch.
 */

#include <EEPROM.h>

// the current address in the EEPROM (i.e. which byte
// we're going to write to next)
int addr = 0;

void setup()
{
}

void loop()
{
  // need to divide by 4 because analog inputs range from
  // 0 to 1023 and each byte of the EEPROM can only hold a
  // value from 0 to 255.
  int val = analogRead(0) / 4;
  
  // write the value to the appropriate byte of the EEPROM.
  // these values will remain there when the board is
  // turned off.
  EEPROM.write(addr, val);
  
  // advance to the next address.  there are 512 bytes in 
  // the EEPROM, so go back to 0 when we hit 512.
  addr = addr + 1;
  if (addr == 512)
    addr = 0;
  
  delay(100);
}

http://arduino.cc/en/Reference/EEPROM

regards,
gnux

alexander_senseo:

[quote author=Nick Gammon link=topic=152977.msg1150093#msg1150093 date=1362892639]

jepoy12:
how many numbers I can store in each address?

One.

so saying one is absolutely correct but might be slightly misleading to the quick reader.
It’s one per address and there are 1k addresses.
[/quote]

Well, the question was: how many numbers can I store in each address?

If I understand the problem correctly this is what I would do:

#include <avr/eeprom.h>

#define NUM_MAX 20
char num_rcv [NUM_MAX];
char num_save [NUM_MAX] EEMEM; // You could have an initial value here

gsm.readSMS(smsdata, 160, num_rcv, NUM_MAX);
eeprom_write_block(num_save, num_rcv, NUM_MAX);

More details on the AVR GCC online manual: avr-libc: <avr/eeprom.h>: EEPROM handling and this tutorial is great: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=38417

I guess that you will also need to modify your startup to load the number saved in num_save back to num_rcv.

eeprom_read_block(num_rcv, num_save, NUM_MAX);

Data stored in EEPROM needs to be read with the special functions.

Cheers!

Thanks everyone…+1 karma to all. : )
is this correct?

#include <SIM900.h>
#include <sms.h>
SMSGSM sms;
#include <avr/eeprom.h>
#define NUM_MAX 20


char  reg_num[NUM_MAX];        // for reading
char  save[NUM_MAX];               //store the number receive here
const char passRx[] = "Regpass#";     //password for registering
char  storage[160];                 //message receive storage
char  smsdata[160];                //message receive
char  EEMEM numberRx[NUM_MAX];               //number receive


void checkSMS()
{ 
  if(gsm.readSMS(smsdata, 160, numberRx, 20))     //read the sms 
  {
    if(strncmp (smsdata, passRx, 8) == 0)               //if password password is correct
    {
    sms.SendSMS(numberRx, "Registered");              // send reply
    eeprom_write_block(save, numberRx, NUM_MAX);                  //save it on EEPROM
    }

eeprom_read_block(reg_num, save, NUM_MAX);        //read the stored data

   else if (strncmp (numberRx, regnum, 20) == 0)           //if it is equal 
      {
        if (strlen(smsdata) < 151)                                   //check it if greater than 151
        {
          sms.SendSMS(numberRx, "Message received");      //send reply
          strcpy( storage, smsdata );                               //store the message 
        }
        else                                                                 //if greater
        {
       sms.SendSMS(numberRx, "Max char reached");        //send reply
          }
      }
  }
}