Time Loader and Adjuster

Le Fri 27 December 2013

The following is a successful component of the system that loads the dates from an SD Card. Displays the loaded dates and then updates the weekend to being the next from from now. The example results shown below were generated on the 27th of December 2013 just before 2:00pm. The Input file looked like this:

1386347400,1386563400,1387548000,1389155400,1389967200,1390278600,1397743200,1398141000,1398348000,1398659400,1401458400,1401769800,1414159200,1414470600

At this stage the number of entries is set by the Variable NUMBER_OF_FIELDS = 14 which we will look to load off the SD Card to make it more flexible. It will instead be maximised at 10 holiday periods at this stage. That should cover just over one year's worth.

Related Pages

The extra libraries required for this sketch are:

You can also download the libraries from the following webpage: A Collection of Arduino Libraries Used in This Project.

The sketch below can be downloaded from here: SDFileContentLoader2.ino

/*
This Project is designed to load the content of a CSV file
into an Array ready for processing.
It combines two coding examples;one to read the CSV off the
SD card and the other to load the content into an array
of numbers.
The Method for breaking up the CSV text stream into an array of numbers
is based on one designed for older Arduino ~IDEs, and is a bit more
understandable.
This code expects a message in the form: 12,345,241 etc with a newline at the end
The sketch has been modified from the original by using an unsigned long
for the array. This allows us to successfully handle the very large
numbers that will be required for the dates and times.

Once loaded the array contents are analysed and adjusted to meet a condition -
in this case the next date after the present. This will form the basis for the
"catch up" phase of the program.  The resulting date will be displayed.


*/

#include <SD.h>
#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <TimeAlarms.h> // this library will allow us to set alarms - ultimately these alarms will be read from the SD Card


// Note that even if it's not used as the CS pin, the hardware
// CS pin (10 on most Arduino boards,
// must be left as an output or the SD library functions will
// not work.  In the Sparkfun SD Shield the CS is on
// Arduino Pin 8.
const int chipSelect = 8;
const int NUMBER_OF_FIELDS = 14;  //How many comma separated fields are expected.
int fieldIndex = 0;  //The current field being received
unsigned long values[NUMBER_OF_FIELDS];  //This is the array to hold the values.
unsigned long Nowtime = 0;  //This will hold the current time

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  setSyncInterval(320543);    // set the interval to resynchronise the internal clock
                              // this is set toabout 3.7ish days.
  if(timeStatus()!= timeSet)  // this makes sure the RTC service is going 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect))
  {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  File dataFile = SD.open("times.csv");

  // if the file is available, read from it:
  if (dataFile)
  {
    while (dataFile.available())
    {
      char ch = dataFile.read();    //read the SD file content to a variable
      if (ch >= '0' && ch <= '9')  //is this an ASCII digit between 0 and 9?
      {
      //yes, accumulate the value if the fieldIndex is within range
      //additional fields are not stored.
        if(fieldIndex<NUMBER_OF_FIELDS)
        {
          values[fieldIndex]=(values[fieldIndex]*10)+(ch-'0');  //
        }
      }
      else if(ch==',')
      {  //If the character is a comma continue to the next field
        fieldIndex++;  //increment the fieldIndex
      }
      else
      {
       //any character other than a digit or a comma end the capture of fields
       //in this example new line character sent by the Serial Monitor
       //will stop the capture process.
       //Print each of the stored numbers from the array
        for(int i=0;i<min(NUMBER_OF_FIELDS,fieldIndex+1);i++)
        {
          Serial.println(values[i]);
        }
      }
    }
    dataFile.close();
  }

  // if the file isn't open, pop up an error:
  else
  {
    Serial.println("error opening times.csv");
  }
  Nowtime = now(); //This pulls the current time off the system which is hopefully synced to the RTC.
  Serial.println("Time rightnow");
  Serial.println(Nowtime);
  Serial.println("Original alarms times");
  Serial.println(values[0]);
  Serial.println(values[1]);
  while(values[0]<Nowtime)
  {
    values[0]=values[0]+604800;  //Add a week to the number (604800 seconds)
    values[1]=values[1]+604800;
  }
  Serial.println("Updated alarms times");
  Serial.println(values[0]);
  Serial.println(values[1]);

}

void loop()
{
}

Output

Output from the Sketch looks like the following (first column only -the second column is to decipher the Unix Time codes)

1386347400  Friday, 6 December 2013 16:30:00
1386563400  Monday, 9 December 2013 04:30:00
1387548000  Friday, 20 December 2013 14:00:00
1389155400  Wednesday, 8 January 2014 04:30:00
1389967200  Friday, 17 January 2014 14:00:00
1390278600  Tuesday, 21 January 2014 04:30:00
1397743200  Thursday, 17 April 2014 14:00:00
1398141000  Tuesday, 22 April 2014 04:30:00
1398348000  Thursday, 24 April 2014 14:00:00
1398659400  Monday, 28 April 2014 04:30:00
1401458400  Friday, 30 May 2014 14:00:00
1401769800  Tuesday, 3 June 2014 04:30:00
1414159200  Friday, 24 October 2014 14:00:00
1414470600  Tuesday, 28 October 2014 04:30:00
1386347400  Friday, 6 December 2013 16:30:00
1386563400  Monday, 9 December 2013 04:30:00
1387548000  Friday, 20 December 2013 14:00:00
1389155400  Wednesday, 8 January 2014 04:30:00
1389967200  Friday, 17 January 2014 14:00:00
1390278600  Tuesday, 21 January 2014 04:30:00
1397743200  Thursday, 17 April 2014 14:00:00
1398141000  Tuesday, 22 April 2014 04:30:00
1398348000  Thursday, 24 April 2014 14:00:00
1398659400  Monday, 28 April 2014 04:30:00
1401458400  Friday, 30 May 2014 14:00:00
1401769800  Tuesday, 3 June 2014 04:30:00
1414159200  Friday, 24 October 2014 14:00:00
1414470600  Tuesday, 28 October 2014 04:30:00
Time rightnow   
1388152815  Friday, 27 December 2013 14:00:15
Original alarms times   
1386347400  Friday, 6 December 2013 16:30:00
1386563400  Monday, 9 December 2013 04:30:00
Updated alarms times    
1388161800  Friday, 27 December 2013 16:30:00
1388377800  Monday, 30 December 2013 04:30:00

Par Hamish Trolove, Cat├ęgorie : Tech Projects

Tags : Arduino / CSV /