Hello Arduino Community,
I am trying to log the date, time and the analog value from analog pin 3 on the Arduino Uno R3. I am able to make all this information displayed on the GLCD display. Except it will not log the information on to the SD card.
Also, if I upload an example of an SD card logger, the SD card will work.
So here is my code and if you have any suggestions or if you are able to point me in the right direction, much appreciated.
#include <SD.h>
const int chipSelect = 4;
#include <SoftwareSerial.h>
#include "LCD12864RSPI.h"
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
#include "U8glib.h"
U8GLIB_ST7920_128X64_1X u8g(5, 8, 9);
#define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
int sensorValue = analogRead(A3);
int Key;
void draw(void) {
DateTime now = RTC.now();
u8g.setFont(u8g_font_tpss);
u8g.setPrintPos(0, 9);
String MONTH[] = {"Jan","Feb","Mar","April","May","June","July","Aug","Sept","Oct","Nov","Dec" };
int i = now.month() - 1;
u8g.print(MONTH[i]);
u8g.print(" ");
u8g.print(now.day(),DEC);
u8g.print(",");
u8g.print(now.year(),DEC);
u8g.setPrintPos(87, 9);
u8g.print(now.hour(),DEC);
u8g.print(":");
if(now.minute() <= 9)
{
u8g.print("0");
u8g.print(now.minute(),DEC);
}
else{
u8g.print(now.minute(),DEC);
}
u8g.print(":");
if (now.second() <= 9)
{
u8g.print("0");
u8g.print(now.second(),DEC);
}
else {
u8g.print(now.second(),DEC);
}
u8g.setPrintPos(0, 50);
delay(29);
}
void setup()
{
Serial.begin(9600);
int analogPin = 3;
int val = 0;
Wire.begin();
RTC.begin();
RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop()
{
DateTime now = RTC.now();
int val = analogRead(A3);
Serial.println(val);
String dataString = "";
dataString += String("Date: ");
dataString += String(now.hour());
dataString += String(":");
dataString += String(now.minute());
dataString += String(":");
dataString += String(now.second());
dataString += String(" Sensor Value:");
dataString += String(val);
delay(400);
Serial.println(dataString);
File dataFile = SD.open("LOGGER.txt", FILE_WRITE);
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error opening datalog.txt");
}
delay(300);
u8g.firstPage();
do {
draw();
u8g.print("Sensor Reading: ");
u8g.print(val);
} while( u8g.nextPage() );
delay(20);
}
If I posted this in the wrong section, please let me know.
Thank you,
Chris
I am also sorry for the terrible grammar.