Hello,
I was hoping someone could help me with my problem. I am trying to connect an LCD display to my Arduino Duemillenova and Adafruit datalogger shield that is reading two themocouples via MAX 31855 chips.
My code seems to work as expected and displays correctly on the LCD until the code checks to see if the card is present using the following code, after that it displays gibberish.
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
error("Card failed, or not present");
}
Below is my entire code. Any help would be greatly appreciated! Thanks!!
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
// LCD setup
//initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 13, 11, 12);
//TC code
#include "Adafruit_MAX31855.h"
int thermoDO = 3;
int thermoCS1 = 4;
int thermoCLK = 5;
int thermoCS2 = 6;
Adafruit_MAX31855 thermocouple1(thermoCLK, thermoCS1, thermoDO);
Adafruit_MAX31855 thermocouple2(thermoCLK, thermoCS2, thermoDO);
// A simple data logger for the Arduino analog pins
//********** how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
//***LOGGING FREQUENCY, 60000 = 1 minute; 1000 = 1 second
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
// the digital pins that connect to the LEDs
//#define redLEDpin 2
//#define greenLEDpin 3
// The analog pins that connect to the sensors
#define photocellPin 0 // analog 0
#define tempPin 1 // analog 1
#define BANDGAPREF 14 // special indicator that we want to measure the bandgap
#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
#define bandgap_voltage 1.1 // this is not super guaranteed but its not -too- off
RTC_DS1307 RTC; // define the Real Time Clock object
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
// digitalWrite(redLEDpin, HIGH);
while(1);
}
void setup(void)
{
Serial.begin(9600);
Serial.println();
// use debugging LEDs
// pinMode(redLEDpin, OUTPUT);
// pinMode(greenLEDpin, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("TC1 =");
lcd.setCursor(0,1);
lcd.print("TC2 =");
delay(500);
#if WAIT_TO_START
Serial.println("Type any character to start");
while (!Serial.available());
#endif //WAIT_TO_START
lcd.print("test");
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
error("Card failed, or not present");
}
lcd.print("test2");
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("date,time,temp1,temp2");
#if ECHO_TO_SERIAL
Serial.println("date,time,temp1,temp2");
#endif //ECHO_TO_SERIAL
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
}
void loop(void)
{
DateTime now;
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
//digitalWrite(greenLEDpin, HIGH);
// fetch the time
now = RTC.now();
// log time
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(",");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
#if ECHO_TO_SERIAL
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(",");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
#endif //ECHO_TO_SERIAL
//TC1 code
double c1 = thermocouple1.readCelsius();
if (isnan(c1)) {
logfile.print(",");
logfile.println("Something wrong with thermocouple!");
Serial.print(",");
Serial.println("Something wrong with thermocouple!");
} else {
logfile.print(",");
Serial.print(",");
logfile.print(c1);
Serial.print(c1);
}
//TC2 code
double c2 = thermocouple2.readCelsius();
if (isnan(c2)) {
logfile.print(",");
logfile.print("Something wrong with thermocouple!");
Serial.print(",");
Serial.print("Something wrong with thermocouple!");
} else {
logfile.print(",");
logfile.print(c2);
Serial.print(",");
Serial.print(c2);
}
lcd.setCursor(8,0);
if (isnan(c1)) {
lcd.print("T/C Problem");
} else {
lcd.print(c1);
lcd.print(" ");
}
lcd.setCursor(8,1);
if (isnan(c2)) {
lcd.print("T/C Problem");
} else {
lcd.print(c2);
lcd.print(" ");
}
//create a new line for the next set of data
logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif // ECHO_TO_SERIAL
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
// blink LED to show we are syncing data to the card & updating FAT!
// digitalWrite(redLEDpin, HIGH);
logfile.flush();
//digitalWrite(redLEDpin, LOW);
}