Data Logger Program Not Recognizing Card

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Xjkh3vk
 
Posts: 32
Joined: Sun Mar 24, 2013 1:20 pm

Data Logger Program Not Recognizing Card

Post by Xjkh3vk »

Today I added the function of having digital input pins on to a program I have been working on for quite a while. When I went to test it, the Arduino (mega r3) gave me the failed card warning ( !SD.begin(chipSelect) was triggered) and wouldn't run. I thought, ok maybe the card is dirty or something, but that wasn't the issue. I tested the shield and card with another arduino and got the same result. When I used an earlier simpler version of the program that I was running, it worked fine, the Arduino recognized the card and wrote to it perfectly. Any suggestions you have would be great.

If it helps, I'm using Arduino 1.0.5 on mac 10.6.8

Here's the code:

Code: Select all

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_LSM303.h>
#include <Adafruit_Sensor.h>

// If you're using an Arduino Mega, you'll have to access: Arduino(the app)/contents/resources/java/libraries/SD/utility/Sd2Card.h 
// and then change #define MEGA_SOFT_SPI 0 to #define MEGA _SOFT_SPI 1
// If you are using any Arduino other than the Mega, you don't have to worry about this

#define LOG_INTERVAL  1000 // mills between entries 
#define SYNC_INTERVAL 1000 // mills between calls to flush() (to write data to the card)
uint32_t syncTime = 0; 

#define logPin 6 // Input coming from drive system
#define redLEDpinExternal 5 // Red LED that will be mounted on the front panel
#define redLEDpin 4 // Red LED on the adafruit data logger shield
#define greenLEDpin 3 // Green LED on the adafruit data logger shield
#define greenLEDpinExternal 2 // Green LED that will be mounted on the front panel

#define analogPin0 0 // Various sensors         
#define analogPin1 1
#define analogPin2 2
#define analogPin3 3
#define analogPin4 4
#define analogPin5 5
#define analogPin6 6
#define analogPin7 7
#define analogPin8 8
#define analogPin9 9
#define analogPin10 10
#define analogPin11 11
#define analogPin12 12
#define analogPin13 13
#define analogPin14 14
#define analogPin15 15

#define pos1pin 22 // Digital input pins
#define neg1pin 23 // Some have pullup resistors and will be activated when grounded, these are the inputs refered to as negative
#define pos2pin 24 // Some have pulldown resistors and will be activated when positive voltage is applied, these are reered to as positive
#define neg2pin 25
#define pos3pin 26
#define neg3pin 27
#define pos4pin 28
#define neg4pin 29
#define pos5pin 30
#define neg5pin 31
#define pos6pin 32
#define neg6pin 33
#define pos7pin 34
#define neg7pin 35
#define pos8pin 36
#define neg8pin 37
#define pos9pin 38
#define neg9pin 39

Adafruit_LSM303_Accel accel = Adafruit_LSM303_Accel(48151623); // Gives the sensor an ID
Adafruit_LSM303_Mag mag = Adafruit_LSM303_Mag(48151623);

RTC_DS1307 RTC; 

const int chipSelect = 10; // Digital pin 10 is the cs line for the SD card

File logfile;

String pos1 = "INACTIVE"; // Setting the strings for the digital input status
String neg1 = "INACTIVE";
String pos2 = "INACTIVE";
String neg2 = "INACTIVE";
String pos3 = "INACTIVE";
String neg3 = "INACTIVE";
String pos4 = "INACTIVE";
String neg4 = "INACTIVE";
String pos5 = "INACTIVE";
String neg5 = "INACTIVE";
String pos6 = "INACTIVE";
String neg6 = "INACTIVE";
String pos7 = "INACTIVE";
String neg7 = "INACTIVE";
String pos8 = "INACTIVE";
String neg8 = "INACTIVE";
String pos9 = "INACTIVE";
String neg9 = "INACTIVE";

