DataLogger not working as intended

Good Morning,
I was wondering if someone could tell me where i messed up at.
back drop of project.
A0 Voltage sensor
A1 Current sensor
D7 LED (write indicator)

The project creates a new file every hour, a new folder every day, a new folder every month, and a new folder every year.

I can not get
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
in my code to work correctly. it is taking a time sample ever 1000 ms and i'm trying to change it to 500 ms and no change however 2000ms works as intended. on the default program it works as intended with both 500ms and 2000ms. I'm not sure what I broke.

Also as a part 2 to my project I want the header to stop writing to the file every time it's restarted/booted/powered on. I'm not sure where i broke that either as an older project i had did it just fine.

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27,4,20);
const int chipSelect = 10;
unsigned long previousMillis = 0;
const long interval = 500;
String previouspath = "";
#define LED1 7

void setup() {
  // Open serial communications and wait for port to open:
  pinMode(LED1, OUTPUT);
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");
  lcd.setCursor(0, 0);
  lcd.print("Init SD card...");
  //delay(1000);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    lcd.setCursor(0, 1);
    lcd.print("Card failed");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
  lcd.setCursor(0, 1);
  lcd.print("card initialized.");
Wire.begin();
if (!rtc.begin()) {
    Serial.println("RTC failed");
    lcd.setCursor(0,2);
    lcd.print("RTC failed");
}
}

void loop() {
  //Read RTC
 DateTime now = rtc.now();
  // create millis
  unsigned long currentMillis = millis();
  
  // make a string for assembling the data to log:
  String datastring = "";
  String path = "";
  String filename = "";
  String foldername = "";
  String date = "";
  String header = "";

  
if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
int V1 = analogRead(A0);
int C1 = analogRead(A1);

float Volt1 = V1 / (40.96);
float Amp1 = ((C1 - 511)/17.06); 
float Watt1 = Volt1 * Amp1;

datastring += String();
datastring += String(Volt1);
datastring += ",";
datastring += String(Amp1);
datastring += ",";
datastring += String(Watt1);
datastring += ",";
datastring += String(now.month(), DEC);
datastring += "/";
datastring += String(now.day(), DEC);
datastring += "/";
datastring += String(now.year(), DEC);
datastring += ",";
datastring += String(now.hour(), DEC);
datastring += ":";
datastring += String(now.minute(), DEC);
datastring += ":";
datastring += String(now.second(), DEC);

filename += String(now.hour(), DEC);
filename += ".csv";
 
foldername += String(now.year(), DEC);
foldername += String(now.month(), DEC);
foldername += String(now.day(), DEC);
  
path += String(foldername);
path += "/";
path += String(filename);

date += String(now.month(), DEC);
date += "/";
date += String(now.day(), DEC);
date += "/";
date += String(now.year(), DEC);
date += "  ";
date += String(now.hour(), DEC);
date += ":";
date += String(now.minute(), DEC);
date += ":";
date += String(now.second(), DEC);

header += "Volt,Amp,Watt,Date,Time";

 digitalWrite(LED1, LOW);
    SD.mkdir(foldername);  
  
  File dataFile = SD.open(path, FILE_WRITE);

  if (path != previouspath){
    dataFile.println(header);
    dataFile.close();
    previouspath = String(path);
    }
    else { 
         }
if (dataFile) {
  dataFile.println(datastring);
  dataFile.close();
}
  
  dataFile.println(datastring);  
  lcd.init();
  lcd.setCursor(0, 0);
  lcd.print(datastring);
  Serial.println(datastring);


}

 digitalWrite(LED1, HIGH);

}









Hi,
on this line you are creating a new directory with the same name every 500 ms.

SD.mkdir(foldername);

Check how long it takes loop to loop by printing millis each pass

Use unsigned long for millis related variables.

valid point, looks like it's 1200ish millis
need to find a way to make it faster.

Do not open and close the file for each write. Leave the file open, and let the library manage the physical writes when the buffer is full.

unless i am missing something i did use unsigned long currentMillis and unsigned long previousMillis

A faster and better way would be to get rid of all those String variables and use char arrays (e.g. strings - lowercase s) instead. You also do not need to keep creating them every time through loop. You are doing a ton of work to get to the same answer as the last time through the loop.

