Is this programming the right approach?

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.

cant see a SD.begin(); anywhere....

Look at the SD card datalogger example in the ide which I reproduce below:

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  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)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

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 < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      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);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

Thank you both for your help. The program is now working, after I placed the SD.begin function in the setup, and here is the working script.

In the future I am hoping to have the circuit only log every 5 minutes.

Cheers,

Chris

#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);
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);
  
}
   
    
  
  delay(29);  
}


void setup()
{
 
 
  Serial.begin(9600);
  Wire.begin();
   Serial.print("Initializing SD card...");

  pinMode(2, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");

  }
  Serial.println("card initialized.");
   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(":");
  if(now.minute() > 9 )
    {
      dataString += String("0");  
      dataString += String(now.minute());
    }
  else
    {
      dataString += String(now.minute());
    }
  
  dataString += String(":");
  if(now.second() > 10 )
    {
      dataString += String("0");  
      dataString += String(now.second());
    }
  else
    {
      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 text file");
  }
    
  u8g.firstPage();  
  do {
    draw();

     
   u8g.setPrintPos(0, 50);
   u8g.print("Sensor Reading: ");
   u8g.print(val);
  } while( u8g.nextPage() );
  
  
 
  delay(20);
}