Print to SD card works in setup but not in loop

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
theinzen
 
Posts: 55
Joined: Sat Apr 08, 2017 10:55 am

Print to SD card works in setup but not in loop

Post by theinzen »

I am using a myFile.print statement and it works in the set up but not the loop. I had some code that worked fine when I used the Metro MO Express with a data logging shield but when I switched to the Feather MO adalogger the myFile.print statement works in the setup but not in the loop. To demonstrate this, I just modified the SD example code.

Code: Select all

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("Datalog.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("Datalog.txt");
  if (myFile) {
    Serial.println("Datalog.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  myFile = SD.open("Datalog.txt");
  myFile.println("test");
   myFile.close();
  delay(1000);
 Serial.print("loop is running");

}
Can somebody tell me why the myFile.print statement does not work in the loop? All I get is the "testing 1, 2, 3." line.

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

Re: Print to SD card works in setup but not in loop

Post by adafruit_support_bill »

Your setup code is not the same as your loop code.

In setup, you open the file for write access:

Code: Select all

myFile = SD.open("Datalog.txt", FILE_WRITE);
In the loop, you do not specify write access:

Code: Select all

 myFile = SD.open("Datalog.txt");
If you do not specify write access, the open() defaults to read-only;
https://www.arduino.cc/en/Reference/SDopen

User avatar
theinzen
 
Posts: 55
Joined: Sat Apr 08, 2017 10:55 am

Re: Print to SD card works in setup but not in loop

Post by theinzen »

So here is my full code. I still can print to myFile in the loop. It only works in the setup. Thoughts?

Code: Select all

#include <Stepper.h>
#include <Adafruit_MLX90614.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614(); //BANNED temp sensor
#include <Wire.h>
#include <SD.h>
#include <Arduino.h>
#include "RTClib.h"
#include <SPI.h>
#include "Adafruit_MAX31855.h" //TC Modules
#include <elapsedMillis.h>
elapsedMillis timeElapsed;
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 13, 12, 6, 5); // initialize the stepper library on pins 2 through 5:
double temp1 = 0.00;
double temp2 = 0.00;
double temp3 = 0.00;
double temp4 = 0.00;
#define MAXDO  10 // Define TC SPI pins
#define MAXCLK 0
#define MAXCS1 MISO
#define MAXCS2 11
#define MAXCS3 1
#define MAXCS4 9
Adafruit_MAX31855 thermocouple1(MAXCLK, MAXCS1, MAXDO);// initialize the Thermocouples
Adafruit_MAX31855 thermocouple2(MAXCLK, MAXCS2, MAXDO);
Adafruit_MAX31855 thermocouple3(MAXCLK, MAXCS3, MAXDO);
Adafruit_MAX31855 thermocouple4(MAXCLK, MAXCS4, MAXDO);
RTC_DS3231 rtc;
File myFile;
const int chipSelect = 4;
unsigned int interval = 60000;
int count = 0;
int start = 0;
int stepenable = SCK;
double height = 0.00;
int dir = 1;
double toptemp = 0.00; 
String stringVal = "";
String stringVal2 = "";
int i = 0;
int count2 = 0;
int j = 0;

void setup() {
Serial.begin(9600);

  if (!SD.begin(4)) {

    return;
  }

  Wire.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  myFile = SD.open("Datalog.txt", FILE_WRITE);
  myFile.println("Date Time Temp1 Temp2 Temp3 Temp4 Height");
  myFile.close();
  pinMode(stepenable, OUTPUT);
  digitalWrite(stepenable, LOW);
  Serial.begin(9600);

  
}

void loop() {
  
  temp1 = thermocouple1.readFarenheit();
  temp2 = thermocouple2.readFarenheit();
  temp3 = thermocouple3.readFarenheit();
  temp4 = thermocouple4.readFarenheit();  
 
  DateTime now = rtc.now();
  myFile = SD.open("Datalog.txt", FILE_WRITE);
  myFile.print(now.year(), DEC);
  myFile.print('/');
  myFile.print(now.month(), DEC);
  myFile.print('/');
  myFile.print(now.day(), DEC);
  myFile.print(' ');
  myFile.print(now.hour(), DEC);
  myFile.print(':');
  myFile.print(now.minute(), DEC);
  myFile.print(':');
  myFile.print(now.second(), DEC); myFile.print(" ");
  myFile.print(temp1); myFile.print(" "); myFile.print(temp2); myFile.print(" "); myFile.print(temp3); myFile.print(" "); myFile.print(temp4); myFile.print(" ");
  myFile.close();

  if (timeElapsed <= interval && j != 0) {

    myFile = SD.open("Datalog.txt", FILE_WRITE);
    myFile.println("");
    myFile.close();
  }

  if (timeElapsed > interval || start == 0)
  {
    digitalWrite(stepenable, HIGH);
    // step one step:
      myStepper.step(dir);
      delay(100);
      myStepper.step(dir);
      delay(80);
      myStepper.step(dir);
      delay(60);
      myStepper.step(dir);
      delay(40);
      myStepper.step(dir);
      delay(20);
      myStepper.step(dir);
      delay(10);
      myStepper.step(dir);
      delay(7);
      myStepper.step(dir);
      delay(5);
      myStepper.step(dir);
      delay(3);
    for (int i = 0; i < 450; i++) {
      myStepper.step(dir);
      delay(3);
    }

    for (int i = 0; i < 330; i++) {
      myStepper.step(dir);
      height = (analogRead(A0) / 1023.00) * 5.00; 
      stringVal2 = stringVal2 + String(height) + " ";
      count = count + 1;
      if (count > 30) {
        toptemp = (mlx.readObjectTempF());
        stringVal = stringVal + " " + String(toptemp);
        myFile.close();
        count = 0;
      }
      delay(3);
    }

    for (int i = 0; i < 450; i++) {
      myStepper.step(dir);
      delay(3);
    }
    myFile = SD.open("Datalog.txt", FILE_WRITE);
   
    myFile.print(stringVal2);
    myFile.println(stringVal);
    myFile.close();
    stringVal = "";
    stringVal2 = "";
    timeElapsed = 0;
    
    digitalWrite(stepenable, LOW);
    dir = dir * -1;
    j = 1;
    start = 1;

  } delay(1000);
}



  

User avatar
theinzen
 
Posts: 55
Joined: Sat Apr 08, 2017 10:55 am

Re: Print to SD card works in setup but not in loop

Post by theinzen »

OK, figured it out. Can't use the sd card spi pins for digital outputs.....

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

Re: Print to SD card works in setup but not in loop

Post by adafruit_support_bill »

Yea, pins 12 & 13 would conflict with card communication.

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

Return to “Arduino”