void setup(void){
  
  Serial.begin(115200);
  Wire.begin();
   
  pinMode(logPin, INPUT);
  pinMode(redLEDpinExternal, OUTPUT);
  pinMode(redLEDpin, OUTPUT);
  pinMode(greenLEDpin, OUTPUT);
  pinMode(greenLEDpinExternal, OUTPUT);
  pinMode(10, OUTPUT);
  
  pinMode(pos1pin, OUTPUT);
  pinMode(neg1pin, OUTPUT);
  pinMode(pos2pin, OUTPUT);
  pinMode(neg2pin, OUTPUT);
  pinMode(pos3pin, OUTPUT);
  pinMode(neg3pin, OUTPUT);
  pinMode(pos4pin, OUTPUT);
  pinMode(neg4pin, OUTPUT);
  pinMode(pos5pin, OUTPUT);
  pinMode(neg5pin, OUTPUT);
  pinMode(pos6pin, OUTPUT);
  pinMode(neg6pin, OUTPUT);
  pinMode(pos7pin, OUTPUT);
  pinMode(neg7pin, OUTPUT);
  pinMode(pos8pin, OUTPUT);
  pinMode(neg8pin, OUTPUT);
  pinMode(pos9pin, OUTPUT);
  pinMode(neg9pin, OUTPUT);
  
  digitalWrite(redLEDpin, LOW);
  digitalWrite(redLEDpinExternal, LOW);
  digitalWrite(greenLEDpin, LOW);
  digitalWrite(greenLEDpinExternal, LOW);
  
  if (!RTC.begin()) {
    Serial.println("ERROR: RTC failed"); // In the case that the RTC has failed, blink twice and continue
    logfile.println("RTC failed");
    for(int b=0; b < 2; b++){
      digitalWrite(redLEDpin, HIGH);
      digitalWrite(redLEDpinExternal, HIGH);
      delay(400);
      digitalWrite(redLEDpin, LOW);
      digitalWrite(redLEDpinExternal, LOW);
      delay(300);
    }
  }
 
  if(!accel.begin()){
    Serial.println("ERROR: LSM303 failed"); // In the case that the accelerometer has failed, blink thrice and continue
    logfile.println("LSM303 failed");
    for(int b=0; b < 3; b++){
      digitalWrite(redLEDpin, HIGH);
      digitalWrite(redLEDpinExternal, HIGH);
      delay(400);
      digitalWrite(redLEDpin, LOW);
      digitalWrite(redLEDpinExternal, LOW);
      delay(300);
    }
  }

  Serial.print("Initializing SD card... "); // Initialize the SD card  
  
  if (!SD.begin(chipSelect)) { // In the case that the SD card has failed or is not present, blink once, then resets after 5 seconds
    Serial.println("ERROR: Card failed, or not present");
    for(int a=0; a < 5; a++){
      digitalWrite(redLEDpin, HIGH);
      digitalWrite(redLEDpinExternal, HIGH);
      delay(400);
      digitalWrite(redLEDpin, LOW);
      digitalWrite(redLEDpinExternal, LOW);
      delay(1300);
    }
    asm volatile ("jmp 0"); // Resets the Arduino
  }

  char filename[] = "LOGGER00.CSV"; // Creates new file
  for(uint8_t i = 0; i < 100; i++){
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // Leave the loop
    }
  }
  
  if (! logfile){
    Serial.println("FATAL ERROR: Couldnt create file"); // In the case that the a new file couldn't be created, blink quice (4 times, yes quice is not a word), forever
    for(int a=0; a > -1; a++){
        for(int b=0; b < 4; b++){
          digitalWrite(redLEDpin, HIGH);
          digitalWrite(redLEDpinExternal, HIGH);
          delay(400);
          digitalWrite(redLEDpin, LOW);
          digitalWrite(redLEDpinExternal, LOW);
          delay(300);
        }
      delay(1000);
    }
    
  }
  
  Serial.print("Logging to: "); // Says the name of the file that is being logging to 
  Serial.print(filename);
  Serial.println(", the unit will begin logging and giving a readout when the drive engine is turned on");
  /*Serial.println(">: Thank you, namaste, and good luck"); // Just for fun. You can comment this out if you want
  Serial.println("           DDDDDDDDDDDDDDDDDDD");
  Serial.println("        DD  M  MMMMMMMMMMM  M  DD");
  Serial.println("      DD   MM   MMMM MMMM   MM   DD");
  Serial.println("    DDD  MM MM            MM  MM  DDD ");
  Serial.println("   DD   M  MM M  MMMMMMM    MM .MM  DD");
  Serial.println(" DDD  MM MM MMM           MM  MM MM  DDD");
  Serial.println(" D   MMMMM MM   DDDDDDDDD   MM MM .M   D");
  Serial.println(" D  M    MM   DDDDDDDDDDDDD  MM     M  D");
  Serial.println(" D  MMMM     DDDDDDDDDDDDDDD     MM M  D");
  Serial.println(" D  MMMM M  NDDDDDDDDDDDDDDDD  M MM M  D");
  Serial.println(" D  MMMM M  DD   DHAMRMA   DD  M MM M  D");
  Serial.println(" D  MMMM M  DDDDDDDDDDDDDDDDD  M MM M  D");
  Serial.println(" D  MMMM     DDDDDDDDDDDDDDD     MM M  D");
  Serial.println(" D  M     MM  DDDDDDDDDDDDD  MM     M  D");
  Serial.println(" D   M  MM MM   DDDDDDDDD  MMM MM  M   D");
  Serial.println(" DDD  MM MM  MM           MM MMM MM   DD");
  Serial.println("  .DD  MM  MM M  MMM MMM  M MM MMM  DD.");
  Serial.println("    DDD  MM  MM           MM  MM  DDD ");
  Serial.println("      DD  .MM   MMMMMMMMM   MM.  DD");
  Serial.println("        DD  M  MMMMMMMMMMM  M  DD");
  Serial.print  ("           DNNNNNNNNNNNNNNNNND");*/
  
  logfile.println("millis (ignore this),time,accel,pitch,dps,roll,analog0,analog1,analog2,analog3,analog4,analog5,analog6,analog7,analog8,analog9,analog10,analog11,analog12,analog13,analog14,analog15,pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9,neg1,neg2,neg3,neg4,neg5,neg6,neg7,neg8,neg9"); // This will be the header of the csv file  

  for(int y=0; y < 3; y++){ // Blink thrice to signal that the unit has been initialized 
    digitalWrite(greenLEDpin, HIGH);
    digitalWrite(greenLEDpinExternal, HIGH);
    delay(400);
    digitalWrite(greenLEDpin, LOW);
    digitalWrite(greenLEDpinExternal, LOW);
    delay(300);
  }
  
  RTC.adjust(DateTime(__DATE__, __TIME__)); // Uncomment this line and re-upload the code to set the RTC's time to your computer's time
}

