Go Down

Topic: About EEPROM (Read 302 times) previous topic - next topic

Setsuna

Hello this is my first time to use EEProm in this project I searched a lot of example in the internet only I know how to read and write eeprom block here is the code.

Code: [Select]

#include <EEPROM.h>
#include <GPRS_Shield_Arduino.h>
#include <sim900.h>
#include <SoftwareSerial.h>

SoftwareSerial gpsSerial(12, 13);
GPRS gsm(10, 11, 9600);

int messageIndex = 0;//inbox
char phone[16]; //Phone number from sender
char datetime[24]; // date and time
#define MESSAGE_LENGTH 160
char message[MESSAGE_LENGTH];
char text[160];

#define NUM_MAX 20
char number[NUM_MAX];
char number2[NUM_MAX];
char EEMEM save[NUM_MAX];


void setup() {
  while (!gsm.init()) { //Initialize GPRS module including SIM card
    delay(1000);
    Serial.print(F("INIT ERROR\r\n")); //A message when the GSM is not connected properly
  }
  delay(3000);
  Serial.println(F("INITIALIZE SUCCESS!.."));
}

void loop()
{
  gsm.listen();

  if (gsm.isListening())
  {
    Serial.println("   ");
    Serial.println("   ");
    messageIndex = gsm.isSMSunread();
    if (messageIndex > 0)
      if (messageIndex > 0)
      {
        gsm.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);
        Serial.println(phone);
        Serial.println(datetime);
        Serial.println(message);
        if (strcmp(message, "Register $$$") == 0)
          gsm.listen();
        if (gsm.isListening())
        { gsm.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);
          Serial.println(phone);
          strcpy(number, phone);
          eeprom_write_block(number, save, NUM_MAX);
          eeprom_read_block(number2, save, NUM_MAX);
          Serial.println(number2);
          gsm.sendSMS(number2, "Succesfully Registered");
          Serial.println("Successfully Registered");
          delay(500);
          gsm.deleteSMS(messageIndex);
        }
      }
  }
}


Now I want to know is to how to clear the saved data, won't allow the user to store data while it is already occupied and how to store another phone number.

BulldogLowell

Now I want to know is to how to clear the saved data, won't allow the user to store data while it is already occupied and how to store another phone number.
There is a place to learn about the  EEPROM library.

septillion

Think to know is, there is always data in EEPROM. No matter if you write to it or not. So "clearing" it would only mean to set it back to a known value. The default for a EEPROM address is 0xFF. So even if you didn't write anything to EEPROM you will read 0xFF back.

So if you want to know is there is, what you call "valid data" you need to store in EEPROM somewhere that you indeed wrote something to EEPROM by writing a non default value to it somewhere. So next time you read that and treat the rest of the info as valid data or occupied the moment you see that that particular address is the non-default value.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

econjack

I've not worked with a gsm object, but if phone already holds the number, why are you copying it into number?

If your goal is to track phone numbers, why not use the first two bytes of EEPROM as an int to hold the number of phone numbers that have been stored? In setup(), you would read those first 2 bytes and form them into an int and assign it to a variable (e.g., numbersSaved). If each number is 20 bytes long, then you can read the first two bytes and use that as an index where the new number is written. Then you could do something like:

Code: [Select]


char savedNumber[NUM_MAX];
// more code...

   EEPROM.put(numberSaved * NUM_MAX + sizeof(int), savedNumber);



when there are no numbers yet stored, the expression above become:

   EEPROM.put(0 * NUM_MAX + 2, savedNumber);

which writes the first phone number at the position 2 (i.e., the third byte) in EEPROM. (Bytes 0 and 1 hold numberSaved.) You can use the same scheme to retrieve the numbers back from EEPROM. Just remember to update numberSaved after each new phone number is stored.

There are dozens of other ways to do this, but this one is pretty simple.


Setsuna

I just got his idea, so I think there are simple way to do that
http://forum.arduino.cc/index.php?topic=225499.0

septillion

Using avr/EEPROM is the hard way considering you're using the Arduino environment... And all we suggested isn't hard. Like I said, every EEPROM already has a default value and you can't really clear it because there is no difference between "empty" (or NULL) and 0 (zero)...
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

CrossRoads

"every EEPROM already has a default value "
Unwritten EEPROM in a virgin chip will have a value of 255 when read back and until changed.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up