1

I have been searching everywhere now and nothing seems to work out for me. When I try to input a new word, it overlaps the previously entered words.
Here is my struct definition:

struct LogEvent{
  char fullname[30];  
} logEvent;

Here is how I assign a value to fullname:

strcpy(logEvent.fullname, testName);  

Here is how I print the output:

for(int i = 0; i <= db.count(); i++)
{
  EDB_Status result = db.readRec(i, EDB_REC logEvent);
  if(result == EDB_OK)
  {
     Serial.print(logEvent.fullname);
     Serial.print(" ");
  }
} 

Here is the sample output:

Input 1 - Lebron James  
Output 1- Lebron James

Input 2 - Kobe Bryant
Output 2 - Kobe Bryant Kobe Bryant  (The output should be - Lebron James Kobe Bryant)

Input 3 - LeGoat 
Output 3 - LeGoat LeGoat LeGoat (The output should be - Lebron James Kobe Bryant LeGoat)  

How to fix this?

EDIT. Here is my code:

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

// Use the external SPI SD card as storage
#include <SPI.h>  
#include <SD.h>

int recnumber = 1;  
//Arbitrary definition
struct LogEvent {
    int id;
    int temperature;
    char fullname[30];
}
logEvent;  


inline void writer (unsigned long address, const byte* data, unsigned int  recsize) {
    digitalWrite(13, HIGH);
    dbFile.seek(address);
    dbFile.write(data,recsize);
    dbFile.flush();
    digitalWrite(13, LOW);
}

inline void reader (unsigned long address, byte* data, unsigned int recsize) {
   digitalWrite(13, HIGH);
   dbFile.seek(address);
   dbFile.read(data,recsize);
   digitalWrite(13, LOW);
}  
  EDB db(&writer, &reader);

// Run the demo
void setup()
{
    pinMode(13, OUTPUT);
    digitalWrite(13, LOW);

    Serial.begin(9600);
    Serial.println(" Extended Database Library + External SD CARD storage demo");
    Serial.println();

    randomSeed(analogRead(0));

    if (!SD.begin(SD_PIN)) {
        Serial.println("No SD-card.");
        return;
    }

    // Check dir for db files
    if (!SD.exists("/db")) {
        Serial.println("Dir for Db files does not exist, creating...");
        SD.mkdir("/db");
    }

    if (SD.exists(db_name)) {

        dbFile = SD.open(db_name, FILE_WRITE);

      // Sometimes it wont open at first attempt, espessialy after cold start
      // Let's try one more time
        if (!dbFile) {
            dbFile = SD.open(db_name, FILE_WRITE);
        }

        if (dbFile) {
            Serial.print("Openning current table... ");
            EDB_Status result = db.open(0);
            if (result == EDB_OK) {
                Serial.println("DONE");
            } else {
                Serial.println("ERROR");
                Serial.println("Did not find database in the file " + String(db_name));
                Serial.print("Creating new table... ");
                db.create(0, TABLE_SIZE, (unsigned int)sizeof(logEvent));
                Serial.println("DONE");
                return;
            }
        } else {
            Serial.println("Could not open file " + String(db_name));
            return;
        }
    } else {
        Serial.print("Creating table... ");
        // create table at with starting address 0
        dbFile = SD.open(db_name, FILE_WRITE);
        db.create(0, TABLE_SIZE, (unsigned int)sizeof(logEvent));
        Serial.println("DONE");
    }
    deleteAll();
    recordLimit();
    countRecords();  
}
char testName[30];   

void loop()
{
  if(Serial.available())
  {
    Serial.println("Waiting for name...");
    index = 0;
    for(int i = 0; i < sizeof(testName); ++i)
    {
      testName[i] = (char)0;
    }
    while(Serial.available())
    {
      char character = Serial.read();
      testName[index] = character;
      index++;
    }
    createRecords(testName);
    selectAll();
    delay(1000);
  } 
  delay(1000);
 }  

void createRecords(char *testName)
{
  Serial.print("Creating Record... ");
  logEvent.id = recnumber;
  logEvent.temperature = random(1, 125);
  strcpy(logEvent.fullname, testName);
  recnumber++;
  EDB_Status result = db.appendRec(EDB_REC logEvent);
}

void selectAll()
{
   for (int recno = 1; recno <= db.count(); recno++)
   {
      EDB_Status result = db.readRec(recno, EDB_REC logEvent);
      if (result == EDB_OK)
      {
        Serial.print("Recno: ");
        Serial.print(recno);
        Serial.print(" ID: ");
        Serial.print(logEvent.id);
        Serial.print(" Temp: ");
        Serial.print(logEvent.temperature);
        Serial.print(" Fullname: ");
        Serial.print(logEvent.fullname);
        Serial.print(" Length: ");
        Serial.println(strlen(logEvent.fullname));
      }
    }
 }  

Here is the output:

Extended Database Library + External SD CARD storage demo

