arduino uno sd卡无法初始化

时间:2017-06-27 14:10:11

标签: logging arduino arduino-uno

我不确定为什么我的SD卡在我运行我的代码时无法初始化(如下),但是当我只是检查Arduino是否正在使用Arduino库中的CardInfo代码读取它时。数据记录器来自Ada fruit,我连接的传感器是maxbotic。我已经尝试重置SD卡和Arduino并切换SD卡。当我验证Arduino中的代码时没有问题,所以我不完全确定为什么这不起作用。非常感谢任何帮助。

更新: 我能够通过移动

来获得SD卡的初始化
if (!SD.begin(chipSelect)) {
  error("Card failed, or not present");
}

之后

Serial.println().

但现在它没有保存数据,也没有将数据打印为新行。当我添加代码的平均部分时,事情就变成了南方,因为在此之前它将数据打印为新行并且SD卡初始化。 我的时间非常错误我得到了像2165/165/165 165:165:85这样的日期。 attached image of circuit

 #include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
/*
The goal of this code is to collect 10 data points a minute, average them, and store them to an SD card

*/
#define LOG_INTERVAL 6000 // mills between entries (reduce to take more/faster data)
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to 
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0// Wait for serial input in setup()
RTC_DS1307 RTC; // define the Real Time Clock object
// The analog pin that connect to the sensor
const int ultrasonicPin = A0;    // declare the input pin for the ultrasonic sensor
unsigned long time;
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
const int numReadings = 10;    //define number of samples to keep track of
int readings[numReadings];    //the readings from the analog input
int readIndex = 0;            // the index of the current reading
int total = 0;                // the running total
int average = 0;              // the average
// the logging file
File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);

  // red LED indicates error
  //digitalWrite(redLEDpin, HIGH);

  while(1);
}

void setup() {
  Serial.begin(9600);
  Serial.println();
  //initialize all the readings to 0:
for(int thisReading = 0; thisReading < numReadings; thisReading++) {
  readings[thisReading] = 0;


#if WAIT_TO_START
  Serial.println("Type any character to start");
  while (!Serial.available());
#endif //WAIT_TO_START

  // initialize the SD card
  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(10, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
  Serial.println("card initialized.");

  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }

  if (! logfile) {
    error("couldnt create file");
  }

  Serial.print("Logging to: ");
  Serial.println(filename);

  // connect to RTC
  Wire.begin();  
  if (!RTC.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL
  }


  logfile.println("millis,stamp,datetime,light,temp,vcc");    
#if ECHO_TO_SERIAL
  Serial.println("millis,stamp,datetime,light,temp,vcc");
#endif //ECHO_TO_SERIAL

  // If you want to set the aref to something other than 5v
  analogReference(EXTERNAL);
}
}


void loop() {
   DateTime now;

  // delay for the amount of time we want between readings
 delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));

  // log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m);           // milliseconds since start
  logfile.print(", ");    
#if ECHO_TO_SERIAL
  Serial.print(m);         // milliseconds since start
  Serial.print(", ");  
#endif

  // fetch the time
  now = RTC.now();
  // log time
  logfile.print(now.unixtime()); // seconds since 1/1/1970
  logfile.print(", ");
  logfile.print('"');
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print('"');
#if ECHO_TO_SERIAL
  Serial.print(now.unixtime()); // seconds since 1/1/1970
  Serial.print(", ");
  Serial.print('"');
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print('"');
#endif //ECHO_TO_SERIAL
  analogRead(ultrasonicPin);
  delay(10);
  int value = analogRead(ultrasonicPin);
 for (int i=0; i < 50; i++){
 if (millis() < 60000*(++i))
{
  // subtract the last reading:
  total = total - readings[readIndex];
  //read from the sensor:
  readings[readIndex] = analogRead(ultrasonicPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  //advance to the next position in the array:
  readIndex = readIndex + 1;

  //if we're at the end of the array
  if (readIndex >= numReadings) {
    //wrap around to the beginning:
    readIndex = 0;
   }
   //calculate the average:
   average = total / numReadings;
   delay(6000);
}

else if (millis() == 60000*(++i))
{
  logfile.print(",");
  logfile.print(average);

  Serial.print("Ultrasonic Reading = ");
  Serial.println(average);
  //Serial.print("Time: ");
  //time = millis();
  //prints time since program started
  //Serial.println(time);
  //wait a second so as not to send massive amounts of data
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
  delay(100);  

}
 }
}
  // this reads in cm

0 个答案:

没有答案