0

I'm not sure why my SD card fails to initialize when I run my code (below) but works when I just check to see if it is being read by the Arduino with the CardInfo code in the Arduino library.

The data logger is from Adafruit and the sensor that I'm connecting to it is a MaxBotix one. I've tried reseting the SD card and the Arduino and switching out the SD card. When I verify the code in Arduino there are no issues, so I'm not entirely sure why this isn't working. Any help is greatly appreciated. UPDATES: I was able to get it to initalize the SD card by moving

if (!SD.begin(chipSelect)) {
 error("Card failed, or not present");
}

after the

Serial.println().

but now it's not saving data or even creating a new document and it's not printing data as a new line. Things went south when I added the averaging portion of the code because before then it was printing data as a new line and the SD card initializing. My time is extremely wrong I'm getting a date like 2165/165/165 165:165:85. [attached image of circuit] 1

#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,depth");
 #if ECHO_TO_SERIAL
 Serial.println("millis,stamp,datetime,depth");
 #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
asked Jun 27, 2017 at 14:51

2 Answers 2

1

You cannot initialize the SD card more than once. Pull this code out of the for loop:

if (!SD.begin(chipSelect)) {
 error("Card failed, or not present");
}

Put it after the Serial.println().

answered Jun 27, 2017 at 20:36
0

Make a new sketch that initialize the SD Card and write data to it, without loop.

If this doesn't works,maybe the sdcard is damage.

So, as shash-dev says,initialize the SD card before you start the for cicle and after the Serial.printl().

answered Oct 28, 2017 at 5:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.