Go Down

Topic: eeprom default state (Read 397 times) previous topic - next topic

bule

HI to everybody,
Here is my problem, When sketch start I load state of four pin from variable stored in eeprom address 0.
My problem is that default state is 255, meaning in binary code its 11111111.
And because its gsm controled relays, when I upload sketch I get all four relays enabled, and thats not what I want.
I want to be by default 240 or 11110000.
Here is my code:
Code: [Select]

#include <EEPROM.h>
#include <GSM.h> // include the GSM library
#define PINNUMBER "" // PIN Number for the SIM
GSM gsmAccess; // initialize the library instances
GSMScanner scannerNetworks;
GSM_SMS sms;
#define gsm 9 // define pin for startup gsm
int addr = 0; // EEPROM address for storing pin state
unsigned char pins;
char remoteNumber[20];
void setup()
{
  // restore the lights
  pins = EEPROM.read (addr);
  for (int i=A0; i<A4; i++) {
    digitalWrite(i, bitRead(pins, i-A0));
  }
  gsmOn(); //power up GSM
  Serial.begin(9600);  // initialize serial communications and wait for port to open:
  //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.print("Signal Strength: "); //
  Serial.print(scannerNetworks.getSignalStrength()); //
  Serial.println(" [0-31]"); //
  Serial.println(F("Waiting for messages")); //
  showFlags(); //
  Serial.println(freeRam());
}

PaulS

Quote
My problem is that default state is 255,

The default value is what it is. There's no sense crying over it.

If you read the value, and it is the default value, you can replace that with whatever value you want.

bule

OK I can change it, but isn't this a chicken-egg problem.
If by default it's 255 and I change it to 240 in setup, and after some time user turn on relay2 (there is power down situation) I save Relay2-ON state to eeprom, and when atmega power on void setup is going to change to 240. And all relays are off again.
So this is my real problem.

ookid

EEPROM will save your state regardless of whether the chip has power. Let me clarify, you don't want the relays to turn on when power is reset but you want them to save the last state they were in before power was cut?

KeithRB

Write a separate script that resets the EEPROM to all zeros. Then load your other script.

bule

SOLVED
I listen advice from KeithRB.
I load sketch example from arduino EEPROM_clear, and after that I load my code and everything work.

Go Up