void loop(void){
  
  int logstate=digitalRead(logPin);
   
  if(logstate==HIGH){
  
    DateTime now;
    delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); // Sets the delay for the amount of time we want between readings
  
    digitalWrite(greenLEDpin, HIGH);
    digitalWrite(greenLEDpinExternal, HIGH);
   
    int analog0 = analogRead(analogPin0); // Read all of the analog values
    int analog1 = analogRead(analogPin1);
    int analog2 = analogRead(analogPin2);  
    int analog3 = analogRead(analogPin3);
    int analog4 = analogRead(analogPin4);
    int analog5 = analogRead(analogPin5);
    int analog6 = analogRead(analogPin6);
    int analog7 = analogRead(analogPin7);
    int analog8 = analogRead(analogPin8);
    int analog9 = analogRead(analogPin9);
    int analog10 = analogRead(analogPin10);
    int analog11 = analogRead(analogPin11);
    int analog12 = analogRead(analogPin12);
    int analog13 = analogRead(analogPin13);
    int analog14 = analogRead(analogPin14);
    int analog15 = analogRead(analogPin15);
    
    int pos1state = digitalRead(pos1pin); // Read all of the digital values
    int neg1state = digitalRead(neg1pin);
    int pos2state = digitalRead(pos2pin);
    int neg2state = digitalRead(neg2pin);
    int pos3state = digitalRead(pos3pin);
    int neg3state = digitalRead(neg3pin);
    int pos4state = digitalRead(pos4pin);
    int neg4state = digitalRead(neg4pin);
    int pos5state = digitalRead(pos5pin);
    int neg5state = digitalRead(neg5pin);
    int pos6state = digitalRead(pos6pin);
    int neg6state = digitalRead(neg6pin);
    int pos7state = digitalRead(pos7pin);
    int neg7state = digitalRead(neg7pin);
    int pos8state = digitalRead(pos8pin);
    int neg8state = digitalRead(neg8pin);
    int pos9state = digitalRead(pos9pin);
    int neg9state = digitalRead(neg9pin);
    
    if(pos1state == HIGH){ pos1 = "ACTIVE"; } else{ pos1 = "INACTIVE"; } // Instead of reading simply HIGH or LOW, this makes it so when a digital pin is grounded (for the neg inputs), or supplied positive voltage (for the pos inpus), ACTIVE or INACTIVE will be displayed and recorded
    if(pos2state == HIGH){ pos2 = "ACTIVE"; } else{ pos2 = "INACTIVE"; }
    if(pos3state == HIGH){ pos3 = "ACTIVE"; } else{ pos3 = "INACTIVE"; }
    if(pos4state == HIGH){ pos4 = "ACTIVE"; } else{ pos4 = "INACTIVE"; }
    if(pos5state == HIGH){ pos5 = "ACTIVE"; } else{ pos5 = "INACTIVE"; }
    if(pos6state == HIGH){ pos6 = "ACTIVE"; } else{ pos6 = "INACTIVE"; }
    if(pos7state == HIGH){ pos7 = "ACTIVE"; } else{ pos7 = "INACTIVE"; }
    if(pos8state == HIGH){ pos8 = "ACTIVE"; } else{ pos8 = "INACTIVE"; }
    if(pos9state == HIGH){ pos9 = "ACTIVE"; } else{ pos9 = "INACTIVE"; }
    
    if(neg1state == LOW){ neg1 = "ACTIVE"; } else{ neg1 = "INACTIVE"; }
    if(neg2state == LOW){ neg2 = "ACTIVE"; } else{ neg2 = "INACTIVE"; }
    if(neg3state == LOW){ neg3 = "ACTIVE"; } else{ neg3 = "INACTIVE"; }
    if(neg4state == LOW){ neg4 = "ACTIVE"; } else{ neg4 = "INACTIVE"; }
    if(neg5state == LOW){ neg5 = "ACTIVE"; } else{ neg5 = "INACTIVE"; }
    if(neg6state == LOW){ neg6 = "ACTIVE"; } else{ neg6 = "INACTIVE"; }
    if(neg7state == LOW){ neg7 = "ACTIVE"; } else{ neg7 = "INACTIVE"; }
    if(neg8state == LOW){ neg8 = "ACTIVE"; } else{ neg8 = "INACTIVE"; }
    if(neg9state == LOW){ neg9 = "ACTIVE"; } else{ neg9 = "INACTIVE"; }

    sensors_event_t event;
    accel.getEvent(&event); // Take accelerometer sample
  
    float Accelx=event.acceleration.x-0.27; // Calibration of the accelerometer
    float Accely=event.acceleration.y-0.15; // This must be calibrated to the sensor you are using
    float Accelz=event.acceleration.z-0.35; // If you can't calibrate it, just say float Accel[axis]=event.acceleration.[axis];
    
    float pitch =atan2(-Accely,-Accelz)*57.2957795+180; // Calculation to find angle of pitch 
    if(pitch>180){
      pitch -= 360; // Makes it so that when the sensor is tilting up the pitch is negative
    }
    
    float roll =atan2(-Accelx,-Accelz)*57.2957795+180; // Calculation to find angle of roll 
    if(roll>180){
      roll -= 360; // Makes it so that when the sensor is tiling left the roll is negative
    }
  
    mag.getEvent(&event); // Take magnetometer sample

    float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / 3.14159; // Calculation to find heading
    if (heading < 0){
      heading += 360;
    }

    delay(200);
    
    mag.getEvent(&event); // Take magnetometer sample

    float heading1 = (atan2(event.magnetic.y,event.magnetic.x) * 180) / 3.14159; // Calculation to find the second heading
    if (heading1 < 0){
      heading1 += 360;
    }

    float dps = (heading1-heading)*5; // This calculates the rotation of the sensor in degrees per second by comparing the two headings
    
    uint32_t m = millis();
    //logfile.print(m);           // milliseconds since start
    logfile.print(", ");    
    now = RTC.now();
    logfile.print(now.month(), DEC);
    logfile.print("/");
    logfile.print(now.day(), DEC);
    logfile.print("/");
    logfile.print(now.year(), DEC);
    logfile.print(" ");
    logfile.print(now.hour(), DEC);
    logfile.print(":");
    logfile.print(now.minute(), DEC);
    logfile.print(":");
    logfile.print(now.second(), DEC);
    logfile.print(", ");
    
    logfile.print(Accelx); // Logs the accelerometer data, and the sensor inputs
    logfile.print(", "); 
    logfile.print(pitch); 
    logfile.print(", ");
    logfile.print(dps); 
    logfile.print(", ");
    logfile.print(roll);
    logfile.print(", "); 
    
    logfile.print(analog0);
    logfile.print(", ");    
    logfile.print(analog1);
    logfile.print(", ");    
    logfile.print(analog2); 
    logfile.print(", ");    
    logfile.print(analog3);
    logfile.print(", ");
    logfile.print(analog4);
    logfile.print(", "); 
    logfile.print(analog5);
    logfile.print(", ");
    logfile.print(analog6);
    logfile.print(", ");
    logfile.print(analog7);
    logfile.print(", ");
    logfile.print(analog8);
    logfile.print(", ");
    logfile.print(analog9);  
    logfile.print(", ");
    logfile.print(analog10);
    logfile.print(", ");
    logfile.print(analog11);
    logfile.print(", ");
    logfile.print(analog12);
    logfile.print(", ");
    logfile.print(analog13);
    logfile.print(", ");
    logfile.print(analog14);
    logfile.print(", "); 
    logfile.print(analog15); 
    
    logfile.print(", ");
    logfile.print(pos1);
    logfile.print(", ");
    logfile.print(pos2);
    logfile.print(", ");
    logfile.print(pos3);
    logfile.print(", ");
    logfile.print(pos4);
    logfile.print(", ");
    logfile.print(pos5);
    logfile.print(", ");
    logfile.print(pos6);
    logfile.print(", ");
    logfile.print(pos7);
    logfile.print(", ");
    logfile.print(pos8);
    logfile.print(", ");
    logfile.print(pos9);
    
    logfile.print(", ");
    logfile.print(neg1);
    logfile.print(", ");
    logfile.print(neg2);
    logfile.print(", ");
    logfile.print(neg3);
    logfile.print(", ");
    logfile.print(neg4);
    logfile.print(", ");
    logfile.print(neg5);
    logfile.print(", ");
    logfile.print(neg6);
    logfile.print(", ");
    logfile.print(neg7);
    logfile.print(", ");
    logfile.print(neg8);
    logfile.print(", "); // The program is ordered like it is so that all of the data can be printed extremetly quikly
    logfile.println(neg9); // If you set the serial monitor to the right size, it will look like the data is just refreshing intead of scrolling
    logfile.flush(); // Syncs data to the card & updates FAT
   
    Serial.println();
    Serial.print("Date/time: "); // Prints the date and time 
    //Serial.print('"');
    Serial.print(now.month(), DEC);
    Serial.print("/");
    Serial.print(now.day(), 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.println(now.second(), DEC);
    //Serial.println('"');
  
    Serial.print("Acceleration: "); // Prints the accelerometer data
    Serial.println(Accelx); 
    Serial.print("Pitch: "); 
    Serial.println(pitch); 
    Serial.print("Deg per sec: ");
    Serial.println(dps); 
    Serial.print("Roll: ");
    Serial.println(roll);
  
    Serial.print("Analog 0: "); // Prints the sensor inputs
    Serial.println(analog0);
    Serial.print("Analog 1: ");    
    Serial.println(analog1);
    Serial.print("Analog 2: ");   
    Serial.println(analog2);
    Serial.print("Analog 3: ");    
    Serial.println(analog3);
    Serial.print("Analog 4: ");
    Serial.println(analog4);
    Serial.print("Analog 5: ");
    Serial.println(analog5);
    Serial.print("Analog 6: ");
    Serial.println(analog6);
    Serial.print("Analog 7: ");
    Serial.println(analog7);
    Serial.print("Analog 8: ");
    Serial.println(analog8);
    Serial.print("Analog 9: ");
    Serial.println(analog9);
    Serial.print("Analog 10: ");
    Serial.println(analog10);
    Serial.print("Analog 11: ");
    Serial.println(analog11);
    Serial.print("Analog 12: ");
    Serial.println(analog12);
    Serial.print("Analog 13: ");
    Serial.println(analog13);
    Serial.print("Analog 14: ");
    Serial.println(analog14);
    Serial.print("Analog 15: ");
    Serial.println(analog15);
    
    Serial.print("POS1: "); Serial.print(pos1); Serial.print("  NEG1: "); Serial.println(neg1);
    Serial.print("POS2: "); Serial.print(pos2); Serial.print("  NEG2: "); Serial.println(neg2);
    Serial.print("POS3: "); Serial.print(pos3); Serial.print("  NEG3: "); Serial.println(neg3);
    Serial.print("POS4: "); Serial.print(pos4); Serial.print("  NEG4: "); Serial.println(neg4);
    Serial.print("POS5: "); Serial.print(pos5); Serial.print("  NEG5: "); Serial.println(neg5);
    Serial.print("POS6: "); Serial.print(pos6); Serial.print("  NEG6: "); Serial.println(neg6);
    Serial.print("POS7: "); Serial.print(pos7); Serial.print("  NEG7: "); Serial.println(neg7);
    Serial.print("POS8: "); Serial.print(pos8); Serial.print("  NEG8: "); Serial.println(neg8);
    Serial.print("POS9: "); Serial.print(pos9); Serial.print("  NEG9: "); Serial.println(neg9);

    digitalWrite(greenLEDpin, LOW);
    digitalWrite(greenLEDpinExternal, LOW);

    if ((millis() - syncTime) < SYNC_INTERVAL) return; // Limits the speed of the logging
    
    syncTime = millis();
  }
  
  else{ // If not logging, turn off the LEDs
    digitalWrite(greenLEDpin, LOW);
    digitalWrite(greenLEDpinExternal, LOW);
    /*Serial.println();
    Serial.println(">:_"); // This makes it look like the cursor is changing (">:_" changes to ">:")
    Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); 
    Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); 
    delay(500);
    Serial.println(">:");
    Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); 
    Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); 
    delay(500);*/
  }
      
}

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: Data Logger Program Not Recognizing Card

