Can't write to SD card in a loop

Hi, I’m trying to finish up a project right now that creates a timestamp, using a real time clock, every time that the button is pushed, then stores it to the SD card in the datalogger. I’m working on an Uno board, with a datalogger and a real time clock both from adafruit. I can get the program to create the file and write a timestamp to it once, but it won’t write repeatedly, even stopping in the serial monitor too.

The Deb.button_and_counter file is one I wrote that works exactly how I want it to and shows up in the serial monitor in the format I’m looking for, except it doesn’t write to the SD card. I basically just want what is in the serial monitor to go straight to my text file on the SD card. The unit also needs to be standalone so I can’t use hyperterminal or any programs like that.

In the Test file I can get it to write to the SD card with the info and formatting I want but it only records the one button press/event. Sometimes I can get two if I press the button before starting the serial monitor and then pressing it again after start but that is the limit.

Can anyone help me figure out how to make it so that I can actually record more than one or two button presses for my loop? This is my first “large” project with arduino so please excuse all of the commented out code, I kept it around in case I needed to revert to previous designs.

Test.ino (2.74 KB)

Deb.button_and_counter.ino (2.99 KB)

You are checking for the changing button twice, in a nested fashion.
If you remove the inner check, you should be able to save all button presses.

I commented out that second button check like below, but it still only records the one button push instead of repeated presses.

buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
buttonPushCounter++;
Serial.print ("number of button pushes: ");
Serial.println(buttonPushCounter);

// buttonState = digitalRead(buttonPin);
// if (buttonState != lastButtonState) {
// if (buttonState == HIGH) {

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

// if the file opened okay, write to it:
if (dataFile) {
Serial.print("Writing to test.txt...");
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print(' ');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.println();
buttonPushCounter++;
dataFile.print ("number of button pushes: ");
dataFile.println(buttonPushCounter);

What does the compiler say once your code is compiled i.e. how much free flash & sram?

Using an SD Card is borderline on an Uno.

Check if you have any warning for low SRAM memory.

Sketch uses 16,578 (51%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,256 (61%) of dynamic memory leaving 792 bytes for local variables. Maximum is 2,048 bytes.

No warnings come up.

In Deb.button_and_counter you do not reopen the file within the loop after you close it. Move file.open() into the loop as shown.
Please learn to post your code within the code tags and not as attachments as it is easier for people to see your code and help you.

#include <SD.h>

#include <SPI.h>


// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"



RTC_DS1307 RTC;

const int buttonPin = 2;
const int chipSelect = 10;

File dataFile;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

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

    Serial.print ("Initializing SD card...");
    pinMode (SS, OUTPUT);

    if (!SD.begin(chipSelect)) {
      Serial.println("Card failed, or not present");
      while (1) ;
    }
    Serial.println("card initialized.");

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // uncomment it & upload to set the time, date and start run the RTC!
    RTC.adjust(DateTime(2015, 8, 25, 14, 31, 0));
  }
//  dataFile = SD.open("datalog.txt", FILE_WRITE);
//  if (! dataFile) {
//    Serial.println("error opening datalog.txt");
//    while (1) ;
//  }

  
pinMode(buttonPin, INPUT);
}

void loop () {
    DateTime now = RTC.now();
       
    buttonState = digitalRead(buttonPin);
    if (buttonState != lastButtonState) {
        if (buttonState == HIGH) {
      Serial.print(now.year(), DEC);
      Serial.print('/');
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.day(), DEC);
      Serial.print(' ');
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.println();
      delay(500);      
      buttonPushCounter++;
      Serial.print ("number of button pushes: ");
      Serial.println(buttonPushCounter);
      
       dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    while (1) ;
  }

     if (dataFile) {
      Serial.print ("Writing to Log.txt");
      dataFile.print(now.year(), DEC);
      dataFile.print('/');
      dataFile.print(now.month(), DEC);
      dataFile.print('/');
      dataFile.print(now.day(), DEC);
      dataFile.print(' ');
      dataFile.print(':');
      dataFile.print(now.minute(), DEC);
      dataFile.print(':');
      dataFile.print(now.second(), DEC);
      dataFile.println();

      
      dataFile.close();
      Serial.println("done.");
     } else {
      Serial.println("error opening dataLog.txt");
     }
    }
    else {
      Serial.println("off");
    }
    int reading = digitalRead(buttonPin);
    lastButtonState = buttonState;
    }
}
//
//    dataFile = SD.open("Log.txt");
//    if (dataFile) {
//      Serial.println("Log.txt");
//
//      while (dataFile.available()) {
//        Serial.write(dataFile.read());
//      }
//      dataFile.close();
//    } else {
//      Serial.println("error opening test.txt");
//    }

//    String dataString = "";
//    for (int buttonPin = 2) {
//      int button = digitalRead(buttonPin);
//      dataString += String(button);
//    }
//    dataFile.println(dataString);
//    dataFile.flush();
//    delay(500);