CSV Array Loader

Le Sun 27 October 2013

The following sketch successfully loads an array of dates from an SD Card and is able to handle variable numbers of input fields. It reads the first two digits off the SD Card string and uses this as the Number of Fields that it expects for the array. Using the ParseInt() function saves on-board memory for the Arduino compared to the Breaking up CSVsexample.

The sketch below can be downloaded from here: SDFileLoader_VarCSV2.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 with a newline at the end
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.

*/

#include <SD.h>

// 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

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



  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("timesfic.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.println(NoF);


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

    dataFile.close();
  }

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

void loop()
{
}

Input file

22,1386347400,1386563400,1387548000,1389155400,1389967200,1390278600,1397743200,1398141000,1398348000,1398832200,1401458400,1405398600,1414159200,1414470600,1415714400,1416457800,1417615200,1418016000,1418657400,1419297600,1421338800,1422507660

Output from Serial Monitor

Initializing SD card...card initialized.
22
22 Fields recieved:
1386347400
1386563400
1387548000
1389155400
1389967200
1390278600
1397743200
1398141000
1398348000
1398832200
1401458400
1405398600
1414159200
1414470600
1415714400
1416457800
1417615200
1418016000
1418657400
1419297600
1421338800
1422507660

Par Hamish Trolove, Cat├ęgorie : Tech Projects

Tags : Arduino / Communication / CSV /