Post by adafruit_support_bill »

Looking at your code, I see that you have a lot of literal strings. I suspect what you are seeing is due to a memory problem. Have a look at this tutorial: http://learn.adafruit.com/memories-of-an-arduino

This page in particular: http://learn.adafruit.com/memories-of-a ... izing-sram

Xjkh3vk
 
Posts: 32
Joined: Sun Mar 24, 2013 1:20 pm

Re: Data Logger Program Not Recognizing Card

Post by Xjkh3vk »

I've tried to reduce the size of the program as much as I can, but it still isn't working. Is there any way I can further reduce the size? I don't know how to make it any smaller.

Code: Select all

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_LSM303.h>
#include <Adafruit_Sensor.h>

// If you're using an Arduino Mega, you'll have to access: Arduino(the app)/contents/resources/java/libraries/SD/utility/Sd2Card.h 
// and then change #define MEGA_SOFT_SPI 0 to #define MEGA _SOFT_SPI 1
// If you are using any Arduino other than the Mega, you don't have to worry about this

#define LOG_INTERVAL  1000 // mills between entries 
#define SYNC_INTERVAL 1000 // mills between calls to flush() (to write data to the card)
uint32_t syncTime = 0; 

#define logPin 6 // Input coming from drive system, this will trigger the logging
#define redLEDpinExternal 5 // Red LED that will be mounted on the front panel
#define redLEDpin 4 // Red LED on the adafruit data logger shield
#define greenLEDpin 3 // Green LED on the adafruit data logger shield
#define greenLEDpinExternal 2 // Green LED that will be mounted on the front panel

#define analogPin0 0 // Various sensors         
#define analogPin1 1
#define analogPin2 2
#define analogPin3 3
#define analogPin4 4
#define analogPin5 5
#define analogPin6 6
#define analogPin7 7
#define analogPin8 8
#define analogPin9 9
#define analogPin10 10
#define analogPin11 11
#define analogPin12 12
#define analogPin13 13
#define analogPin14 14
#define analogPin15 15

#define pos1pin 22 // Digital input pins
#define neg1pin 23 // Some have pullup resistors and will be activated when grounded, these are the inputs refered to as negative
#define pos2pin 24 // Some have pulldown resistors and will be activated when positive voltage is applied, these are refered to as positive
#define neg2pin 25
#define pos3pin 26
#define neg3pin 27
#define pos4pin 28
#define neg4pin 29
#define pos5pin 30
#define neg5pin 31
#define pos6pin 32
#define neg6pin 33
#define pos7pin 34
#define neg7pin 35
#define pos8pin 36
#define neg8pin 37
#define pos9pin 38
#define neg9pin 39

Adafruit_LSM303_Accel accel = Adafruit_LSM303_Accel(48151623); // Gives the sensor an ID
Adafruit_LSM303_Mag mag = Adafruit_LSM303_Mag(48151623);

RTC_DS1307 RTC; 

const int chipSelect = 10; // Digital pin 10 is the cs line for the SD card

File logfile;

