Ok, so I built a project to read the entries/exits into our school library. It remembers the count and it 11:30PM each night it should save that information....
Well I tested it by changing the clock time and it worked first time. It saved the MM/DD/YY | Count.
So i ran it again, it went to next set of addressing and saved same information.... But wait now the previous count number has change =(
if i write again it does the same thing, the new number is correct and all previous are wrong.....
I tried separating the data with an extra bit and it still did it. I'm not sure if its me or an issue with avr/eeprom.h .
any ideas appreciated.
and here is the code. (this is just the part dealing with writing to eeprom. The entire code is over 40 pages....) Good thing its a simple counter right?
If you want my current full code i can send it to you. only 33kb, ill post pics of it later I have to leave for a meeting.
void saveTheCount(){ // this should only run once per day
if(savecount == 1 && saved != 1){
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
savedAddress = eeprom_read_word((uint16_t*)10); //read address for starting position
// saves in mm/dd/yy|count order
eeprom_write_byte((uint8_t*)savedAddress, month); //eg address 100
eeprom_write_byte((uint8_t*)savedAddress+1, monthDay); //101
eeprom_write_byte((uint8_t*)savedAddress+2, year); //102
eeprom_write_word((uint16_t*)savedAddress+3, counter); //103 and 104 since its a word (2 bytes)
savedAddress = savedAddress + 5; // increment address and prepare to save
if(savedAddress >= 4090){ //rollover to starting address 100
savedAddress = 100;
}
eeprom_write_word((uint16_t*)10, savedAddress);
//lets add offset to second and save to update clock
second = 1 + offset + second; //not sure how long the time interval is from the last read to now so add 1 second,
//since I know this DS1307 is 2-6s off per day :( (tested already)
//this is overkill but ya never know
if(second >= 61){ //just incase seconds ends up at 61+
second = second - 60;
minute = minute + 1; // only runs at around min 30-32 so no need to carry to hours
}
//end overkill
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop oscillator
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
saved = 1; // this way it only runs once per day, after that it is returned
// to 0 a few minutes later after savecount statement is no longer true
}
}
LRC_Restartmegawbackup3.ino (31.9 KB)