You can also switch to the TimeLib library which allows you to set the interval for how often you actually read the RTC. I2C communication is slow so the library can read the RTC every hour or so and compute the time based on the millis() and the last reading of the RTC which is much, much faster.

I guess i hooked on to this line and that ought to be safe. Sorry.

No idea what I changed but it will works as intended.

Basically I started all over from scratch again by combining all of my "smaller" code segments that i knew worked.

#include <SD.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS1307 rtc;
const int chipSelect = 10;
LiquidCrystal_I2C lcd(0x27,4,20);
unsigned long previousMillis = 0;
const long interval = 500;
String previouspath = "";
#define LED1 7
#define LED2 6

void setup()
{
 pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
 pinMode(10, OUTPUT);
 digitalWrite(10, HIGH);
 lcd.init(); 
 lcd.backlight();
 Serial.begin(9600);
 Serial.println("Prepare SD card");
 lcd.setCursor(0, 0);
 lcd.print("Prepare SD card");
 delay(500);
 if (!SD.begin(chipSelect)) {
 Serial.println("Card Failed");
 digitalWrite(LED2, HIGH);
 lcd.setCursor(0,1);
 lcd.print("Card Failed");
 while (1);
 }
 Serial.println("Card Ready");
 lcd.setCursor(0,1);
 lcd.print("Card Ready");
 delay(500);
 lcd.clear();
 delay(500);
 Wire.begin();
if (!rtc.begin()) {
    Serial.println("RTC failed");
    digitalWrite(LED2, HIGH);
    lcd.setCursor(0,2);
    lcd.print("RTC failed");
 }
}

void loop() 
{

  DateTime now = rtc.now();
  
  unsigned long currentMillis = millis();
  
  String path = "";
  String sensors = "";
  String dataString = "";
  String filename = "";
  String foldername = "";
  String date = "";
  String header = "";
  


  
  if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
  lcd.clear();
int V1 = analogRead(A0);
int C1 = analogRead(A1);

float Volt1 = V1 / (40.96);
float Amp1 = ((C1-511)/17.06); 
float Watt1 = Volt1 * Amp1;

dataString += String();
dataString += String(Volt1);
dataString += ",";
dataString += String(Amp1);
dataString += ",";
dataString += String(Watt1);
dataString += ",";
dataString += String(now.month(), DEC);
dataString += "/";
dataString += String(now.day(), DEC);
dataString += "/";
dataString += String(now.year(), DEC);
dataString += ",";
dataString += String(now.hour(), DEC);
dataString += ":";
dataString += String(now.minute(), DEC);
dataString += ":";
dataString += String(now.second(), DEC);

filename += String(now.hour(), DEC);
filename += ".csv";
  
foldername += String(now.year(), DEC);
foldername += String(now.month(), DEC);
foldername += String(now.day(), DEC);
  
path += String(foldername);
path += "/";
path += String(filename);

date += String(now.month(), DEC);
date += "/";
date += String(now.day(), DEC);
date += "/";
date += String(now.year(), DEC);
date += "  ";
date += String(now.hour(), DEC);
date += ":";
date += String(now.minute(), DEC);
date += ":";
date += String(now.second(), DEC);

header += "Volt,Amp,Watt,Date,Time";
digitalWrite(LED1, HIGH);
    SD.mkdir(foldername);  
  
  File dataFile = SD.open(path, FILE_WRITE);

  if (path != previouspath){
    dataFile.println(header);
    previouspath = String(path);
   }
    else { 
       }
  
  if (dataFile) {
  
  dataFile.println(dataString);
  dataFile.close();
   digitalWrite(LED1, LOW);


  //Serial.println(sensors);
  Serial.println(dataString);
  
  lcd.home();
  lcd.print(" ROBOT POWER LOGGER");
  lcd.setCursor(4,1);
  lcd.print("BY ID MILLER");
  lcd.setCursor(0,2);
  lcd.print(date);
  lcd.setCursor(0,3);
  lcd.print("PATH:");
  lcd.setCursor(5,3);
  lcd.print(path);
  
  }
  else {
  Serial.println("Error Opening File");
  lcd.setCursor(0,0);
  lcd.print("Error Opening File");
  }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.