Using Adafruit SD Logging Shield with HX711 Breakout + Load Cell

Hello, I’m using a load cell to measure force in Newtons and saving the data using an adafruit logging shield.
Using:

SCK is connected to digital pin 51
DOUT is connected to digital pin 50

Ideally the serial monitor should display that the card has been initialized, the file name should be displayed then the load cell data should begin with time in millis on the left and force in newtons on the right. The data from the serial monitor should then be saved to the file.

Instead what i get is that the card is initialized, the file name is displayed, the left column shows increasing numbers not in millis and the right shows a constant zero.( I will attach a photo of the serial monitor below. )Also, when I open the newly made CSV file it only contains the titles. I’m not sure how to fix this as the load cell produces good numbers when the sketch does not have the data logger included.

So my questions are :
How do I get the serial monitor to display correct elapsed time and force data?
Why isn’t everything being saved to the SD as opposed to just the titles?

#include <SD.h>
#include <Wire.h>
#include <SPI.h>
#include <HX711.h>
//#include "RTClib.h"

#define DOUT 51//51 2
#define CLK 50 //50 3
HX711 scale(DOUT, CLK);
float calibration_factor = -43910;  //-43910;      -431210

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
 
// the logging file
File logfile;
char filename[] = "LOGGER00.CSV";

//=============================================================

void setup() {
  Serial.begin(9600);
  
  while(!Serial){
    ;
    }

  pinMode(chipSelect, OUTPUT);
  
  Serial.print("Initializing SD card...");
    if(!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    }
    else {
  Serial.println("card initialized.");
    }
    
  Serial.print("Creating new file...");
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }

   logfile = SD.open(filename, FILE_WRITE);
   if (!logfile) {
    Serial.println("Couldn't create file");
   }  
    else {
  Serial.print("Logging to: ");
  Serial.println(filename);
  logfile.println("time, force");
  logfile.close(); 
    }   
 //Serial.print("x");   //debugging : this is where the code stops working 

scale.begin(DOUT, CLK);
scale.set_scale(calibration_factor);  //use number obtained from calibration sketch
scale.tare();


  }

//==============================================================
void loop() {

uint8_t runTime = millis( );
//logfile = SD.open(filename, FILE_WRITE);  
logfile.print(runTime);
logfile.print(',');
logfile.println(scale.get_units(),3);
logfile.close();


Serial.print(runTime);
Serial.print(",");
Serial.println(scale.get_units(),3); 

if(Serial.available())
{
  char temp = Serial.read();
   if(temp == 't' || temp == 'T')
      scale.tare();
  }
}

You create the log file in setup() and then close it. However you don’t open the file again in loop(). Maybe uncomment SD.open() so you have a file to write to.

Depending on how quickly you need to log data, you may need to open the file just once and keep it open to log continuously, rather than opening and closing the file for each sample.

Does the SD card code in setup() not throw an error as once you have the next available filename and you’ve opened it, you then open the file again without closing it?

I’ve uncommented the SD.open() in the loop and still having the same issue.

No error popped up, but I have removed the second unnecessary SD.open() in the setup.

I’m still not understanding why elapsed time is incorrect.

EDIT

After reading through Jeremy Blum’s Exploring Arduino I realized that the logging shield connects to SPI via pins 50,51 and 52 on the Mega. I switched the HX711 pins from 50 and 51 to 32 and 33 and everything functioned as expected. Elapsed time was still off so I changed the uint8_t time stamp to long.

You should change the data type for runTime to unsigned long, which is what millis() returns.

Why do you think the elapsed time is incorrect?

Post an example of the serial port output so we can see what you mean by "elapsed time is incorrect".