Hello, I'm using a load cell to measure force in Newtons and saving the data using an adafruit logging shield.
Using:
- HX711 breakout with 5kg load cell link:(https://www.amazon.com/gp/product/B07CN1VDC4/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1)
- Adafruit data logging shield
- Arduino Mega 2560
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();
}
}