Project is looping the setup

Hello!

I'm attempting to do this project, where I write data measured by an accelerometer & altimeter to a SD card. The output, however, is behaving very weirdly. Instead of writing, it repeats the setup. Nothing is written to the file.

/*############### VIRTUABOTIX BASIC ACCELEROMETER CODE #########################
This code is meant to serve as the most basic example of how to read out
from the X Y and Z axis as raw sensor readings.

            ######################################################
 TO PIN A0  #                                                    # To 5 Volts
       ######                                                    ######
       #    #     X Axis                              5 Volt     #    #
       ######                                                    ######
 TO PIN A1  #                                                    #
       ######                                                    ######
       #    #     Y Axis                              3.3 Volt   #    #
       ######                                                    ######
 TO PIN A2  #                                                    # To Ground
       ######                                                    ######
       #    #     Z Axis                              Ground     #    #
       ######                                                    ######
 TO PIN 2
 #                                                    #
       ######                                                    ######
       #    #     Sleep                 GS (Sensitivity Select)  #    #
       ######                                                    ######
            #                                                    #
       ######                                                    ######
       #    #     0G                             ST (Self Test)  #    #
       ######                                                    ######
            #                                                    #
            ######################################################
    
 * Find more free code, and great products at http://www.virtuabotix.com/
 * ------------------------------------------------------------------------------
 * $Author: Mr. Joe $
 * $Date: 2011-11-5 $
 * $Revision: 0     $
 * ------------------------------------------------------------------------------
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
################################################################################*/

int sleepPin= 2; //Turning sleep high turns on the Accelerometer
int xpin= A0;
int ypin = A1;
int zpin = A2;
int gpin = A3;
#include <Wire.h>
#include "IntersemaBaro.h"
#include <SD.h>

Intersema::BaroPressure_MS5607B baro(true);


void setup() {
  Serial.begin(9600);
  baro.init(); 
  pinMode(sleepPin, OUTPUT);
  digitalWrite(sleepPin, HIGH);
  delay(500);
  pinMode(xpin, INPUT);
  digitalWrite(xpin, HIGH);
  delay(500);
  pinMode(ypin, INPUT);
  digitalWrite(ypin, HIGH);
  delay(500);
  pinMode(zpin, INPUT);
  digitalWrite(zpin, HIGH);
  delay(500);
  pinMode(gpin, INPUT);
  digitalWrite(gpin, HIGH);
  pinMode(10, OUTPUT);
  if (!SD.begin(4)) {
    return;  
  }
  Serial.println("Complete!");
  delay(2000);
}

void loop() {
  Serial.println("Attempting to open file.");
  File  myFile = SD.open("datalogger.txt", FILE_WRITE);
  Serial.println("File has opened");
  int alt = baro.getHeightCentiMeters();  
  if (myFile) {
      Serial.println("Writing to file");
      myFile.print("Time: ");
      myFile.print(millis()/1000); 
      delay(500);
      myFile.print(", Centimeters: ");
      myFile.print((float)(alt));
      delay(500);
      myFile.print(", Feet: ");
      myFile.println((float)(alt) / 30.48);
      delay(500);
      myFile.print("X Reading: "); 
      myFile.println(analogRead(xpin), DEC);
      delay(500);
      myFile.print("Y Reading: "); 
      myFile.println(analogRead(ypin), DEC);
      myFile.print("Z Reading: "); 
      delay(500);
      myFile.println(analogRead(zpin), DEC);
      myFile.print("0G Reading: ");
      myFile.println(analogRead(gpin), DEC);
      delay(500);
      myFile.println("----------------------------");
      myFile.close();
      Serial.println("File has been closed");
  }
  delay(5000);
}

Here is my output,

Complete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
A´Complete!
A´Complete!
AtComplete!
AtComplete!
AtComplete!
A´Complete!
AtComplete!
A´Complete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
AtComplete!
A´Complete!

