Recording multiple data points to SD, logged data repeating previous records.

Hi all,

I am looking for some advice regarding my programming.

I have multiple sensors recording various data points, temp, distance (utilising analog and dig interface).

I receive all the data, timestamped for later analysis.

However, when I review the text file I see that the new timestamped data and data that was recorded previously has been reprinted.

I am transferring the information via a String into a file, but cannot figure out why it is repeating the old information on every print to file instruction.

void dataLogger(float *temp, float *temp1, float *MCP9808, long *distance) {
  
  tmElements_t tm;
  
  if (RTC.read(tm)) {
    dataString = "";
    dataLogger2digits(tm.Hour);
    dataString += ":";
    dataLogger2digits(tm.Minute);
    dataString += ":";
    dataLogger2digits(tm.Second);
    dataString += ",";
    dataLogger2digits(tm.Day);
    dataString += "/";
    dataLogger2digits(tm.Month);
    dataString += "/";
    dataString += String(tmYearToCalendar(tm.Year));
    dataString += ",";
    dataString += String(*temp);
    dataString += ",";
    dataString += String(*temp1);
    dataString += ",";
    dataString += String(*MCP9808);
    dataString += ",";
    dataString += String(*distance);
  }
  //open the file
  File dataFile = SD.open("datalog.txt",FILE_WRITE);

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    //print to the serial port too
    Serial.println(dataString);  //Check to see if this is an easier way to print to Serial port
  }
  //If the file is not open, pop up an error message
  else  {
    Serial.println("Error opening datalog.txt"); 
  }
}
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>
#include "Adafruit_MCP9808.h"
#include <TimeLib.h>
#include <DS1307RTC.h>


#define trigPin 40
#define echoPin 41
#define DS1307_CTRL_ID 0x68 // Define constant for RTC I2C Address

File myFile;

const int chipSelect = 53; //Can be altered
int ticks = 0, old_tick_value = 0; // Variables to count square wave pulses
int TMP36 = 0,testTMP36 = 1, blueLED = 23, waterHigh = 33;
float supplyVoltage = 5;
String dataString = "";

  float temp, temp1, MCP9808;
  long duration, distance;

Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

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

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

  // Attach Interrupt to pin D2
  attachInterrupt(0,handleInt,FALLING);
 
  // Set Square Wave at 1Hz
  setSQW(0x10);
  
  //Detect MCP9808 is found over I2C bus
  //Address with tempsensor.begin(0x19) for exmple
  //Check if tempsensor.begin(0x18) is correct
  if (!tempsensor.begin(0x18))
  {
    Serial.println("Could not find MCP9808!!");
    while (1);
  }

  //Intialize Temperature from internal and external TMP36 and Ultrasonic Sensor
  temperatureTMP36(&temp, &temp1);
  ultraSound(duration, &distance);
    
  //Set up LCD number of columns and rows:
  lcd.begin(16, 2);
  //Test LCD screen
  lcd.print("Sensor Test");

  pinMode(blueLED, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(waterHigh, OUTPUT);
}

void loop() {
  //Read temp TMP36 and Ultrasound every 10 seconds to allow them to stabilize
  if (ticks == 10)  {
    temperatureTMP36(&temp, &temp1);
    ultraSound(duration, &distance);
    ticks = 0;
  }

  //Temperature from MCP9808
  MCP9808 = tempsensor.readTempC();

  //Distance obtained from Ultrasonic Sensor
  ultraSound(duration, &distance);

  //Send data to LCD screen
  LCD(&temp, &MCP9808);

  // Update serial monitor display if a second has elapsed  
  if (ticks != old_tick_value) {
        old_tick_value = ticks;
        //Read sensors and append to the string
        dataLogger(&temp, &temp1, &MCP9808, &distance);
        //printCurrentTime(temp, temp1, MCP9808, distance);
  }
}

// Interrupt Handler
void handleInt() {
  digitalWrite(blueLED, HIGH);
  delay(100);
  digitalWrite(blueLED, LOW);
  ticks++;
}

// Setup RTC Module for 1 Hz square wave
void setSQW(uint8_t value) {
  Wire.beginTransmission(DS1307_CTRL_ID);
  Wire.write(7);
  Wire.write(value);
  Wire.endTransmission();
}

// Format numbers as 2-digit numbers for data logger
void dataLogger2digits(int number) {
  if (number >= 0 && number < 10) {
   dataString += "0";
  }
  dataString += String(number);
}

void dataLogger(float *temp, float *temp1, float *MCP9808, long *distance) {
  
  tmElements_t tm;
  
  if (RTC.read(tm)) {
    dataString += "\n";
    dataLogger2digits(tm.Hour);
    dataString += ":";
    dataLogger2digits(tm.Minute);
    dataString += ":";
    dataLogger2digits(tm.Second);
    dataString += ",";
    dataLogger2digits(tm.Day);
    dataString += "/";
    dataLogger2digits(tm.Month);
    dataString += "/";
    dataString += String(tmYearToCalendar(tm.Year));
    dataString += ",";
    dataString += String(*temp);
    dataString += ",";
    dataString += String(*temp1);
    dataString += ",";
    dataString += String(*MCP9808);
    dataString += ",";
    dataString += String(*distance);
  }
  //open the file
  File dataFile = SD.open("datalog.txt",FILE_WRITE);

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    //print to the serial port too
    Serial.println(dataString);  //Check to see if this is an easier way to print to Serial port
  }
  //If the file is not open, pop up an error message
  else  {
    Serial.println("Error opening datalog.txt"); 
  }
}

void temperatureTMP36(float *temp, float *temp1) {

  float voltage, testVoltage, reading, testReading;
  
  reading = analogRead(TMP36);
  testReading = analogRead(testTMP36);

  voltage = reading * supplyVoltage / 1024;
  testVoltage = testReading * supplyVoltage / 1024;

  //Convert from 10mV per degree with 500mV offset as per tecspecs
  //Alteration to allow for calibration to be close to MCP9808;
  *temp = (voltage - 0.5) * 100; 
  *temp1 = (testVoltage - 0.5) * 100;
  
  return;
}
  
void ultraSound(long duration, long *distance) { 
  long period, measure;
  
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  *distance = (duration/2) / 29.1;
  
  return;
}

void LCD(float *temp, float *MCP9808) {

P.S. Sorry if I haven't followed the posting procedure, first time asking a question on the forum and have attempted to follow the instructions.

DATALOG.TXT (498 KB)

why is there a test involving the "read"?

if (RTC.read(tm)) {

if that test fails, should the following block of code writing to the file be executed?