133709 SD Card Module Slot Socket Reader For Arduino ARM MCU

From air
Jump to: navigation, search
(thumbnail)
Arduino and SD Card Reader for sensor data logging


A voir

Arduino sketch

/*
 * Log the value of the analog inputs in a data file 
 *
 * Requires the SD library from the Adafruit website :https://github.com/adafruit/SD
 *
 *
 * SD card attached MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 10
 * GND to GND pin - +5 to 5V
 * 
 * @from http://profmason.com/?p=1956
 */
 
#include <SD.h>
 
const int chipSelect = 10;
 
const int NBLINE_BEFORE_FLUSH = 50;

const int PERIOD = 10; // period in ms between 2 acquisitions 

#define LOGFILE_NAME "LOGGER00.CSV"

File dataFile;
 
void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  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(SS, 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:
    while (1) ;
  }
  Serial.println("card initialized.");
 
  // Open up the file we're going to log to!
  // create a new file
  
  char filename[] = LOGFILE_NAME;
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      dataFile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }
 
  if (! dataFile) {
    Serial.println("couldnt create file");
  }
  Serial.print("Logging to: ");
  Serial.println(filename);
}
 
int i = 0;  //Use this for the counter
void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";
  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 6; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 5) {
      dataString += ",";
    }
  }
  dataFile.println(dataString);
  // print to the serial port too:
  //Serial.println(' ');
  // The line dataFile.flush() will 'save' the file to the SD card after every
  // line of data - this will use more power and slow down how much data
  // you can read but it's safer!
  // If you want to speed up the system, call flush() less often and it
  // will save the file only when called and every 512 bytes - every time a sector on the
  // SD card is filled with data. However, don't depend on the automatic flush!
  i++;
  if (i>NBLINE_BEFORE_FLUSH){  //Call every NBLINE_BEFORE_FLUSH times
    dataFile.flush();
    i = 0;
  }
  // Take 1 measurement every PERIOD milliseconds
  delay(PERIOD);
}


Personal tools
Namespaces

Variants
Actions
Navigation
multilingual version
Tools