Go Down

Topic: How to save data in EEPROM? (Read 1 time) previous topic - next topic

jepoy12

Mar 08, 2013, 05:14 am Last Edit: Mar 10, 2013, 05:47 am by jepoy12 Reason: 1
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?


Code: [Select]
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


Osgeld

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
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

jepoy12

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

Nick Gammon

http://www.gammon.com.au/electronics

Osgeld

http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

alexander_senseo



how many numbers I can store in each address?

One.


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?

gnusso

you can take this example:

Code: [Select]

/*
* 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

Nick Gammon




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.


Well, the question was: how many numbers can I store in each address?
http://www.gammon.com.au/electronics

kowalski

#8
Mar 10, 2013, 02:01 pm Last Edit: Mar 10, 2013, 02:51 pm by kowalski Reason: 1
If I understand the problem correctly this is what I would do:
Code: [Select]

#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: http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html#gac5c2be42eb170be7a26fe8b7cce4bc4d 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.
Code: [Select]

eeprom_read_block(num_rcv, num_save, NUM_MAX);

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

Cheers!

jepoy12

#9
Mar 11, 2013, 01:45 am Last Edit: Mar 11, 2013, 01:51 am by jepoy12 Reason: 1
Thanks everyone..+1 karma to all. : )
is this correct?

Code: [Select]

#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
         }
     }
 }
}

Go Up