Hi, so I've built this little device that includes an LCD screen a light sensor and a laser with a data logging shield and SD card on top of my arduino. I want the voltages to read out on my LCD screen and it was working until I tried to put code in for the SD card. Now its reading out random letters and numbers in no apparent order.
Here's my code:
#include <LiquidCrystal.h>
#include "SD.h"
#include <Wire.h>
#include "RTClib.h"
#define LOG_INTERVAL 1000 // mills between entries
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int sensorPin = A0;
int sensorValue;
float Res = 10.0;
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);
while(1);
}
void setup(){
lcd.begin(16,2);
lcd.clear();
Serial.begin(9600);
Serial.println();
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
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);
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("millis,time,light");
ECHO_TO_SERIAL;
Serial.println("millis,time,light");
ECHO_TO_SERIAL; // attempt to write out the header to the file
}
void loop(){
{ sensorValue = analogRead(sensorPin);
float Vout = sensorValue*0.0048828125;
delay(200);
Serial.print("Voltages");
Serial.print(Vout);
lcd.print("V\t");
lcd.print(sensorValue);
delay(200);{
if(Serial.available()){
delay(100);
lcd.clear();}
DateTime now;
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
// log milliseconds since starting
uint32_t m = millis();
logfile.print(m); // milliseconds since start
logfile.print(", ");
#if ECHO_TO_SERIAL
Serial.print(m); // milliseconds since start
Serial.print(", ");
#endif
// fetch the time
now = RTC.now();
// log time
logfile.print(", ");
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(", ");
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
logfile.print(", ");
logfile.print(Vout);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(Vout);
#endif //ECHO_TO_SERIAL
}
}
}
Any help or advice much appreciated