void setup(void){
  
  Serial.begin(115200);
  Wire.begin();
   
  pinMode(logPin, INPUT);
  pinMode(redLEDpinExternal, OUTPUT);
  pinMode(redLEDpin, OUTPUT);
  pinMode(greenLEDpin, OUTPUT);
  pinMode(greenLEDpinExternal, OUTPUT);
  pinMode(10, OUTPUT);
  
  pinMode(pos1pin, INPUT);
  pinMode(neg1pin, INPUT);
  pinMode(pos2pin, INPUT);
  pinMode(neg2pin, INPUT);
  pinMode(pos3pin, INPUT);
  pinMode(neg3pin, INPUT);
  pinMode(pos4pin, INPUT);
  pinMode(neg4pin, INPUT);
  pinMode(pos5pin, INPUT);
  pinMode(neg5pin, INPUT);
  pinMode(pos6pin, INPUT);
  pinMode(neg6pin, INPUT);
  pinMode(pos7pin, INPUT);
  pinMode(neg7pin, INPUT);
  pinMode(pos8pin, INPUT);
  pinMode(neg8pin, INPUT);
  pinMode(pos9pin, INPUT);
  pinMode(neg9pin, INPUT);
  
  digitalWrite(redLEDpin, LOW);
  digitalWrite(redLEDpinExternal, LOW);
  digitalWrite(greenLEDpin, LOW);
  digitalWrite(greenLEDpinExternal, LOW);
  
  /*if (!RTC.begin()) {
    Serial.println(F("ERROR: RTC failed")); // In the case that the RTC has failed, blink twice and continue
    logfile.println("RTC failed");
    for(int b=0; b < 2; b++){
      digitalWrite(redLEDpin, HIGH);
      digitalWrite(redLEDpinExternal, HIGH);
      delay(400);
      digitalWrite(redLEDpin, LOW);
      digitalWrite(redLEDpinExternal, LOW);
      delay(300);
    }
  }
 
  if(!accel.begin()){
    Serial.println(F("ERROR: LSM303 failed")); // In the case that the accelerometer has failed, blink thrice and continue
    logfile.println("LSM303 failed");
    for(int b=0; b < 3; b++){
      digitalWrite(redLEDpin, HIGH);
      digitalWrite(redLEDpinExternal, HIGH);
      delay(400);
      digitalWrite(redLEDpin, LOW);
      digitalWrite(redLEDpinExternal, LOW);
      delay(300);
    }
  }*/

  Serial.print(F("Initializing SD card... ")); // Initialize the SD card  
  
  if (!SD.begin(chipSelect)) { // In the case that the SD card has failed or is not present, blink once, then resets after 5 seconds
    Serial.println(F("ERROR: Card failed, or not present"));
    for(int a=0; a < 5; a++){
      digitalWrite(redLEDpin, HIGH);
      digitalWrite(redLEDpinExternal, HIGH);
      delay(400);
      digitalWrite(redLEDpin, LOW);
      digitalWrite(redLEDpinExternal, LOW);
      delay(1300);
    }
    asm volatile ("jmp 0"); // Resets the Arduino
  }

  char filename[] = "LOGGER00.CSV"; // Creates new file
  for(uint8_t i = 0; i < 100; i++){
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // Leave the loop
    }
  }
  
  /*if (! logfile){
    Serial.println(F("FATAL ERROR: Couldnt create file")); // In the case that the a new file couldn't be created, blink quice (4 times, yes quice is not a word), forever
    for(int a=0; a > -1; a++){
        for(int b=0; b < 4; b++){
          digitalWrite(redLEDpin, HIGH);
          digitalWrite(redLEDpinExternal, HIGH);
          delay(400);
          digitalWrite(redLEDpin, LOW);
          digitalWrite(redLEDpinExternal, LOW);
          delay(300);
        }
      delay(1000);
    }
    
  }*/
  
  Serial.print(F("Logging to: ")); // Says the name of the file that is being logging to 
  Serial.print(filename);
  Serial.println(F(", the unit will begin logging and giving a readout when the drive engine is turned on"));

  logfile.println("millis (ignore this),time,accel,pitch,heading,roll,analog0,analog1,analog2,analog3,analog4,analog5,analog6,analog7,analog8,analog9,analog10,analog11,analog12,analog13,analog14,analog15,pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9,neg1,neg2,neg3,neg4,neg5,neg6,neg7,neg8,neg9"); // This will be the header of the csv file  

  for(int y=0; y < 3; y++){ // Blink thrice to signal that the unit has been initialized 
    digitalWrite(greenLEDpin, HIGH);
    digitalWrite(greenLEDpinExternal, HIGH);
    delay(400);
    digitalWrite(greenLEDpin, LOW);
    digitalWrite(greenLEDpinExternal, LOW);
    delay(300);
  }
  
  //RTC.adjust(DateTime(__DATE__, __TIME__)); // Uncomment this line and re-upload the code to set the RTC's time to your computer's time
}

