Project includes
(2) 30amp current sensors
(2) voltage sensors
(1) Light detector sensor
(1) SD card
(1) 20x4 LCD
Arduino Mega
Arduino Mega Sensor shield
I took the DataLogger example and tweaked it to what i am looking for. it's not perfect and that might be part of my problem but the bigger part of the problem is knowing where and how to insert the info into the datastring is my biggest problem.
The LCD portion is very rough. when i get 4 digit values and they return to lower values i have ghost characters that do not clear out but that is a different issue all together.
Below is the modified code.
Please note this works perfectly i just want to add a date and time to the end when the reading was taken.
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int chipSelect = 10;
LiquidCrystal_I2C lcd(0x27,20,4);
void setup()
{
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
pinMode(53, OUTPUT);
lcd.init();
lcd.backlight();
Serial.begin(9600);
Serial.print("Initializing SD card...");
lcd.setCursor(0, 0);
lcd.print("Prepare SD card");
delay(2000);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
lcd.setCursor(0,1);
lcd.print("Card Failed");
while (1);
}
Serial.println("card initialized.");
lcd.setCursor(0,1);
lcd.print("Card Ready");
delay(3000);
lcd.init();
delay(2000);
}
void loop() {
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 5; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 4) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
lcd.setCursor(0,0);
lcd.print(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
lcd.setCursor(0,0);
lcd.print("Error datalog.txt");
}
delay(500);
}
I am trying to incorporate RTClib DS3231 into the code but i do not think that is what i need to do. that code is.
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(9600);
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
Will I be able to merge these 2 codes and get them to do what I want or do I need to be looking else where?