Openning current table... DONE
Truncating table... DONE
Record Limit: 2045
Record Count: 0
Record Count: 0
Waiting for name...
Name received: Kobe Bryant
LogEvent Fullname: 
The sentence entered is 11 characters long
Creating Record... Done Creating Record...
Recno: 1 ID: 1 Temp: 64 Fullname: Kobe Bryant Length: 11
Waiting for name...
Name received: Lebron James
LogEvent Fullname: Kobe Bryant
The sentence entered is 12 characters long
Recno: 1 ID: 1 Temp: 64 Fullname: Kobe Bryant Length: 11
Creating Record... Done Creating Record...
Recno: 1 ID: 1 Temp: 64 Fullname: Lebron James Length: 12
Recno: 2 ID: 2 Temp: 78 Fullname: Lebron James Length: 12
Waiting for name...
Name received: Michael Jordan
LogEvent Fullname: Lebron James
The sentence entered is 14 characters long
Recno: 1 ID: 1 Temp: 64 Fullname: Lebron James Length: 12
Recno: 2 ID: 2 Temp: 78 Fullname: Lebron James Length: 12
Creating Record... Done Creating Record...
Recno: 1 ID: 1 Temp: 64 Fullname: Michael Jordan Length: 14
Recno: 2 ID: 2 Temp: 78 Fullname: Michael Jordan Length: 14
Recno: 3 ID: 3 Temp: 29 Fullname: Michael Jordan Length: 14  

Update: I tried to use memset but the result is still the same as above.

7
  • 1
    The sample output doesn't correspond with the your code. Publish a complete, mininal code that show the result you posted. – user31481 Feb 10 '18 at 14:34
  • @LookAlterno Sure – Xirb Feb 10 '18 at 14:52
  • memset fill a block of memory with a given value. See it cplusplus.com/reference/cstring/memset – user31481 Feb 10 '18 at 15:11
  • Okay, I'll try to work on that. – Xirb Feb 10 '18 at 15:13
  • Your code looks clean and well structured, and you have a good debugging approach, but code and output doesn't match (and setup is missing). Your question is valuable; please edit it so code and output match. And complete, so others can test it easily. – user31481 Feb 10 '18 at 15:21
1

I've just tried to extend one of the examples (with internal EEPROM), and it's working as expected:

/*
 EDB_Simple.pde
 Extended Database Library + Internal Arduino EEPROM Demo Sketch 

 The Extended Database library project page is here:
 http://www.arduino.cc/playground/Code/ExtendedDatabaseLibrary

 */
#include "Arduino.h"
#include <EDB.h>

// Use the Internal Arduino EEPROM as storage
#include <EEPROM.h>

// Uncomment the line appropriate for your platform
#define TABLE_SIZE 512 // Arduino 168 or greater

// The number of demo records that should be created.  This should be less 
// than (TABLE_SIZE - sizeof(EDB_Header)) / sizeof(LogEvent).  If it is higher, 
// operations will return EDB_OUT_OF_RANGE for all records outside the usable range.
#define RECORDS_TO_CREATE 10

// Arbitrary record definition for this table.  
// This should be modified to reflect your record needs.
struct LogEvent {
  int id;
  int temperature;
  char test[20] = {};
} 
logEvent;


// The read and write handlers for using the EEPROM Library
void writer(unsigned long address, byte data)
{
  EEPROM.write(address, data);
}

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

// Create an EDB object with the appropriate write and read handlers
EDB db(&writer, &reader);

void setup()
{
  Serial.begin(115200);
  Serial.println("Extended Database Library + Arduino Internal EEPROM Demo");
  Serial.println();

  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;
    strcpy(logEvent.test, (String{recno*3} + " whatever " + recno).c_str());
    db.appendRec(EDB_REC logEvent);
  }

  Serial.print("Record Count: "); Serial.println(db.count());
  // Thanks to katts0...@gmail.com for the next line
  for (recno = 1; recno <= RECORDS_TO_CREATE; recno++)
  {
    db.readRec(recno, EDB_REC logEvent);
    Serial.print("ID: "); Serial.print(logEvent.id);
    Serial.print(" Temp: "); Serial.print(logEvent.temperature);
    Serial.print(" Data: "); Serial.println(logEvent.test);
  }
}

void loop()
{
}

The output:

Extended Database Library + Arduino Internal EEPROM Demo

Record Count: 0
Creating Records...
Record Count: 10
ID: 1 Temp: 2 Data: 3 whatever 1
ID: 2 Temp: 4 Data: 6 whatever 2
ID: 3 Temp: 6 Data: 9 whatever 3
ID: 4 Temp: 8 Data: 12 whatever 4
ID: 5 Temp: 10 Data: 15 whatever 5
ID: 6 Temp: 12 Data: 18 whatever 6
ID: 7 Temp: 14 Data: 21 whatever 7
ID: 8 Temp: 16 Data: 24 whatever 8
ID: 9 Temp: 18 Data: 27 whatever 9
ID: 10 Temp: 20 Data: 30 whatever 10
3
  • I'll try to look into this. – Xirb Feb 10 '18 at 15:39
  • I tried this and it is working as you said. Maybe my problem is with the SD Card library SD.h – Xirb Feb 11 '18 at 12:16
  • @Xirb or reader/writer is not working as expecter. I've used different signatures - it's called for each byte – KIIV Feb 11 '18 at 12:46
1

I managed to solve the problem. The problem is with the SD card library of Arduino. I downloaded this library sdFat library, included it in the sketch #include "SdFat.h", and defined SdFat SD. I also removed the #include <SD.h>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.