void loop(void){
  
  int logstate=digitalRead(logPin);
   
  if(logstate==HIGH){
  
    DateTime now;
    delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); // Sets the delay for the amount of time we want between readings
  
    digitalWrite(greenLEDpin, HIGH);
    digitalWrite(greenLEDpinExternal, HIGH);
   
    int analog0 = analogRead(analogPin0); // Read all of the analog values
    int analog1 = analogRead(analogPin1);
    int analog2 = analogRead(analogPin2);  
    int analog3 = analogRead(analogPin3);
    int analog4 = analogRead(analogPin4);
    int analog5 = analogRead(analogPin5);
    int analog6 = analogRead(analogPin6);
    int analog7 = analogRead(analogPin7);
    int analog8 = analogRead(analogPin8);
    int analog9 = analogRead(analogPin9);
    int analog10 = analogRead(analogPin10);
    int analog11 = analogRead(analogPin11);
    int analog12 = analogRead(analogPin12);
    int analog13 = analogRead(analogPin13);
    int analog14 = analogRead(analogPin14);
    int analog15 = analogRead(analogPin15);
    
    int pos1 = digitalRead(pos1pin); // Read all of the digital values
    int neg1 = digitalRead(neg1pin);
    int pos2 = digitalRead(pos2pin);
    int neg2 = digitalRead(neg2pin);
    int pos3 = digitalRead(pos3pin);
    int neg3 = digitalRead(neg3pin);
    int pos4 = digitalRead(pos4pin);
    int neg4 = digitalRead(neg4pin);
    int pos5 = digitalRead(pos5pin);
    int neg5 = digitalRead(neg5pin);
    int pos6 = digitalRead(pos6pin);
    int neg6 = digitalRead(neg6pin);
    int pos7 = digitalRead(pos7pin);
    int neg7 = digitalRead(neg7pin);
    int pos8 = digitalRead(pos8pin);
    int neg8 = digitalRead(neg8pin);
    int pos9 = digitalRead(pos9pin);
    int neg9 = digitalRead(neg9pin);

    sensors_event_t event;
    accel.getEvent(&event); // Take accelerometer sample
  
    int Accelx=event.acceleration.x-0.27; // Calibration of the accelerometer
    int Accely=event.acceleration.y-0.15; // This must be calibrated to the sensor you are using
    int Accelz=event.acceleration.z-0.35; // If you can't calibrate it, just say float Accel[axis]=event.acceleration.[axis];
    
    int pitch =atan2(-Accely,-Accelz)*57.2957795+180; // Calculation to find angle of pitch 
    if(pitch>180){
      pitch -= 360; // Makes it so that when the sensor is tilting up the pitch is negative
    }
    
    int roll =atan2(-Accelx,-Accelz)*57.2957795+180; // Calculation to find angle of roll 
    if(roll>180){
      roll -= 360; // Makes it so that when the sensor is tiling left the roll is negative
    }
  
    mag.getEvent(&event); // Take magnetometer sample

    int heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / 3.14159; // Calculation to find heading
    if (heading < 0){
      heading += 360;
    }
    
    uint32_t m = millis();
    //logfile.print(m);           // milliseconds since start
    logfile.print(F(", "));    
    now = RTC.now();
    logfile.print(now.month(), DEC);
    logfile.print(F("/"));
    logfile.print(now.day(), DEC);
    logfile.print(F("/"));
    logfile.print(now.year(), DEC);
    logfile.print(F(" "));
    logfile.print(now.hour(), DEC);
    logfile.print(F(":"));
    logfile.print(now.minute(), DEC);
    logfile.print(F(":"));
    logfile.print(now.second(), DEC);
    logfile.print(F(", "));
    
    logfile.print(Accelx); // Logs the accelerometer data, and the sensor inputs
    logfile.print(F(", ")); 
    logfile.print(pitch); 
    logfile.print(F(", "));
    logfile.print(heading); 
    logfile.print(F(", "));
    logfile.print(roll);
    logfile.print(F(", ")); 
    
    logfile.print(analog0);
    logfile.print(F(", "));    
    logfile.print(analog1);
    logfile.print(F(", "));    
    logfile.print(analog2); 
    logfile.print(F(", "));    
    logfile.print(analog3);
    logfile.print(F(", "));
    logfile.print(analog4);
    logfile.print(F(", ")); 
    logfile.print(analog5);
    logfile.print(F(", "));
    logfile.print(analog6);
    logfile.print(F(", "));
    logfile.print(analog7);
    logfile.print(F(", "));
    logfile.print(analog8);
    logfile.print(F(", "));
    logfile.print(analog9);  
    logfile.print(F(", "));
    logfile.print(analog10);
    logfile.print(F(", "));
    logfile.print(analog11);
    logfile.print(F(", "));
    logfile.print(analog12);
    logfile.print(F(", "));
    logfile.print(analog13);
    logfile.print(F(", "));
    logfile.print(analog14);
    logfile.print(F(", ")); 
    logfile.print(analog15); 
    
    logfile.print(F(", "));
    logfile.print(pos1);
    logfile.print(F(", "));
    logfile.print(pos2);
    logfile.print(F(", "));
    logfile.print(pos3);
    logfile.print(F(", "));
    logfile.print(pos4);
    logfile.print(F(", "));
    logfile.print(pos5);
    logfile.print(F(", "));
    logfile.print(pos6);
    logfile.print(F(", "));
    logfile.print(pos7);
    logfile.print(F(", "));
    logfile.print(pos8);
    logfile.print(F(", "));
    logfile.print(pos9);
    
    logfile.print(F(", "));
    logfile.print(neg1);
    logfile.print(F(", "));
    logfile.print(neg2);
    logfile.print(F(", "));
    logfile.print(neg3);
    logfile.print(F(", "));
    logfile.print(neg4);
    logfile.print(F(", "));
    logfile.print(neg5);
    logfile.print(F(", "));
    logfile.print(neg6);
    logfile.print(F(", "));
    logfile.print(neg7);
    logfile.print(F(", "));
    logfile.print(neg8);
    logfile.print(F(", ")); // The program is ordered like it is so that all of the data can be printed extremetly quikly
    logfile.println(neg9); // If you set the serial monitor to the right size, it will look like the data is just refreshing intead of scrolling
    logfile.flush(); // Syncs data to the card & updates FAT
   
    Serial.println();
    Serial.print(F("Date/time: ")); // Prints the date and time 
    //Serial.print(F('"'));
    Serial.print(now.month(), DEC);
    Serial.print(F("/"));
    Serial.print(now.day(), DEC);
    Serial.print(F("/"));
    Serial.print(now.year(), DEC);
    Serial.print(F(" "));
    Serial.print(now.hour(), DEC);
    Serial.print(F(":"));
    Serial.print(now.minute(), DEC);
    Serial.print(F(":"));
    Serial.println(now.second(), DEC);
    //Serial.println(F('"'));
  
    Serial.print(F("Acceleration: ")); // Prints the accelerometer data
    Serial.println(Accelx); 
    Serial.print(F("Pitch: ")); 
    Serial.println(pitch); 
    Serial.print(F("Heading: "));
    Serial.println(heading); 
    Serial.print(F("Roll: "));
    Serial.println(roll);
  
    Serial.print(F("Analog 0: ")); // Prints the sensor inputs
    Serial.println(analog0);
    Serial.print(F("Analog 1: "));    
    Serial.println(analog1);
    Serial.print(F("Analog 2: "));   
    Serial.println(analog2);
    Serial.print(F("Analog 3: "));    
    Serial.println(analog3);
    Serial.print(F("Analog 4: "));
    Serial.println(analog4);
    Serial.print(F("Analog 5: "));
    Serial.println(analog5);
    Serial.print(F("Analog 6: "));
    Serial.println(analog6);
    Serial.print(F("Analog 7: "));
    Serial.println(analog7);
    Serial.print(F("Analog 8: "));
    Serial.println(analog8);
    Serial.print(F("Analog 9: "));
    Serial.println(analog9);
    Serial.print(F("Analog 10: "));
    Serial.println(analog10);
    Serial.print(F("Analog 11: "));
    Serial.println(analog11);
    Serial.print(F("Analog 12: "));
    Serial.println(analog12);
    Serial.print(F("Analog 13: "));
    Serial.println(analog13);
    Serial.print(F("Analog 14: "));
    Serial.println(analog14);
    Serial.print(F("Analog 15: "));
    Serial.println(analog15);
    
    Serial.print(F("POS1: ")); Serial.print(pos1); Serial.print(F("  NEG1: ")); Serial.println(neg1);
    Serial.print(F("POS2: ")); Serial.print(pos2); Serial.print(F("  NEG2: ")); Serial.println(neg2);
    Serial.print(F("POS3: ")); Serial.print(pos3); Serial.print(F("  NEG3: ")); Serial.println(neg3);
    Serial.print(F("POS4: ")); Serial.print(pos4); Serial.print(F("  NEG4: ")); Serial.println(neg4);
    Serial.print(F("POS5: ")); Serial.print(pos5); Serial.print(F("  NEG5: ")); Serial.println(neg5);
    Serial.print(F("POS6: ")); Serial.print(pos6); Serial.print(F("  NEG6: ")); Serial.println(neg6);
    Serial.print(F("POS7: ")); Serial.print(pos7); Serial.print(F("  NEG7: ")); Serial.println(neg7);
    Serial.print(F("POS8: ")); Serial.print(pos8); Serial.print(F("  NEG8: ")); Serial.println(neg8);
    Serial.print(F("POS9: ")); Serial.print(pos9); Serial.print(F("  NEG9: ")); Serial.println(neg9);

    digitalWrite(greenLEDpin, LOW);
    digitalWrite(greenLEDpinExternal, LOW);

    if ((millis() - syncTime) < SYNC_INTERVAL) return; // Limits the speed of the logging
    
    syncTime = millis();
  }
  
  else{ // If not logging, turn off the LEDs
    digitalWrite(greenLEDpin, LOW);
    digitalWrite(greenLEDpinExternal, LOW);
  }
      
}

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: Data Logger Program Not Recognizing Card

Post by adafruit_support_bill »

First I would go back to the last version of the program that worked - just to make sure you are not looking at a hardware failure.

As for reducing the size - there are lots of opportunities:

You missed this string:

Code: Select all

  logfile.println("millis (ignore this),time,accel,pitch,heading,roll,analog0,analog1,analog2,analog3,analog4,analog5,analog6,analog7,analog8,analog9,analog10,analog11,analog12,analog13,analog14,analog15,pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9,neg1,neg2,neg3,neg4,neg5,neg6,neg7,neg8,neg9"); 
And you could eliminate a lot of variables and condense much of the repetition using loops. For example, consider these two sections of code:

Code: Select all

    int analog0 = analogRead(analogPin0); // Read all of the analog values
    int analog1 = analogRead(analogPin1);
    int analog2 = analogRead(analogPin2);  
    int analog3 = analogRead(analogPin3);
    int analog4 = analogRead(analogPin4);
    int analog5 = analogRead(analogPin5);
    int analog6 = analogRead(analogPin6);
    int analog7 = analogRead(analogPin7);
    int analog8 = analogRead(analogPin8);
    int analog9 = analogRead(analogPin9);
    int analog10 = analogRead(analogPin10);
    int analog11 = analogRead(analogPin11);
    int analog12 = analogRead(analogPin12);
    int analog13 = analogRead(analogPin13);
    int analog14 = analogRead(analogPin14);
    int analog15 = analogRead(analogPin15);

