This is a simple framework you can drop your data collection into.
BUT it uses the DS1307 RTC library and the Time library.
Here is the sort of issue you might face....
I want to read the Analog Sensor Values or maybe a BMA180 accelerometer or maybe both (like I do)-- but every minute I want to collect the Air Pressure and the Temperature and the altitude. (Why not eh? )
I don't need to read the air pressure more often -- it does not change that fast -- at least not for my purposes of determining sound/vibration velocity.
So how do I do that? Try the code below!
When you run it it will synch the RTC clock to the time library and then start cycling. It will print out what it is doing to help you trace along. Then, every minute it stalls while it talks to that "other routine" where it does the "slow stuff".
I could have used an alarm function from the library -- or a timer and an interrupt -- but I wanted something simple and obvious.
Newbie question: (For extra points) Why did I NOT check to see if the time was increasing by one all the time? --- and then trip the additional data collection routine --- Would that have worked?
I don't think it depends on any of the extensions I added to Time and the DS1307 library.
It does require that you have set the clock as a separate issue -- say using the NNTP settime routine that comes with the DS1307 lib. I just programmed it to synch to a DS1307 that already was running and had the correct time set!
There are no great expositions of programming skill here -- just a routine that may be useful to a few people.
/*
By WillR
A Template for getting some use out of the DS1307 Clock
It uses the Time and the DS1307 libraries
It simply allows a routine to be called whenever the minute value changes
Nothing fancy. Just one way of doing it...
Take out the delays when you collect data. *******************
Delays often interfere with interrupts and timers. ***********
March 25, 2011
*/
// check minute...
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
int myMinute1;
int myMinute2;
int myFactor;
int SetFlag;
void setup(){
Serial.begin(9600);
Serial.flush();
Serial.println(); // give me a clean line to start logging
// The RTC was set previously via the NNTP routine... So I will just SYNCH!
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");
// time adjustment
myFactor = 0;
adjustTime(myFactor);
setSyncInterval(3600);
//show the time
digitalClockDisplay();
delay(1000);
// finished with the clock...
myMinute1 = minute();
Serial.print(" Minute is: ");
Serial.println (myMinute1);
Serial.println();
SetFlag = 0;
}//end setup
void loop() {
// the next routine gets the data that you wish to read as fast as possible
// Perhaps it is a routine that is called only when you interrupt.
Serial.println(" ****** Cycle Start *****");
ReadMyUsualData();
// Inside the next routine you can go off and read the data that is "low speed"
// or that you only want to read every minute
// every time through the loop that the minute value changes -- you call the
// low speed routine.
if (minute() != myMinute1)
{
myMinute1 = minute();
Serial.print(" The Minute is: ");
Serial.println(myMinute1);
// update barometer -- call read function
readmyotherData();
/* If you wish, you could send the data to your PC via a separate routine here.
Maybe it should be a different format or a different port.
Then you do not need the "pack the additional data area".
*/
} //end -- if minute!=
SendAllTheData();
Serial.println(" ********* Cycle End *****");
Serial.println();
delay(1000);
} //end main loop
//***************
void ReadMyUsualData() {
// read the analog sensors
Serial.println (" Read a BMA180 Here -------->");
// Call my BMA180 routines etc...
} // end ReadMyUsualData
void readmyotherData(void){
// read my data here
SetFlag =1;
Serial.println(" Read my Barometer here ------------------------->");
delay(3000);
} // end ReadMyOtherData
void SendAllTheData(void){
//pack the usual data -- get it ready to send
Serial.println(" Packing up the NORMAL data ... ");
//check for some additional data before sending
if (SetFlag == 1) {
Serial.println(" Packing up the **additional** data ++++++++++");
// put the addtional data in the buffer
SetFlag = 0;
delay(5000);
}
//now you can send the buffer -- UDP Serial or store it on the SD card...
// Complete with your additional once-a-minute reading...
Serial.println(" SENDING ALL the data -- ");
} //end SendAllTheData
// Clock Functions
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}