I'm completely unsure what I'm doing wrong. Any help would be greatly appreciated.

Thank you!

I'd take out all the pinMode() and digitalWrite() stuff and everything in loop() and see if it still does that. Maybe it's the SD card that's making it crash?

Take it down to bare metal and add things back one at a time.

It seems like Watchdog Timer overflow. Did you open watchdog?

What is the return; in setup() supposed to do ?

I'd scatter a few delays around, Serial.println("Attempting to open file."); is going to take around 20ms to print, but it happens in the background, so the file open could have started and crashed, or something later.
I suspect memory overflow, and don't really see any evidence for a watchdog problem.
Try shifting those print strings into program memory with the F() macro.

Lack of power can cause a reset - how is your system powered?

Try removing the barometer and gyro and see if you can then use the SD card successfully.

So I tried using this as my code:

/*############### VIRTUABOTIX BASIC ACCELEROMETER CODE #########################
This code is meant to serve as the most basic example of how to read out
from the X Y and Z axis as raw sensor readings.

            ######################################################
 TO PIN A0  #                                                    # To 5 Volts
       ######                                                    ######
       #    #     X Axis                              5 Volt     #    #
       ######                                                    ######
 TO PIN A1  #                                                    #
       ######                                                    ######
       #    #     Y Axis                              3.3 Volt   #    #
       ######                                                    ######
 TO PIN A2  #                                                    # To Ground
       ######                                                    ######
       #    #     Z Axis                              Ground     #    #
       ######                                                    ######
 TO PIN 2
 #                                                    #
       ######                                                    ######
       #    #     Sleep                 GS (Sensitivity Select)  #    #
       ######                                                    ######
            #                                                    #
       ######                                                    ######
       #    #     0G                             ST (Self Test)  #    #
       ######                                                    ######
            #                                                    #
            ######################################################
    
 * Find more free code, and great products at http://www.virtuabotix.com/
 * ------------------------------------------------------------------------------
 * $Author: Mr. Joe $
 * $Date: 2011-11-5 $
 * $Revision: 0     $
 * ------------------------------------------------------------------------------
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
################################################################################*/

int sleepPin= 2; //Turning sleep high turns on the Accelerometer
int xpin= A0;
int ypin = A1;
int zpin = A2;
int gpin = A3;
#include <Wire.h>
#include "IntersemaBaro.h"
#include <SD.h>

Intersema::BaroPressure_MS5607B baro(true);


void setup() {
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  
   pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  delay(2000);
}

void loop() {
  Serial.println("Attempting to open file.");
  delay(50);
  File dataFile = SD.open("logger.txt", FILE_WRITE);
  delay(50);
  Serial.println("File has opened"); 
  delay(30);
  if (dataFile) {
    Serial.println("Writing to file");
    dataFile.println("MEOWMIX :D");
    delay(50);
    Serial.println("Random things have been written! :D");
    delay(50);
    dataFile.close();
    Serial.println("File has been closed");
    delay(1000);
  }
    else {
    Serial.println("error opening datalog.txt");
  } 
}

And this is my result:

Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.
Attempting to open file.
Initializing SD card...initialization done.

I'm really confused. What's going wrong?

Try shifting those print strings into program memory with the F() macro.

Sorry, I'm new to arduino programming. How would I do this?

Use the F() macro

Skywolf:
Sorry, I'm new to arduino programming. How would I do this?

For example on your first Serial.print() you have this:

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

To use the F() macro you would make it look like:

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

I know it isn't well documented on the Arduino reference pages. You have to know what page(s) to find the 1 or 2 lines that reference it's usage, almost off-hand... But, it is referenced quite often on this forum.

Nothing shows up in the serial when I do this. What does the F() do?

Actually it works, thanks!

Quick side question though. When I run the listfiles (Example sketch) later, my file doesn't show up. I believe that this is because my loop isn't saving the file once it's done. Is there any way I could have the file saved?

Thanks