Timestamp on Load Cell Data Saved to SD Card

Hi,

I am a very novice Arduino user who needs some help with my current project. I'm using a load cell and saving the data to an SD card. The only vital piece I'm missing is a timestamp. I just need the simplest way to record the time the measurements were taken. It does not have to be the real time, it can be the time from when the Arduino began to record.

Attached is the code I wrote that successfully records the data. All I need is the time.

Any help is appreciated.

sketch_apr29.ino (702 Bytes)

millis() returns the number of milliseconds the Arduino has been on for. It is a unsigned 32 bit number (unsigned long) and will overflow after about 49+ days.

unsigned long timestamp = millis();

kellyneedshelp:
It does not have to be the real time, it can be the time from when the Arduino began to record.

Since you are saving data to SD at regular intervals, why do you want to use a timer - of any sort? You already know what the time interval is, and you can be pretty sure that the second event happened immediately after the first. You may want to send a count for reference purposes, but that would be about all, wouldn't it?

OP's code:

#define DOUT  3
#define CLK  2
#define calibration_factor -7050.0

HX711 scale;

float weightKg;

int chipSelect = 10;
File mySensorData;


void setup() {
  Serial.begin(9600);
  SD.begin(10);
  scale.begin(DOUT, CLK);
  scale.set_scale(calibration_factor); 
  scale.tare(); 



}

void loop() {


  weightKg = scale.get_units();

  mySensorData = SD.open("Data0429a.txt", FILE_WRITE);
  if (mySensorData){
  Serial.print("Reading: ");
  Serial.print(weightKg); 
  Serial.println(" kg"); 

mySensorData.print(weightKg);

delay(1000);
mySensorData.close();
  }
}

You aren't printing any whitespace between readings so they will run into each other.

Its wise to use flush after every reading written.

Your code currently overwrites the file every time round loop() - that can't be the intention.