"Error opening data.txt" Troubles with SD cards

Have been trying to make a code for a custom machine that records time, angle, and torque and record at (roughly) half second intervals and after enough data is collected, it will print those data arrays to a SD card using a "OSEPP Real-time Clock & microSD Breakout" (OSEPP - Real-time Clock & microSD Breakout). I have looked at multiple similar issues and tried all of their changes such as shortening the length of the file name as well as adding delay. It sometimes works but a lot of the time is crashes and makes the file but doesn't have anything in the file. I am using 32 gb sd cards and an uno. Any answers to help steer me in the right direction are very welcomed. Thank you for your time.

Sorry if this isn't the correct format for this question but I am rather new to this. 
/*************************************************
OSEPP Real-time clock and microSD card example

Use the DS1307 RTC for accurate time keeping and log timestamps, 
sensor readings and other data with the microSD. 

How to wire:
Date and time functions using a DS1307 RTC connected via I2C and Wire lib
SD card attached to SPI bus as follows:
-MOSI - pin 11
-MISO - pin 12
-CLK -  pin 13
-CS  -  pin 4

   Copyright (C) 2016  Daniel Jay

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
*************************************************/
 
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

#include "RTClib.h"

bool looping = true;
int cw = 2;    
int cws = 4;
const int OUTPUT_PIN = 5;
int reset = 7;    
int save = 8;
int stepPin = 6;    
int dirPin = 9;  
int loadCell = A0;
int torque = 0;
float torqueFloat = 0.0;
int spin1 = 0;
int spin2 = 0;
int newData = 0;
String file = "";
int arraySize = 15;
float Time[15];
float Angle[15];
float Torque[15];
int dataPoints = 0;
String helper = "";
int i = 0;
int times = 0;
float Max = 0;
File myFile;

RTC_DS1307 RTC;
const int chipSelect = 10;
 
void setup () {
    Serial.begin(9600);
    delay (2000);
   
    Wire.begin(); 
    RTC.begin();
  
 
  //if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  //}
  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);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT); 
  digitalWrite(OUTPUT_PIN, HIGH);
  digitalWrite(stepPin, LOW);         //sets the pulse to low (zero volts) 
  digitalWrite(dirPin, HIGH);
  pinMode(OUTPUT_PIN, OUTPUT);
  pinMode(cw, INPUT_PULLUP);      //sets the pin 6 in the MEGA as an input for the limit switch 
  pinMode(reset, INPUT_PULLUP);    
  pinMode(cws, INPUT_PULLUP);      //sets the pin 6 in the MEGA as an input for the limit switch 
  pinMode(save, INPUT_PULLUP);    
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop () {
 DateTime now = RTC.now();
 if (digitalRead(cw)== LOW){
    while (digitalRead(cw)== LOW){
    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(10000);         
    digitalWrite(stepPin, LOW);
    delayMicroseconds(10000);
    spin1++;
    if (spin1 % 25 == 0){
     times = millis();
     torque = (analogRead(loadCell));
     torqueFloat = torque * 1.0;
     torqueFloat = ((torqueFloat/1023)*50);
     if (dataPoints < arraySize){
     Time[dataPoints] = spin1*.02+spin2*.025;
     Torque[dataPoints] = torqueFloat;
     Angle[dataPoints] = (.02353*spin1)+(.0227*spin2);
     //dataString[dataPoints] = ("Angle: " + String(spin) + " Time: " + time1 + "." + time2 + " Torque: " + String(torqueFloat));
     dataPoints = dataPoints + 1;
     }
     else {
     Serial.println("error"); 
     }
     Serial.println(Time[dataPoints-1]);
     Serial.println(Angle[dataPoints-1]);
     //Serial.println(dataPoints);
    }
    }
    newData = 2;    
  }
  if (digitalRead(cws)== LOW){
    while (digitalRead(cws)== LOW){
    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(12500);         
    digitalWrite(stepPin, LOW);
    delayMicroseconds(12500);
    spin2++;
    if (spin2 % 20 == 0){
     torque = (analogRead(loadCell));
     torqueFloat = torque * 1.0;
     torqueFloat = ((torqueFloat/1023)*50);
     if (dataPoints < arraySize){
     Time[dataPoints] = spin1*.02+spin2*.025;
     Torque[dataPoints] = torqueFloat;
     Angle[dataPoints] = (.02353*spin1)+(.0227*spin2);
     dataPoints = dataPoints + 1;
     }
     else {
     Serial.println("error"); 
     }
     Serial.println(Time[dataPoints-1]);
     Serial.println(Angle[dataPoints-1]);
    }
    }
    newData = 2;
  }  
  
  if (digitalRead(save)== LOW && newData > 1){ //slow is slow enough
   torque = (analogRead(loadCell));
   torqueFloat = torque * 1.0;
   torqueFloat = ((torqueFloat/1023)*50);
   Time[dataPoints] = spin1*.02+spin2*.025;
   Torque[dataPoints] = torqueFloat;
   Angle[dataPoints] = (.02353*spin1)+(.0227*spin2);
   dataPoints = 0;
   //file = ("data.txt");
   myFile = SD.open("data.txt", FILE_WRITE);
   //delay(2000);
    // if the file is available, write to it:
   if (myFile) {
    i = 0;
      while (i<= arraySize){
        if (Max <= Time[i]){
          myFile.println(("Angle: " + String(Angle[i]) + " Time: "+String(Time[i]) + " Torque: "+String(Torque[i])));
          delay(3000);
          Serial.println("working");
          Max = Time[i];
        }
        i++;
      }
      Max = 0;
      myFile.close();
      Serial.println("Done");
    }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening data.txt");
  }
  newData = 1;
  }
  
  if (digitalRead(reset)== LOW && newData > 0){
    spin1 = spin1 + spin2;
    while (spin1 > 0){
      digitalWrite(dirPin, LOW);
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(6000);         
      digitalWrite(stepPin, LOW);
      delayMicroseconds(6000);
      spin1 = spin1 - 1;
    }
    dataPoints = 0;
    spin2 = 0;
    digitalWrite(OUTPUT_PIN, LOW);
}
}

did you run the CardInfo diagnostics example?

I think I did and nothing too out of the ordinary came up. I would love to run it again but the next time I'll be in lab will be Monday. Is there anything I should be looking for in particular?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.