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);
}