Loading CSV Off SD Card and Updating Times

Le Fri 27 December 2013

This sketch is a major step along the way for the Timer Project. It loads the data off the SD Card into an array that varies depending on the number of dates that the user has bothered to supply. It then looks at the time and jumps the weekend array components forward to the next one from now.

This is built upon the codes developed here;

Beyond this the planned steps are to get it to actuate things.

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: SDFileContentLoader3.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 uses the parseInt function
that Arduino 1.0 introduces.  This has the advantage of being
less demanding on Arduino memory and also getting around
some of the weird things that the other method experienced.

This code expects a message in the form: 12,345,241 etc
The sketch has been modified from the original by using an unsigned long
for the array. This allows usto 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;
int fieldIndex = 0;  //The current field being received
unsigned long Nowtime = 0;  //This will hold the current time

void setup()
 // Open serial communications and wait for port to open:

  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");
     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:
  Serial.println("card initialized.");

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

    // if the file is available, read from it:
  if (dataFile)
  // In order to correctly size the array the first two digits
  // off the file will be read as the Number of Fields to use.
  // NoF is the number of comma separated fields we expected.

    char ch = dataFile.read();  // Read the first entry - tens
    int NoF = 10 * (ch-'0');    //Change ASCII digit to a tens digit
    ch = dataFile.read();    // Read the second entry
    NoF = NoF + ch-'0';    // Add the ones digit to the tens digit
    ch = dataFile.read(); // read the next figure which will be a comma, but ignore it.
    unsigned long values[NoF];  //This is the array to hold the values.
    Serial.print(" Number of Fields Expected ");

    if (dataFile.available())
      for(fieldIndex = 0; fieldIndex < NoF; fieldIndex ++)
        values[fieldIndex]=dataFile.parseInt(); //get a numerical value
      Serial.println(" Fields recieved:");
      for(int i = 0; i < fieldIndex; i++)


    Nowtime = now(); //This pulls the current time off the system which is hopefully synced to the RTC.
    Serial.println("Time rightnow");
    Serial.println("Original alarms times");
      values[0]=values[0]+604800;  //Add a week to the number (604800 seconds)
    Serial.println("Updated alarms times");


  // if the file isn't open, pop up an error:
    Serial.println("error opening timesv2.csv");


void loop()

Input File Content


Output from Serial Monitor

RTC has set the system time
Initializing SD card...card initialized.
 Number of Fields Expected 14
14 Fields recieved:
Time rightnow
Original alarms times
Updated alarms times

Par Hamish Trolove, Cat├ęgorie : Tech Projects

Tags : Arduino / CSV /