LowPowerLab Forum

Hardware support => Moteino => Topic started by: G550_Pilot on May 26, 2016, 12:24:31 AM

Title: PulseMote.ino Question
Post by: G550_Pilot on May 26, 2016, 12:24:31 AM
This is not specific to the PulseMote but since that is the sketch that I started with I was curious about something.

Basically the sketch writes the current water meter pulses to the EEPROM and then reads it back. In all of my testing to get it working correctly, I now have something like 5M gallons showing up...how can I reset that number to my actual meter reading (based on pulses)?

Thanks
Title: Re: PulseMote.ino Question
Post by: Felix on May 26, 2016, 10:34:55 AM
When EEPROM is virgin from factory it probably is all 1s hence it maxes out anything you read from it.
So you can either write your first struct to the eeprom as all 0s or just initialize the whole eeprom by writing 0s.
Something like:

for (int addr=0; addr <=1023; addr++)
  EEPROM.write(addr, 0);


---------------------

Actually if you think about it... the first time we read an max(unsigned long) from EEPROM. As soon as your counter increases, it will overflow to 0, so you will loose a pulse but the sketch "self-heals" or basically initializes the counter at that point. I remember this happening to me as well, but I didn't make any changes to the sketch since the sketch cannot know that it's running for the first time, unless we make an assumption that with every power cycle we reset the EEPROM which actually defeats the purpose of saving to EEPROM in the first place (ie we could just keep a RAM counter and simplify the sketch in that case).

So bottom line, you don't have to do anything, just issue a pulse or let a pulse happen and let the sketch save it back to EEPROM, that first save will go from 5M to 0.
Title: Re: PulseMote.ino Question
Post by: G550_Pilot on May 28, 2016, 12:20:38 PM
Hi Felix -

That is exactly what it did, it reset to zero eventually, but it took quite a bit more than one or two reboots. What I would like to do is actually set it to the physical meter reading so that the value in the eeprom matches exactly what my meter reads.

Is there anyway to do a one time write of a specific value where it stores the pulsecount?

Thanks
Title: Re: PulseMote.ino Question
Post by: Felix on May 29, 2016, 08:04:40 PM
Yes. Use the following function from the eepromex lib (http://playground.arduino.cc/Code/EEPROMex):

bool writeLong(int address, uint32_t value);
Title: Re: PulseMote.ino Question
Post by: G550_Pilot on May 29, 2016, 11:32:44 PM
Thanks !!