Arduino不写入SD卡?

时间:2017-03-30 01:29:10

标签: c++ arduino sd-card adafruit

问题

我在Adafruit feather mo上有一个Arduino。我正在尝试将数据存储在adalogger上。这个想法很简单。我有一个电位器,我希望将该电位器的数据写入SD卡。我可以创建,打开和关闭CSV文件,但是我无法将数据从电位计写入SD卡上的CSV文件。

很少,我可以从传感器获取数据以写入CSV文件。但是,我不相信这是一个硬件问题,因为我可以使用Serial.println()看到COM中的所有数据,就像我想要的电位器一样。它只是不会写入SD卡。有人可以指出我可能会出错吗?

代码

此代码有效:

**Initalize Stuff** 
//Project Dependencies 
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <string.h>
#include "RTClib.h"

// the setup function runs once when you press reset or power the board
//Global variables, usually associated with physical assets on the board
Sd2Card card;
SdVolume volume;
SdFile root;
char filename[13] = "";
File logfile;
int potPin = 2; //select the input pin for the potentiometer 
const int chipSelect = 10;//chip select for SPID 
File dataFile;
File sensorData;
bool fileOpen = false;

void setup() {
  //Step 1: Initialize Serial
  //================================================================
  Serial.begin(57600);
  //Step 1: Initialize the SD Card
  //================================================================
  //Step 1: Initialize the SD Card
  //================================================================
  Serial.println("Initializing SD card...");
  while (!SD.begin(chipSelect)) {
    // see if the card is present and can be initialized:
    Serial.println("...Card failed, or not present");
    delay(2000);
  }
  Serial.println("...Success.");
  if (card.init(SPI_HALF_SPEED, chipSelect))
    Serial.println("...Card initialized.");
  //// print the type of card
    Serial.print("\n...Card type: ");
    switch (card.type()) {
      case SD_CARD_TYPE_SD1:
        Serial.println("SD1");
        break;
      case SD_CARD_TYPE_SD2:
        Serial.println("SD2");
        break;
      case SD_CARD_TYPE_SDHC:
        Serial.println("SDHC");
        break;
      default:
        Serial.println("Unknown");
    }
    sprintf(filename, "%02d%02d%02d%02d.csv", now.month(), now.day(), now.hour(), now.minute());
    Serial.print("Initializing date file: ");
    Serial.println(filename);
    //Step 2: Create and open new logfile
    if (SD.exists(filename)) {
      Serial.print("...file ");
      Serial.print(filename);
      Serial.println(" already created before...Success.");
    } else {
      //File not found, so create it.
      dataFile = SD.open(filename, FILE_WRITE);
      if (!dataFile) {
        Serial.print("...Couldn't create ");
        Serial.println(filename);
      } else {
        Serial.print("...Success opening ");
        Serial.println(filename);
        Serial.print("...Starting size: ");
        Serial.print(dataFile.size());
        Serial.print(" bytes");
      }
      dataFile.close();
    }
  }

循环

此代码不起作用:

// the loop function runs over and over again until power down or reset
void loop() {
  int WriteEnabled = digitalRead(5); //This is when I turn a switch on to record, this works
  if (WriteEnabled) {
    sensorData = SD.open(filename, FILE_WRITE);
    if (sensorData) {
      uint32_t value = analogRead(potPin);
      //char line[27] = ""; //Define my input line
      //sprintf(line, "%04d/%02d/%02d, %02d:%02d:%02d, %04d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second(), value); //Doesn't work either, why?
      String dataString = String(value);
      sensorData.println(dataString);//This doesn't work
      Serial.println(dataString);
    }
  }
  if (!WriteEnabled && sensorData) {
    sensorData.close();
  }
}

参考

我试图通过咨询几个资源来解决问题。 Stack Overflow上的类似问题无法回答我的问题。

我已使用String()sprint更改了对SD卡的写入,以帮助定义数据。什么都行不通。我不认为这是一个硬件问题,因为我可以打开和关闭文件,但只是不写任何数据。有人可以帮帮我吗?如果有人怀疑硬件,请告诉我原因。我真的很怀疑它,因为为什么我可以在SD卡上创建一个文件并查看COM中的电位计值但不存储它们?

1 个答案:

答案 0 :(得分:3)

尝试更改代码,以便只有当WriteEnable标志从false更改为true时才会发生open调用。为近距离通话做相反的事情。

要更直接地匹配Arduino Reference,请将close调用移到write之后。

相关问题