Only the variable definitions go before setup, the rest goes where ever you'r going to use it.
Maybe do the current time read and the RAM read once in setup, decide if you need to do store something there before getting into loop?
Thanks, yes I got it going like that, now I am doing my manipulations ( and seem to have reset everything to 0 but it doesnt matter, its actually coming right at last !
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
byte date_month;
byte month ;
byte year;
byte prevdate ;
byte prevmonth ;
byte prevyear ;
byte prevbest ;
int trigger = 7;
void setup () {
Wire.begin(); // no address, we are master
Serial.begin (9600);
pinMode ( trigger, INPUT );
digitalWrite ( trigger, HIGH); // sets pin 7 as trigger with pullup
// update real time at bootup Read the day of month, month, year
// Reset the register pointer
Wire.beginTransmission(0x68);
Wire.send(0x04); // to day of the month
Wire.endTransmission();
Wire.requestFrom(0x68,3 );
// seconds = Wire.receive();
// minutes = Wire.receive();
// hours = Wire.receive();
// day_week = Wire.receive();
date_month = Wire.receive(); // sends back current month
month = Wire.receive(); // and this
year = Wire.receive(); // and this
// sqwe = Wire.receive();
Serial.print ("bootup date_month " );
Serial.println (date_month, HEX);
Serial.print ("bootup month " );
Serial.println (month, HEX);
Serial.print ("bootup year " );
Serial.println (year, HEX);
}
void loop () {
// ****************************************
if ( trigger == LOW ) { checkdate () ;}
// **********************************************************************
// Read the first 4 bytes of RAM
// Reset the register pointer
Wire.beginTransmission(0x68);
Wire.send(0x08); // to start of RAM
Wire.endTransmission();
Wire.requestFrom(0x68,4 ); // Read back first four RAM addresses
prevdate = Wire.receive(); // or whatever you decide to the data
prevmonth = Wire.receive();
prevyear= Wire.receive();
prevbest = Wire.receive();
Serial.print ("prev date " );
Serial.println (prevdate, HEX);
Serial.print ("prev month " );
Serial.println (prevmonth, HEX);
Serial.print ("prev year year " );
Serial.println (prevyear, HEX);
Serial.print ("prev best " );
Serial.println (prevbest, HEX);
// ****************************************************************************
// Write the first 4 bytes of RAM
// Reset the register pointer
Wire.beginTransmission(0x68);
Wire.send(0x08); // to start of RAM
Wire.send (prevdate); // or whatver you decide to call the data
Wire.send (prevmonth);
Wire.send (prevyear);
Wire.send (prevbest);
Wire.endTransmission();
} // end loop
void checkdate () {
// Read the day of month, month, year
// Reset the register pointer
Wire.beginTransmission(0x68);
Wire.send(0x04); // to day of the month
Wire.endTransmission();
Wire.requestFrom(0x68,3 );
date_month = Wire.receive(); // sends back current month
month = Wire.receive(); // and this
year = Wire.receive(); // and this
Serial.print ("todays date_month " );
Serial.println (date_month, HEX);
Serial.print ("todays month " );
Serial.println (month, HEX);
Serial.print ("todays year " );
Serial.println (year, HEX);
} // end checkdate function
OK I have my event logger basically running, but I now have to calculate the number of days since the last event. ( max 99 )
Without using a lookup table for months days and leap years etc, if I can find how to convert the previous event date to unix time, I could simply subtract it from the current unix time to give me elapsed seconds, and divide by 606024
I can get the current unix time with the time.h library with time_t now() , but I cannot see how to give it another date to calculate from?
Any ideas ?
I could go back to my original idea of splitting the unix code for the previous event, into bytes and saving them in the RTC ram, but its a big number ( 10 digits decimal )
I could get the unix time, convert it to days, and subtract most of the days to make say Aug 1 2011 the datum , then I will have more manageable number to play with for many years to come.....
I could go back to my original idea of splitting the unix code for the previous event, into bytes and saving them in the RTC ram, but its a big number ( 10 digits decimal )
The number of characters used when printed out is irrelevant. The unix time fits in a long, which is 4 bytes.
Thanks Bob,, that's been superseded by Time.h which I tried, but it didnt like hours and minutes, so I think I must just rename them in the sample you gave me, and I should be rolling.....
I am back on the RTC project again, and am having trouble writing to the DS1307s RAM, I have tried a basic sketch just to store and retreive some data ( below )
Am I doing something stupid here? It just gives me the old data back from the RAM
/* just loading the ram for testing
*/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
byte prevday ; // prev days of month for display
byte prevmonth ; //prev month for display
byte prevyear ; //prev year for display
byte prevbest ; // highest longest interval up to 99
byte upperbyte; // of previous unix days
byte lowerbyte; // -"-
unsigned long old_unix_days;
unsigned long unidays;
unsigned long unisecs;
//*************************************************************************************
void setup() {
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
Wire.begin(); // no address, we are master
Serial.begin (9600);
}
void loop()
{
prevday = 12;
prevmonth= 10;
prevyear = 33;
prevbest = 87;
upperbyte = 22;
lowerbyte = 44;
Wire.beginTransmission(0x68);
Wire.send(0x08); // to start of RAM
Wire.endTransmission();
Wire.send (prevday);
Wire.send (prevmonth);
Wire.send (prevyear);
Wire.send (prevbest);
Wire.send (upperbyte);
Wire.send (lowerbyte);
Wire.endTransmission();
Serial.println("finished loading");
Wire.beginTransmission(0x68);
Wire.send(0x08); // to start of RAM
Wire.endTransmission();
Wire.requestFrom(0x68,6 ); // Read back first six RAM addresses
prevday = Wire.receive();
prevmonth = Wire.receive();
prevyear= Wire.receive();
prevbest = Wire.receive();
upperbyte = Wire.receive();
lowerbyte = Wire.receive();
Serial.print ("prev day read " );
Serial.println (prevday, HEX);
Serial.print ("prev month read " );
Serial.println (prevmonth, HEX);
Serial.print ("prev year read " );
Serial.println (prevyear, HEX);
Serial.print ("prev best read " );
Serial.println (prevbest, HEX);
Serial.print ("upperbyte read " ); Serial.print (upperbyte, HEX); Serial.print (" , upperbyte read " ); Serial.println (upperbyte, HEX);
Serial.print ("lowerbyte read " ); Serial.print (lowerbyte, HEX); Serial.print (", lowerbyte read " ); Serial.println (lowerbyte, BIN);
long old_unix_days = word (upperbyte, lowerbyte);
Serial.print(" old_unix_days = "); Serial.print (old_unix_days); Serial.print(", old_unix_days = "); Serial.println(old_unix_days, BIN);
}
I blame it on being hard to read the screen after 3 days testing the previous project with 1000 ultra bright white LEDs I could literally switch off the rooms normal lights without seeing any difference - depending on the numbers being displayed of course !
True Bob, I do have prescription glasses that darken in sunlight, I forgot to test them on the white LEDs, I must check that.
On a serious note, one of my suppliers has a display with all the different colour LEDs on all day, including the blue ones that I use for illuminating flourescent targets,( they have so much ultra violet content.)
I have warned him about damage to his counterstaffs' eyes, I temporary ( 2 hours or so ) lost the sight of one eye a couple of years back when experimenting with a couple of ultraviolet LEDs, so I am very wary now.