Go Down

Topic: How to write a input counter data into EEPROM and retrieve it.? (Read 299 times) previous topic - next topic

brvnbld

Hi, am new to this forum, new to arduino itself. I am programming an input counter . My worry is in case of power failure, I need the date to be retrieved from EEPROM. So I am writing the date every second to the EEPROM. But am not able to read them.



#include <EEPROM.h>

#include "RTClib.h"
RTC_DS1307 RTC;
#include <LiquidCrystal.h>
LiquidCrystal
lcd(12,11,5,4,3,2);
const int inPin = 7;
float count1 = 0.00f;
float count2 = 0.00f;
int s=0;
int inState = 0;
int lastState = 0;
const long interval = 1000;
unsigned long previousTime = 0;
byte seconds;
byte minutes;
byte hours;
byte value;
int address;
//int sizeaddr=0;

void setup() {
  //RTC.adjust(DateTime(__DATE__, __TIME__));
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // put your setup code here, to run once:
 lcd.begin(16,2);
 pinMode(inPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  address=0;
//  sizeaddr=sizeof(count1);
EEPROM.put(address, count1);
//count1 = EEPROM.read(address);
// address = address + sizeaddr;
EEPROM.put(address+sizeof(count1), count2);
//count2 = EEPROM.read(address);


 

  DateTime now = RTC.now();
 seconds= now.second();
 minutes=now.minute();
 hours=now.hour();
if(seconds<30)

  if(seconds==29)
  {
  count2=0;
  lcd.setCursor(2,1);
  lcd.print("     ");
  }
inState = digitalRead(inPin);
if(inState!=lastState)
{
  if(inState == HIGH)
  {
  count1++;
  //while(digitalRead(inPin)==HIGH);
  }
  //delay(2);
}
lastState = inState;
}
if(seconds>30)

  if(seconds==59)
  {
  count1=0;
  lcd.setCursor(2,0);
  lcd.print("     ");
  }
inState = digitalRead(inPin);
if(inState!=lastState)
{
  if(inState == HIGH)
  {
  count2++;
  //while(digitalRead(inPin)==HIGH);
  }
  //delay(2);
}
lastState = inState;
}
lcd.setCursor(0,0);
lcd.print("A:");
EEPROM.get(address, count1);
lcd.print(count1/100);
lcd.setCursor(0,1);
lcd.print("B:");
EEPROM.get(address+sizeof(count1), count2);
lcd.print(count2/100);
lcd.setCursor(9,1);
lcd.setCursor(8,0);
lcd.print(hours);
lcd.print(":");
lcd.print(minutes);
lcd.print(":");
lcd.print(seconds);
}


Budvar10

Hi and welcome to the forum.
First of all, use the code tags for posting the code, please. It is leftmost button in edit window.

Without reading you code, consider that the EEPROM has limited number of writes to ~100000x. Writing the data each second wears the EEPROM rapidly (the day has 86400s), so the EEPROM is absolutely not suitable.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

Budvar10

Look at this:
Code: [Select]
void loop() {
// put your main code here, to run repeatedly:
  address=0;
//  sizeaddr=sizeof(count1);
  EEPROM.put(address, count1);
//count1 = EEPROM.read(address);
// address = address + sizeaddr;
  EEPROM.put(address+sizeof(count1), count2);
//count2 = EEPROM.read(address);

What do you think, how many times the loop() runs in 1 second? This will destroy the cells in the EEPROM in a moment.
Fortunately put() method uses low level update function which writes only changes.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

brvnbld

Yeah buddy. I think this loop will run every second.
Any ways to update the data to EEPROM only on power failure.??
Or may be on power failure , write to the RTC's memory?

mvpolo2k3

in for info, im doing the same for a mileage save feature for a gauge panel

mvpolo2k3

that loop will run how many times per second, its in the tens of thousands unless you have some sort of delay built in.....

Budvar10

Exactly as @mvpolo2k3 wrote. The loop runs many, many times in second. It depends on MCU's clock frequency, loop() length - number of instructions, interrupts, delays, sleep etc.

The DS1307 could be feasible for saving your data. It has 56 bytes of battery backed RAM with unlimited writes.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

sterretje

eeprom.read() reads a single byte; it's  easier to use eeprom.get() which can read any data type (just like eeprom.put() can write any data type).


If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up