RFID(SoftwareSerial)影响/破坏与SD(SPI)的连接

时间:2015-09-07 18:36:08

标签: arduino rfid spi

我正在用Arduino创建一个RFID记录器。我连接了RFID扫描仪,RTC模块和SD卡读卡器。所有部件都可以正常工作,但是当我开始组合不同的草图时出现问题。

只要我不扫描RFID卡,读取文件和写入SD卡上的文件就没问题。当从RFID扫描仪接收到输入时,不再能够读取或写入SD卡。一旦收到RFID输入,连接似乎就“断开连接”。

我尝试在RFID扫描仪中使用不同的引脚,这是设置中的另一个初始化序列,但它并没有什么区别。

这是Arduino的限制还是我做错了什么?

我正在使用ATS125KRW RFID屏蔽和Catalex MicroSD卡适配器以及Arduino Mega。

// SD
#include <SD.h>
File myFile;
char idArray[100][11];
char nameArray[100][11];

// RTC
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;

// RFID
#include <SoftwareSerial.h>
SoftwareSerial rfid = SoftwareSerial(A8 , A9); //(RX,TX)
String cardID; //string to store card id
char c;
char cardArray[11];

int incomingByte = 0;   // for incoming serial data

String rfidInput;
boolean logtosd;

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

  initializeSD();
  initializeRTC();

  readfromSD();

}

void loop()
{       
  while(rfid.available()>0)
  {
      c = rfid.read(); 
      rfidInput += c;    
  } 

  if(rfidInput.length() >=12)
  {
        Serial.print("SCanned: ");
        Serial.println(rfidInput);
        //writetoSD(rfidInput);
        writetoSD("kaart");
      rfidInput = "";

  }

    while(Serial.available()>0)
  {
      c = Serial.read(); 
      cardID += c;
  } 

  if(cardID.length() >= 2)
  {
    writetoSD(cardID);

    cardID = "";
  }  

}

void initializeSD()
{
    // GND en VCC aansluiting via pin 48 en 49
  pinMode(48, OUTPUT);           // set pin to output
  digitalWrite(48, LOW);         // GND pin dus LOW
  pinMode(49, OUTPUT);           // set pin to output
  digitalWrite(49, HIGH);         // VCC pin dus HIGH

  pinMode(53, OUTPUT);

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

void readfromSD()
{
  //open the file for reading:
  myFile = SD.open("db.txt");

  if (myFile) 
  {
     char line[25];         //Array to store entire line 
     int linenumber = 0;
     int arrayPlace = 0;

     while (myFile.available()) 
     {
        char ch = myFile.read();
        if(ch != '\n')
        {
          line[arrayPlace] = ch;
          arrayPlace ++;
        }

        else
        {      
          char id[11];
          char name[11];

          //get ID from entire line
          for(int x = 0; x <= 10 ; x++)
          {
            id[x] = line[x];
          }

          //Get NAME from entire line
          for(int x = 11; x <= 19 ; x++)
          {
            if (line[x] != ';')
            {
              name[x-11] = line[x];
            }
            else
            { 
              // NULL TERMINATE THE ARRAY
              name[x-11] = '\0';
              //STOP
              x = 20;
            }
          }

          // save name to nameArray       
          for(int x = 0; x <= 11 ; x++)
          {
            nameArray[linenumber][x] = name[x];
          }

          // NULL TERMINATE THE ARRAY
          id[10] = '\0';

          // save id to idArray
          for(int x = 0; x <= 11 ; x++)
          {
            idArray[linenumber][x] = id[x];
          }

          linenumber +=1;
          arrayPlace = 0;
        } //else  
     } //while

      // close the file:
      myFile.close();
  } 
  else 
  {
    // if the file didn't open, print an error:
    Serial.println("error opening db.txt");
  }
}

void writetoSD(String cardID)
{
  //open file for writing
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) 
  {
    Serial.println("Writing time to test.txt...");

    DateTime now = rtc.now();  
    myFile.print(now.day(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.year(), DEC);
    myFile.print(' ');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print('\t');

    Serial.println("Writing string to test.txt...");
    myFile.println(cardID);

    // close the file:
    myFile.flush();
    Serial.println("done.");
  } 
  else 
  {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void initializeRTC()
{
   // GND en VCC aansluiting via pin 18 en 19
  pinMode(18, OUTPUT);           // set pin to output
  digitalWrite(18, LOW);         // GND pin dus LOW
  pinMode(19, OUTPUT);           // set pin to output
  digitalWrite(19, HIGH);         // VCC pin dus HIGH

  #ifdef AVR
  Wire.begin();
  #else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
  #endif
  rtc.begin();

  Serial.print("RTC  Initialized: ");

  DateTime now = rtc.now();

    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

}

void initializeRFID()
{
    rfid.begin(9600);
    Serial.println("RFID initialized"); 
}

0 个答案:

没有答案