Code: Select all

    logfile.print(analog0);
    logfile.print(F(", "));    
    logfile.print(analog1);
    logfile.print(F(", "));    
    logfile.print(analog2); 
    logfile.print(F(", "));    
    logfile.print(analog3);
    logfile.print(F(", "));
    logfile.print(analog4);
    logfile.print(F(", ")); 
    logfile.print(analog5);
    logfile.print(F(", "));
    logfile.print(analog6);
    logfile.print(F(", "));
    logfile.print(analog7);
    logfile.print(F(", "));
    logfile.print(analog8);
    logfile.print(F(", "));
    logfile.print(analog9);  
    logfile.print(F(", "));
    logfile.print(analog10);
    logfile.print(F(", "));
    logfile.print(analog11);
    logfile.print(F(", "));
    logfile.print(analog12);
    logfile.print(F(", "));
    logfile.print(analog13);
    logfile.print(F(", "));
    logfile.print(analog14);
    logfile.print(F(", ")); 
    logfile.print(analog15); 
You could condense all of that into a loop like:

Code: Select all

for (int analogPin = analog0; analogPin  <= analog15; analogPin ++)
{
    logfile.print(analogRead(analogPin));
    logfile.print(F(", "));    
}
The same would apply to the digital reads.

Xjkh3vk
 
Posts: 32
Joined: Sun Mar 24, 2013 1:20 pm

Re: Data Logger Program Not Recognizing Card

Post by Xjkh3vk »

I tried it with an older version of the code and it didn't work. So it looks more like a hardware error. I don't know how anything could be wrong though, the Arduino has been sitting on my desk for the past month, I haven't done anything with it that might break it. Does this code work for you?

Code: Select all

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_LSM303.h>
#include <Adafruit_Sensor.h>

// If you're using an Arduino Mega, you'll have to access: Arduino(the app)/contents/resources/java/libraries/SD/utility/Sd2Card.h 
// and then change #define MEGA_SOFT_SPI 0 to #define MEGA _SOFT_SPI 1
// If you are using any Arduino other than the Mega, you don't have to worry about this

#define LOG_INTERVAL  1000 // mills between entries 
#define SYNC_INTERVAL 1000 // mills between calls to flush() (to write data to the card)
uint32_t syncTime = 0; 

#define logPin 6 // Input coming from drive system
#define redLEDpinExternal 5 // Red LED that will be mounted on the front panel
#define redLEDpin 4 // Red LED on the adafruit data logger shield
#define greenLEDpin 3 // Green LED on the adafruit data logger shield
#define greenLEDpinExternal 2 // Green LED that will be mounted on the front panel

#define analogPin0 0 // Various sensors         
#define analogPin1 1
#define analogPin2 2
#define analogPin3 3
#define analogPin4 4
#define analogPin5 5
#define analogPin6 6
#define analogPin7 7
#define analogPin8 8
#define analogPin9 9
#define analogPin10 10
#define analogPin11 11
#define analogPin12 12
#define analogPin13 13
#define analogPin14 14
#define analogPin15 15

Adafruit_LSM303_Accel accel = Adafruit_LSM303_Accel(48151623); // Gives the sensor an ID
Adafruit_LSM303_Mag mag = Adafruit_LSM303_Mag(48151623);

RTC_DS1307 RTC; 

const int chipSelect = 10; // Digital pin 10 is the cs line for the SD card

File logfile;

void setup(void){
  
  Serial.begin(115200);
  Wire.begin();
   
  pinMode(logPin, INPUT);
  pinMode(redLEDpinExternal, OUTPUT);
  pinMode(redLEDpin, OUTPUT);
  pinMode(greenLEDpin, OUTPUT);
  pinMode(greenLEDpinExternal, OUTPUT);
  pinMode(10, OUTPUT);

  digitalWrite(redLEDpin, LOW);
  digitalWrite(redLEDpinExternal, LOW);
  digitalWrite(greenLEDpin, LOW);
  digitalWrite(greenLEDpinExternal, LOW);
  
  if (!RTC.begin()) {
    Serial.println("ERROR: RTC failed"); // In the case that the RTC has failed, blink twice and continue
    logfile.println("RTC failed");
    for(int b=0; b < 2; b++){
      digitalWrite(redLEDpin, HIGH);
      digitalWrite(redLEDpinExternal, HIGH);
      delay(400);
      digitalWrite(redLEDpin, LOW);
      digitalWrite(redLEDpinExternal, LOW);
      delay(300);
    }
  }
 
  if(!accel.begin()){
    Serial.println("ERROR: LSM303 failed"); // In the case that the accelerometer has failed, blink thrice and continue
    logfile.println("LSM303 failed");
    for(int b=0; b < 3; b++){
      digitalWrite(redLEDpin, HIGH);
      digitalWrite(redLEDpinExternal, HIGH);
      delay(400);
      digitalWrite(redLEDpin, LOW);
      digitalWrite(redLEDpinExternal, LOW);
      delay(300);
    }
  }

  Serial.print("Initializing SD card... "); // Initialize the SD card  
  
  if (!SD.begin(chipSelect)) { // In the case that the SD card has failed or is not present, blink once, then resets after 5 seconds
    Serial.println("ERROR: Card failed, or not present");
    for(int a=0; a < 5; a++){
      digitalWrite(redLEDpin, HIGH);
      digitalWrite(redLEDpinExternal, HIGH);
      delay(400);
      digitalWrite(redLEDpin, LOW);
      digitalWrite(redLEDpinExternal, LOW);
      delay(1300);
    }
    asm volatile ("jmp 0"); // Resets the Arduino
  }

  char filename[] = "LOGGER00.CSV"; // Creates new file
  for(uint8_t i = 0; i < 100; i++){
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // Leave the loop
    }
  }
  
  if (! logfile){
    Serial.println("FATAL ERROR: Couldnt create file"); // In the case that the a new file couldn't be created, blink quice (4 times, yes quice is not a word), forever
    for(int a=0; a > -1; a++){
        for(int b=0; b < 4; b++){
          digitalWrite(redLEDpin, HIGH);
          digitalWrite(redLEDpinExternal, HIGH);
          delay(400);
          digitalWrite(redLEDpin, LOW);
          digitalWrite(redLEDpinExternal, LOW);
          delay(300);
        }
      delay(1000);
    }
    
  }
  
  Serial.print("Logging to: "); // Says the name of the file that is being logging to 
  Serial.print(filename);
  Serial.println(", the unit will begin logging and giving a readout when the drive engine is turned on");
 
  logfile.println("millis (ignore this),time,accel,pitch,dps,roll,analog0,analog1,analog2,analog3,analog4,analog5,analog6,analog7,analog8,analog9,analog10,analog11,analog12,analog13,analog14,analog15,pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9,neg1,neg2,neg3,neg4,neg5,neg6,neg7,neg8,neg9"); // This will be the header of the csv file  
  
  for(int y=0; y < 3; y++){ // Blink thrice to signal that the unit has been initialized 
    digitalWrite(greenLEDpin, HIGH);
    digitalWrite(greenLEDpinExternal, HIGH);
    delay(400);
    digitalWrite(greenLEDpin, LOW);
    digitalWrite(greenLEDpinExternal, LOW);
    delay(300);
  }
  
  RTC.adjust(DateTime(__DATE__, __TIME__)); // Uncomment this line and re-upload the code to set the RTC's time to your computer's time
}

