Using Arduino > Storage

Sd card data loging for rfid


Hi im new to using arduino and im have a problem with adding sd card logging to my app.

So far everything will compile but i get an empty file on the sd card.
i am useing an Arduino MEGA 2560 a wiznet 5100 for the sd card and for the rfid im useing a parallax rfid reader Serial.
i'm completely stuck any help will be greatly appreciated.

hear is the code im useing

--- Code: ---
 #include <SD.h>
const int chipSelect = 4;
int  val = 0;
char code[10];
int bytesread = 0;

void setup() {

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps

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(53, 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.");

pinMode(2,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(2, LOW);                  // Activate the RFID reader

void loop() {
File dataFile ="datalog.txt", FILE_WRITE);
  if(Serial.available() > 0) {          // if data available from reader
    if((val = == 10) {   // check for header
      bytesread = 0;
      while(bytesread<10) {              // read 10 digit code
        if( Serial.available() > 0) {
          val =;
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
            break;                       // stop reading
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit 
      if(bytesread == 10) {              // if 10 digit read is complete
        Serial.print("TAG code is: ");   // possibly a good TAG
        Serial.println(code);         // print the TAG code
        while (dataFile) {
      bytesread = 0;
           delay(500);                       // wait for a second

--- End code ---

Not sure if this will solve your problem, but you don't appear to be NULL terminating your code string anywhere. Depending on what is in the string the println may work or may run off through memory and hang. Try adding a
code[bytesread] = 0;
after your while loop (and check that bytesread is still <10 to avoid overflow).


[0] Message Index

Go to full version