im using a LCD , sdn= card module and a rtc ds1307, RTC is interfaced through A4,A5(i2c) to arduino uno, memory card through the ICSP pins, im enclosing the code please help me.
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <SPI.h>
#include<SD.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
File file;
int CS_PIN=7;
char p;
const byte ROWS = 3; // 3 rows
const byte COLS = 3; // 3 columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},{'I','8','E'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {A3, 9, 8};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {A0,A1,A2};
// Create the Keypad
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(12,13,5,4,3,2);
int text,clas=0;
void setup()
{
Serial.begin(9600);
delay(1000);
initializeSD();
delay(1000);
Wire.begin();
rtc.begin();
if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(2015,6,29,13,10,0));
}
// following line sets the RTC to the date & time this sketch was compiled
delay(1000);
createFile("stack.csv");
file.close();
lcd.begin(16,2);
delay(1000); // delay 1s to stabilize serial ports
}
void loop()
{
DateTime now = rtc.now();
delay(1000);
Serial.println(file);
char key=kpd.getKey();
if(key)
{
switch(key)
{
case '1':
{
file = SD.open("stack.csv", FILE_WRITE);
Serial.println(file);
// if the file is available, write to it:
if (file)
{
delay(2000);
file.print("Mr.G.Srinivas Raju," );
file.print("W C N,");
Serial.println("G.Srinivas Raju ");
file.close();
delay(3000);
lcd.clear();
lcd.setCursor(0, 0); // top left
lcd.write("Mr.G.Srinivas Raju");
lcd.setCursor(0, 1); // bottom left
lcd.write("W C N");
delay(3000);
}
break;
}
case '2':
{
file = SD.open("stack.csv", FILE_WRITE);
Serial.println(file);
// if the file is available, write to it:
if (file) {
delay(2000);
file.print("Mr.P.Praveen Kumar,");
file.print(" C M C,");
Serial.print("Mr.P.Praveen Kumar");
file.close();
lcd.clear();
lcd.setCursor(0, 0); // top left
lcd.write("Mr.P.Praveen Kumar");
lcd.setCursor(0, 1); // bottom left
lcd.write("C M C");
delay(3000);
}
break;
}
case '3':
{
file = SD.open("stack.csv", FILE_WRITE);
Serial.println(file);
// if the file is available, write to it:
if (file)
{
delay(2000);
file.print("Mr.M.Shiva Kumar,");
file.print("Internetworking,");
// print to the serial port too:
Serial.println("M.Shiva Kumar ");
file.close();
lcd.clear();
lcd.setCursor(0, 0); // top left
lcd.write("Mr.M.Shiva Kumar");
lcd.setCursor(0, 1); // bottom left
lcd.write("INT.NET");
delay(3000);
}
break;
}
case '4':
{
file = SD.open("stack.csv", FILE_WRITE);
Serial.println(file);
// if the file is available, write to it:
if (file)
{
delay(2000);
file.print("Substitute,");
// print to the serial port too:
Serial.println("Substitute ");
file.close();
lcd.clear();
lcd.setCursor(0, 0); // top left
lcd.write("SUBSTITUTE");
delay(3000);
}
break;
}
case 'I':
{
Serial.println("I selected");
lcd.clear();
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("PLEASE ENTER");
lcd.setCursor(0, 1); // bottom left
lcd.write("YOUR ID...");
file = SD.open("stack.csv", FILE_WRITE);
Serial.println(file);
// if the file is available, write to it:
if (file)
{
delay(2000);
file.print(now.year(), DEC);
file.print('/');
file.print(now.month(), DEC);
file.print('/');
file.print(now.day(), DEC);
file.print(",");
file.print(now.hour(), DEC);
file.print(':');
file.print(now.minute(), DEC);
file.print(':');
file.print(now.second(), DEC);
file.print(",");
file.close();
}break;
}
case 'E':
{
lcd.clear();
lcd.write("SESSION COMPLETE");
delay(3000);
lcd.clear();
SD.begin();
file = SD.open("stack.csv", FILE_WRITE);
Serial.println(file);
// if the file is available, write to it:
if (file) {
delay(2000);
file.print(now.hour(), DEC);
file.print(':');
file.print(now.minute(), DEC);
file.print(':');
file.print(now.second(), DEC);
file.print("\n");
file.close();
}
break;
}
default: lcd.write("wrong Key");
}
}}
void initializeSD()
{
Serial.println("Initializing SD card...");
pinMode(CS_PIN, OUTPUT);
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
}
int createFile(char filename[])
{
file = SD.open(filename, FILE_WRITE);
if (file)
{
Serial.println("File created successfully.");
return 1;
} else
{
Serial.println("Error while creating file.");
return 0;
}
}