Arduino Extended Database Library using an SD Card

The Arduino Playground Site of the Extended Database Library states that : „With these changes, it is now possible to use this library in conjunction with the standard Arduino EEPROM library, an external EEPROM such as the AT24C1024, providing 128 – 512 kilobytes of non-volatile storage, or any other platform that supports byte level reading and writing such as an SD card.“

But unfortunately I didn’t find any example in the internet that shows how to use the library with an SD Card. In the following is my version of the supplied EDB_Simple using an SD Card instead of the build-in EEPROM. For learning how to connect an SD Card Writer/Reader to an Arduino there are a lot of examples around. My Example is for an Arduino UNO with a cheap SD module hardwired to PIN 10 (Those modules do not work on the newer Arduinos). Important are the reader and writer functions and that you have to create a file and use the seek() method.

PLEASE NOTE! BEGINNING WITH ARDUINO 1.0 YOU HAVE TO CHANGE CODE IN EDB.CPP (THE LIBRARY): #include „WProgram.h“ has to be changed to #include „Arduino.h“.

#include "Arduino.h"
#include <EDB.h>
#include <SD.h>

File dbFile;

#define TABLE_SIZE 512
#define RECORDS_TO_CREATE 10

struct LogEvent {
  int id;
  int temperature;
}
logEvent;

void writer(unsigned long address, byte data)
{
  dbFile.seek(address);
  dbFile.write(data);
  dbFile.flush();
}

byte reader(unsigned long address)
{
  dbFile.seek(address);
  return dbFile.read();
}

EDB db(&writer, &reader);

void setup()
{
  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  pinMode(10, OUTPUT);

  if (!SD.begin()) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.println("initialization done.");

  Serial.println("Opening example.db ...");
  dbFile = SD.open("example.db", FILE_WRITE);

  db.create(0, TABLE_SIZE, sizeof(logEvent));

  Serial.print("Record Count: "); Serial.println(db.count());

  Serial.println("Creating Records...");
  int recno;
  for (recno = 1; recno <= RECORDS_TO_CREATE; recno++)
  {
    logEvent.id = recno;
    logEvent.temperature = recno * 2;
    db.appendRec(EDB_REC logEvent);
  }

  Serial.print("Record Count: "); Serial.println(db.count());
  for (recno = 1; recno < RECORDS_TO_CREATE; recno++)
  {
    db.readRec(recno, EDB_REC logEvent);
    Serial.print("ID: "); Serial.println(logEvent.id);
    Serial.print("Temp: "); Serial.println(logEvent.temperature);
  }
}

void loop()
{

}