Hi, I am relatively new to arduino and only ever asked 4 questions on the arduino forum. I am moving to this forum hoping it can be more helpful and less bullyish. here goes…
I am using the sdFat library.
I have coded a datalogger using it. I am using a LCD as I wish to use it independently of a computer and do not wish to use the serial port . Can anyone help me on how to get the sd card error codes that are generated within the sdFat library that are sent to serial to print through lcd.print instead. I usually find an answer eventually but this one eludes me. I have so found that the sdFat.h file has text I am trying to display on the lcd.
How do I change this class to print to the LCD??
void initErrorPrint(Print* pr) {
if (cardErrorCode()) {
pr->println(F("Can't access SD card. Do not reformat.")); /// THIS LINE HERE!!!
if (cardErrorCode() == SD_CARD_ERROR_CMD0) {
pr->println(F("No card, wrong chip select pin, or SPI problem?"));
}
errorPrint(pr);
} else if (vol()->fatType() == 0) {
pr->println(F("Invalid format, reformat SD."));
} else if (!vwd()->isOpen()) {
pr->println(F("Can't open root directory."));
} else {
pr->println(F("No error found."));
}
}
I’ve looked through the libraries and on google but can’t find any clues. I hope I’ve given enough info.
thanks for any help…
#include <SPI.h>
#include "SdFat.h"
#include <DS1302RTC.h>
#include <TimeLib.h>
#include <Wire.h>
#include <Adafruit_AM2315.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <LiquidCrystal_I2C.h>
// SD chip select pin. Be sure to disable any other SPI devices such as Enet.
const uint8_t chipSelect = SS;
const uint32_t SAMPLE_INTERVAL_MS = 1000;// sample time
// Log file base name. Must be six characters or less.
#define FILE_BASE_NAME "Data"
//------------------------------------------------------------------------------
// define the Real Time Clock object
DS1302RTC RTC(5, 6, 7);// Set pins: CE, IO,CLK
// define AM2315 temperature humidity sensor object
Adafruit_AM2315 am2315;
//define TSL2561 object
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
LiquidCrystal_I2C lcd(0x3F, 20, 4);
// File system object.
SdFat sd;
// Log file.
SdFile file;
// Optional connection for RTC module
#define DS1302_GND_PIN 33
#define DS1302_VCC_PIN 35
int period = 1000;//millis timer
unsigned long time_now = 0;
// Time in micros for next data record.
uint32_t logTime;
//==============================================================================
// User functions. Edit writeHeader() and logData() for your requirements.
//double TIME_1 = 0;// interrupts
//double INTERVAL_1 = 100;
const uint8_t CHANNEL_COUNT = 4;
float dataAM2315[2];
boolean AM2315_OK;
int dataLux;
float data[CHANNEL_COUNT]={88.88,88.88,88.88,88.88};
String label[4]={"C :","RH :","LUX:","CO2:"};
//------------------------------------------------------------------------------
// Error messages stored in flash.
#define error(msg) sd.errorHalt(F(msg))
//------------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
lcd.begin();
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[13] = FILE_BASE_NAME "00.csv";
// Wait for USB Serial
while (!Serial) {
SysCall::yield();
}
delay(500);
if(!tsl.begin()||!am2315.begin())
{
lcd.clear();
lcd.setCursor (0,1);
lcd.print("Sensors Unplugged");
while(1);
}
configureSensor();
/* if (! am2315.begin()) {
lcd.clear();
lcd.setCursor (0,1);
lcd.print("AM2315 Failed");
while (1);
}
*/
if (RTC.haltRTC()) {
lcd.clear();
lcd.setCursor (0,0);
lcd.print("RTC Failed");
while(1);
}
if (!RTC.writeEN()) {
lcd.clear();
lcd.setCursor (0,0);
lcd.print("RTC Found");
delay(1000);
}
lcd.clear();
//
lcd .print(F("Sample Interval:"));
lcd.setCursor (0,1);
lcd.print(SAMPLE_INTERVAL_MS);
lcd.print(F(" ms"));
delay(1500);
/*while (!Serial.available()) {
SysCall::yield();
}*/
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(chipSelect, SD_SCK_MHZ(40))) {
sd.initErrorHalt();
}
// Find an unused file name.
if (BASE_NAME_SIZE > 6) {
error("FILE_BASE_NAME too long");
}
while (sd.exists(fileName)) {
if (fileName[BASE_NAME_SIZE + 1] != '9') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
} else {
error("Can't create file name");
}
}
if (!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) {
error("file.open");
}
// Read any Serial data.
do {
delay(10);
} while (Serial.available() && Serial.read() >= 0);
lcd.clear();
lcd.setCursor (0,1);
lcd.print(("Log File:"));
lcd.print(fileName);
writeHeader(); // Write data header.
logTime = micros()/(1000UL*SAMPLE_INTERVAL_MS) + 1; // Start on a multiple of the sample interval.
logTime *= 1000UL*SAMPLE_INTERVAL_MS;
}
//------------------------------------------------------------------------------
void loop() {
sensors_event_t event;
tsl.getEvent(&event);
if(millis() > time_now + period){
displayData();
time_now = millis();
AM2315_OK=am2315.readTemperatureAndHumidity(dataAM2315[0],dataAM2315[1]);
/* Display the results (light is measured in lux) */
if (event.light)
{
dataLux=event.light;
}
}
// Time for next record.
logTime += 1000UL*SAMPLE_INTERVAL_MS;
// Wait for log time.
int32_t diff;
do {
diff = micros() - logTime;
} while (diff < 0);
// Check for data rate too high.
if (diff > 10) {
error("Missed data record");
}
logData();
// Force data to SD and update the directory entry to avoid data loss.
if (!file.sync() || file.getWriteError()) {
error("write error");
}
if (Serial.available()) {
//digitalWrite(greenLEDpin, LOW);
// Close file and stop.
file.close();
lcd.setCursor(0,1);
lcd.print(("*** File Closed ***"));
SysCall::halt();
}
}
// Write data header.
void writeHeader() {
file.print(F("UNIX"));
file.print(F(",DateTime"));
file.print(F(",micros"));
file.print(F(",Temp"));
file.print(F(",Hum"));
file.print(F(",Lux"));
file.print(F(",Co2"));
//for (uint8_t i = 0; i < CHANNEL_COUNT; i++) {
//file.print(F(",adc"));
//file.print(i, DEC);
//}
file.println();
}
//------------------------------------------------------------------------------
// Log a data record.
void logData() {
// Read all channels to avoid SD write latency between readings.
for (uint8_t i = 0; i < 2; i++) {
data[i] = dataAM2315[i];
data[2]= dataLux;
data[3]= 10;
}
// Write data to file. Start with log time in micros.
displayTime();
tStamp();//logging time stamp
file.write (",");
file.print(logTime); //use for accurate log timing
// Write ADC data to CSV record.
for (uint8_t i = 0; i < CHANNEL_COUNT; i++) {
file.write(',');
file.print(data[i]);
}
file.println();
//
}
//==============================================================================
void configureSensor(void)
{
tsl.enableAutoRange(true); //auto range
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); //fast resoloution
}
void tStamp() { // prints Date time to file
tmElements_t tm;
RTC.read(tm);
// log time
file.print(RTC.get()); // seconds since 1/1/1970
file.print(",");
file.print('"');
file.print((tmYearToCalendar(tm.Year)), DEC);
file.print("/");
file.print((tm.Month),DEC);
file.print("/");
file.print((tm.Day), DEC);
file.print(" ");
logFile2digits(tm.Hour);
file.print(":");
logFile2digits(tm.Minute);
file.print(":");
logFile2digits(tm.Second);
file.print('"');
}
void displayTime() { ////prints date time to LCD
tmElements_t tm;
RTC.read(tm);
// log time
lcd.setCursor(0,0);
lcd.print((tmYearToCalendar(tm.Year)), DEC);
lcd.print("/");
print2digits(tm.Month);
lcd.print("/");
print2digits(tm.Day);
lcd.print(" ");
print2digits(tm.Hour);
lcd.print(":");
print2digits(tm.Minute);
lcd.print(":");
print2digits(tm.Second);
}
void logFile2digits(int number) {
if (number >= 0 && number < 10)
file.write('0');
file.print(number);
}
void print2digits(int number) {
if (number >= 0 && number < 10)
lcd.print('0');
lcd.print(number);
}
void displayData() {
for (uint8_t i = 0; i < 2; i++) { //first two columns of data
lcd.setCursor(0,i+2);
lcd.print(label[i]);
lcd.setCursor(1,2);
lcd.print((char)223);
lcd.setCursor(4,i+2);
lcd.print(data[i]);
}
for (uint8_t i = 2; i < 4; i++) {//last two columns of data
lcd.setCursor(11,i);
lcd.print(label[i]);
lcd.setCursor(15,i);
lcd.print(data[i]);
}
}