Arduino Forum

Using Arduino => Storage => Topic started by: bule on Dec 10, 2013, 09:26 pm

Title: Where to put EEPROM.write to save memory cycle
Post by: bule on Dec 10, 2013, 09:26 pm
Hi guys,
First of all I wrote a program with gsm shield to turn on and off 4 relays.
It's working ok but I want to add storing relay state so if there is a power fail all relay can turn back in previous state.
So I put eeprom.read at first palce in loop(), but I am afraid if I put eeprom.write at the end of loop after function (recieve sms) end it will soon reach maximum eeprom write.
Is it true or I am wrong.
Title: Re: Where to put EEPROM.write to save memory cycle
Post by: CrossRoads on Dec 10, 2013, 09:30 pm
If you are writing to EEPROM every pass thru loop, then yes, you will wear it out quickly.

If you store it only when a relay changes state, that will help a lot.
If you use a different address for each relay, that will help to extend the number of writes that can be achieved.
Title: Re: Where to put EEPROM.write to save memory cycle
Post by: bule on Dec 10, 2013, 09:33 pm
Do you think is better to put eeprom.writes in function for every relay or for all at once?
Title: Re: Where to put EEPROM.write to save memory cycle
Post by: CrossRoads on Dec 10, 2013, 09:47 pm
Takes 3.3mS to write an EEPROM address - for check pointing purposes,  I would store all 4 at once.
How often are you writing? There are "wear leveling" schemes you can follow to spread the writes over many addresses so you do not just wear 1 address out.

This application note discusses wear leveling in Flash, the same principals apply to EEPROM as well.
Title: Re: Where to put EEPROM.write to save memory cycle
Post by: nickgammon on Dec 10, 2013, 10:19 pm

Do you think is better to put eeprom.writes in function for every relay or for all at once?


If you only write when the state changes (eg. every half hour) then it will take a long time to wear out.

If you space the address for each relay apart by 4 bytes then each relay will be in a different "page" of EEPROM.
Title: Re: Where to put EEPROM.write to save memory cycle
Post by: bule on Dec 10, 2013, 10:36 pm
Here is my code:
Code: [Select]
#include <EEPROM.h>
// include the GSM library
#include <GSM.h>
// PIN Number for the SIM
#define PINNUMBER ""
// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;
#define gsm 9
int addr = 0;
unsigned char pins = 0;
void setup()
{
  gsmOn();
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.println(F("SMS Messages Receiver"));
  // connection state
  boolean notConnected = true;

  // Start GSM connection
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println(F("Not connected"));
      delay(1000);
    }
  }
  sms.flush();  // delete all messages
  Serial.println(F("GSM initialized"));
  Serial.println(F("Waiting for messages"));
}
void loop()
{
  // If there are any SMSs available()
  if (sms.available())
  {
    RelaySMS();
    sms.flush();
    //Serial.println("MESSAGE DELETED");
  }
  delay(1000);
}
void gsmOn()
{
  // Takes 30 seconds to complete
  digitalWrite(gsm, HIGH);   // turn the Phone on
  delay(1000);
  digitalWrite(gsm, LOW);
  delay(5000);
}
void RelaySMS()
{
  char c;
  c=sms.read();         // procitaj sms
  if (c=='1')         // ako je prvi znak 1 uradi sledece:
  {
    txt(A3,A3);
  }
  else if (c=='2')
  {
    txt(A2,A2);
  }
  else if (c=='3')
  {
    txt(A1,A1);
  }
  else if (c=='4')
  {
    txt(A0,A0);
  }
}
/*void allstate()
{
String txtmsg = "";
while(c=sms.read())
txtmsg += c;
//Serial.println("Sadrzaj sms-a:");
//Serial.println(txtmsg);
if (txtmsg.compareTo("status") == 0)
{
pinstate = digitalRead(A3);
sms.remoteNumber(remoteNumber, 10);
sms.beginSMS(remoteNumber);
sms.print(pinstate);
sms.endSMS();
}
}*/
void txt(int Relay, int pin)
{
  pinMode(Relay, OUTPUT);
  String txtmsg = "";
  int pinstate;
  char c;
  char remoteNumber[10];
  while(c=sms.read())
    txtmsg += c;
  Serial.println(F("Sadrzaj sms-a:"));
  Serial.println(txtmsg);
  if (txtmsg.compareTo("on") == 0)
  {
    digitalWrite(Relay, HIGH);
  }
  else if (txtmsg.compareTo("off") == 0)
  {
    digitalWrite(Relay, LOW);
  }
  else if (txtmsg.compareTo("status") == 0)
  {
    pinstate = digitalRead(pin);
    sms.remoteNumber(remoteNumber, 10);
    sms.beginSMS(remoteNumber);
    sms.print(pinstate);
    sms.endSMS();
  }
}
Title: Re: Where to put EEPROM.write to save memory cycle
Post by: bule on Dec 10, 2013, 10:43 pm
I think I know how to put eeprom.write in void txt () but how to restore all four pins in Void setup () so in case of power loss i can restore to previous state.
Relays are used for starting up machines or heat system in house.
Title: Re: Where to put EEPROM.write to save memory cycle
Post by: kf2qd on Dec 10, 2013, 11:46 pm
Returning to previous state can be problematic. Are these only controlling things that can't get people caught it them? WHat if conditions have changed and the previous state is not needed?
Title: Re: Where to put EEPROM.write to save memory cycle
Post by: bule on Dec 11, 2013, 07:33 pm
I am putting these option only if there is very short power failing.
For example, man sand sms to turn off water pump, it's not my problem if he forget to turn it off. My only worry is what if he turn on heat pump and shortly after that there is power fail only on second. The heat pump will stay off and he wouldnt know about it.
I dont want to put electronic for power loss detection.