void loop(void){

  int logstate=digitalRead(logPin);

  if(logstate==HIGH){

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

    digitalWrite(greenLEDpin, HIGH);
    digitalWrite(greenLEDpinExternal, HIGH);

    int analog0 = analogRead(analogPin0);  
    int analog1 = analogRead(analogPin1);
    int analog2 = analogRead(analogPin2);  
    int analog3 = analogRead(analogPin3);
    int analog4 = analogRead(analogPin4);
    int analog5 = analogRead(analogPin5);
    int analog6 = analogRead(analogPin6);
    int analog7 = analogRead(analogPin7);
    int analog8 = analogRead(analogPin8);
    int analog9 = analogRead(analogPin9);
    int analog10 = analogRead(analogPin10);
    int analog11 = analogRead(analogPin11);
    int analog12 = analogRead(analogPin12);
    int analog13 = analogRead(analogPin13);
    int analog14 = analogRead(analogPin14);
    int analog15 = analogRead(analogPin15);

    sensors_event_t event;
    accel.getEvent(&event); // Take accelerometer sample

    float Accelx=event.acceleration.x-0.27; // Calibration of the accelerometer
    float Accely=event.acceleration.y-0.15; // This must be calibrated to the sensor you are using
    float Accelz=event.acceleration.z-0.35; // If you can't calibrate it, just say float Accel[axis]=event.acceleration.[axis];

    float pitch =atan2(-Accelx,-Accelz)*57.2957795+180; // Calculation to find angle of pitch 
    if(pitch>180){
      pitch -= 360; // Makes it so that when going at a downward angle the pitch is negative
    }

    float roll =atan2(-Accely,-Accelz)*57.2957795+180; // Calculation to find angle of roll 
    if(roll>180){
      roll -= 360; // Makes it so that when the sensor is tilting left the pitch is negative
    }

    /*mag.getEvent(&event); // Take magnetometer sample

    float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / 3.14159; // Calculation to find heading
    if (heading < 0){
      heading += 360;
    }

    delay(200);

    mag.getEvent(&event); // Take magnetometer sample

    float heading1 = (atan2(event.magnetic.y,event.magnetic.x) * 180) / 3.14159; // Calculation to find the second heading
    if (heading1 < 0){
      heading1 += 360;
    }

    float dps = (heading1-heading)*5; // This calculates the rotation of the sensor in degrees per second by comparing the two headings*/

    now = RTC.now(); // Gets the time from the RTC
    logfile.print(now.month(), DEC);
    logfile.print("/");
    logfile.print(now.day(), DEC);
    logfile.print("/");
    logfile.print(now.year(), DEC);
    logfile.print(" ");
    logfile.print(now.hour(), DEC);
    logfile.print(":");
    logfile.print(now.minute(), DEC);
    logfile.print(":");
    logfile.print(now.second(), DEC);
    logfile.print(", ");

    logfile.print(Accelx); // Logs the accelerometer data, and the sensor inputs
    logfile.print(", "); 
    logfile.print(pitch); 
    logfile.print(", ");
    //logfile.print(dps); 
    //logfile.print(", ");
    logfile.print(roll);
    logfile.print(", ");  
    logfile.print(analog0);
    logfile.print(", ");    
    logfile.print(analog1);
    logfile.print(", ");    
    logfile.print(analog2); 
    logfile.print(", ");    
    logfile.print(analog3);
    logfile.print(", ");
    logfile.print(analog4);
    logfile.print(", "); 
    logfile.print(analog5);
    logfile.print(", ");
    logfile.print(analog6);
    logfile.print(", ");
    logfile.print(analog7);
    logfile.print(", ");
    logfile.print(analog8);
    logfile.print(", ");
    logfile.print(analog9);  
    logfile.print(", ");
    logfile.print(analog10);
    logfile.print(", ");
    logfile.print(analog11);
    logfile.print(", ");
    logfile.print(analog12);
    logfile.print(", ");
    logfile.print(analog13);
    logfile.print(", ");
    logfile.print(analog14);
    logfile.print(", "); // The program is ordered like it is so that all of the data can be printed extremetly quikly
    logfile.println(analog15); // If you set the serial monitor to the right size, it will look like the data is just refreshing intead of scrolling
    logfile.flush(); // Syncs data to the card & updates FAT

    Serial.println();
    Serial.print("Date/time: "); // Prints the date and time 
    //Serial.print('"');
    Serial.print(now.month(), DEC);
    Serial.print("/");
    Serial.print(now.day(), 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.println(now.second(), DEC);
    //Serial.println('"');

    Serial.print("Acceleration: "); // Prints the accelerometer data
    Serial.println(Accelx); 
    Serial.print("Pitch: "); 
    Serial.println(pitch); 
    Serial.print("Deg per sec: ");
    //Serial.println(dps); 
    Serial.print("Roll: ");
    Serial.println(roll);

    Serial.print("Analog 0: "); // Prints the sensor inputs
    Serial.println(analog0);
    Serial.print("Analog 1: ");    
    Serial.println(analog1);
    Serial.print("Analog 2: ");   
    Serial.println(analog2);
    Serial.print("Analog 3: ");    
    Serial.println(analog3);
    Serial.print("Analog 4: ");
    Serial.println(analog4);
    Serial.print("Analog 5: ");
    Serial.println(analog5);
    Serial.print("Analog 6: ");
    Serial.println(analog6);
    Serial.print("Analog 7: ");
    Serial.println(analog7);
    Serial.print("Analog 8: ");
    Serial.println(analog8);
    Serial.print("Analog 9: ");
    Serial.println(analog9);
    Serial.print("Analog 10: ");
    Serial.println(analog10);
    Serial.print("Analog 11: ");
    Serial.println(analog11);
    Serial.print("Analog 12: ");
    Serial.println(analog12);
    Serial.print("Analog 13: ");
    Serial.println(analog13);
    Serial.print("Analog 14: ");
    Serial.println(analog14);
    Serial.print("Analog 15: ");
    Serial.println(analog15);

    digitalWrite(greenLEDpin, LOW);
    digitalWrite(greenLEDpinExternal, LOW);

    if ((millis() - syncTime) < SYNC_INTERVAL) return; // Limits the speed of the logging

    syncTime = millis();
  }

    else{ // If not logging, turn off the LEDs
      digitalWrite(greenLEDpin, LOW);
      digitalWrite(greenLEDpinExternal, LOW);
    }
}

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: Data Logger Program Not Recognizing Card

Post by adafruit_support_bill »

I had to disable the LSM303 initialization because I don't have one handy. But the code seems to get into a reset loop initializing the SD card.

If I comment out a bunch of the print statements, it is able to initialize the card. You clearly have a memory problem there.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”