Won't print to an SD card

So I’ve written this rather niffty code you see below to measure pressure and RPM inputs and it all worked perfectly UNTIL I tried to make it print the data to an SD card on the Adafruit Data Logger Shield. I’m not really sure why it’s not working, which is pretty much entierly why I’m here I suppose xD Anyways though, help me por favor <3

// This code measure the frequency of an incoming 
// square-wave signal using Timer/Counter 1 and the onboard 
// comparator. The signal I am measuring has a duty-cycle of 
// 50% but the code should work regardless of the duty-cycle.
// It should also work for non-square-wave signals so long as
// the signal voltage drops below the bandgap voltage of the
// onboard comparator (approximately 1.1 V) and is sufficiently
// clean to prevent bouncing. 

// A variable to keep track of the number of overflows on 
// Timer/Counter 1.
volatile unsigned int overflows = 0; 

// A variable to keep track of how many rising edges of the 
// signal have been counted.
volatile unsigned long edges = 0; 

// A variable to keep track of the count on Timer/Counter 1
// when I start counting the edges of the signal.
volatile unsigned long tstart = 0;

// A variable to keep track of the count on Timer/Counter 1
// when I stop counting the edges of the signal.
volatile unsigned long tstop = 0;

// A variable to store temporarily store the count on 
// Timer/Counter 1.
volatile unsigned long tnow = 0;

// This specifies how many cycles over which I want to 
// average the frequency.
const unsigned long cycles = 5; 

// A variable to store the currently measured frequency
float frequency = 0;

  Analog input, analog output, serial output
 Reads an analog input pin, maps the result to a range from 0 (0.5v) to 500 (4.5v)
 and uses the result to set the pulsewidth modulation (PWM) of an output pin.
 Also prints the results to the serial monitor.
 The circuit:
 * The analog signal is run through pin 0.
   Center pin of the potentiometer goes to the analog pin.
   side pins of the potentiometer go to +5V and ground

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 13; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pressure sensor
int outputValue = 0;        // value output to the PWM (analog out)

//Calling upon file for the clock
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

//Initial setup for SD Card
#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;

File dataFile;

void setup(void) {
    boolean checkIfExists;
    dataFile = SD.open("datalog.txt", FILE_WRITE);  //Opens the data file we are going to log to
    if (dataFile){
    pinMode(7,INPUT); // This is the analog comparator negative input.
    // This is where the input signal enters the Arduino.

    SREG = SREG | B10000000; // Enable gobal interrupts. They should 
    // already be enabled but I like to do this out of good measure.

    //Serial.begin(9600); // For printing the frequency to the terminal
    #ifdef AVR
      Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due

    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__));
       DateTime now = rtc.now();
    dataFile.print(now.year(), DEC);
    dataFile.print(now.month(), DEC);
    dataFile.print(now.day(), DEC);
    dataFile.print(' ');
    dataFile.print(now.hour(), DEC);
    dataFile.print(now.minute(), DEC);
    dataFile.println("Time, Pressure, Frequency");

void loop(void) {
    // make a string for assembling the data to log:
    String dataString = "";
    // read three sensors and append to the string:
    // read the analog in value:
    sensorValue = analogRead(analogInPin);            
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 133, 951, 0, 198);  
    // change the analog out value:
    analogWrite(analogOutPin, outputValue);           
    // print the results
    dataFile.print(", ");   
    dataFile.print(", "); 
    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:

void measureFreq(void) {
    edges = 0;

    ACSR = ACSR | B01000010; // enable analog comparator interrupt 
    // on failing edge (bit 1) which would actually capture a rising
    // edge of the signal and use the internal bandgap reference
    // voltage as the positive input (bit 6).
    delay(5); // A short wait for bandgap voltage to stabilize.

    overflows = 0;

    TCCR1A = B00000000; // Set Timer/Counter 1 in normal mode where 
    // it will count to 0xFFFF then repeat.
    TIMSK1 = TIMSK1 | B00000001; // Turn on Timer/Counter 1 overflow 
    // interrupt (bit 0).
    // Turn on the counter with no prescaler.
    TCCR1B = TCCR1B | B00000001;

    ACSR = ACSR | B00001000; // Enable analog comparator interrupt 
    // (bit 3).
    // Calculate the frequency.
    frequency = (float)16000000*(float)cycles/(float)(tstop - tstart);


    overflows += 1;

ISR(ANALOG_COMP_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt
    tnow = TCNT1; // current time
    edges += 1;
      if (edges == 1) { // Start counting edges.
          tstart = overflows*65536 + tnow;      }
      else if (edges == cycles + 1) { // Stop counting edges.
           tstop = overflows*65536 + tnow;
           // Turn off Timer/Counter 1 and the comparator.
           ACSR = 0; //Analog Comparator Control and Status Register set to zero
           TCCR1B = 0;

All the # statements need to go before any code, you have code between them here:

//Calling upon file for the clock
#include <Wire.h> <<<<< move these to the top of the sketch
#include “RTClib.h” <<<<< move these to the top of the sketch

RTC_DS1307 rtc;
//Initial setup for SD Card
#include <SPI.h> <<<<< move these to the top of the sketch
#include <SD.h> <<<<< move these to the top of the sketch

So I followed your instructions and put the # items at the top of the code but it still doesn't print to the SD card. I made a file on the SD card titled datalog.txt just like it appears in the code to see if that was the problem, it wasn't. (Just a side note, the SD card is formatted correctly. It is a 8GB SanDisk SDHC Card formatted with the 32 FAT system which is supposedly supported by Arduino and the Adafruit Data Logger Shield.)

How much memory do you have available? The SD class needs 512 bytes per open file.


The SD cards are completely new, the full 8 GB should be available.

The SD cards are completely new, the full 8 GB should be available.

That is completely irrelevant. How much memory is available ON THE ARDUINO?

How did you format the cards? fat16lib wrote & maintains the sdfat.h library, and I think he recommends they only be formatted thusly: https://www.sdcard.org/downloads/formatter_4/

https://code.google.com/p/sdfatlib/ He regularly posts test results too.

Fixed it. The problem was simply that the code was missing a flush(); command or some other form of save, thanks for the help! :D