I am having issues with a code that I have been modifying. I can change the TIME, and DATE from a keypad, and see the data on the serial monitor, but it is not recording on the SD Card. Anyone that could help me. Thank you.
#include <OneWire.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include "RTClib.h"
LiquidCrystal_I2C lcd(0x27,20,4)
RTC_DS3231 rtc;
DateTime now;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPINS[ROWS] = {10,9,8,7};
byte colPINS[COLS] = {6,5,4,11};
int i1,i2,i3,i4;
char c1,c2,c3,c4;
char keypressed;
Keypad myKeypad = Keypad(makeKeymap(keys),rowPINS,colPINS, ROWS, COLS);
#define DS18B20_PIN A3
File dataLog;
boolean sd_ok = 0;
void setup()
{
rtc.begin(); // initialize RTC chip
lcd.init(); // initialize LCD module
lcd.backlight();
lcd.setCursor(0, 3); // move cursor to column 0, row 3 (last row)
lcd.print("Temp:");
// open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Initializing SD card...");
// initialize the SD card
if ( !SD.begin() )
Serial.println(F("initialization failed!")); // initialization error
else { // initialization OK
sd_ok = 1;
Serial.println(F("initialization done."));
if( SD.exists("Log.txt") == 0 ) // test if file with name 'Log.txt' already exists
{ // create a text file named 'Log.txt'
Serial.print("\r\nCreate 'Log.txt' file ... ");
dataLog = SD.open("Log.txt", FILE_WRITE); // create (&open) file Log.txt
if(dataLog) { // if the file opened okay, write to it:
Serial.println(F("OK"));
// write some texts to 'Log.txt' file
dataLog.println(" DATE | TIME | TEMPERATURE");
dataLog.println("(dd-mm-yyyy)|(hh:mm:ss)|");
dataLog.close(); // close the file
}
else
Serial.println(F("error creating file."));
}
}
Serial.println(F("\r\n DATE | TIME | TEMPERATURE"));
Serial.println(F("(dd-mm-yyyy)|(hh:mm:ss)|"));
}
// main loop
void loop()
{
now = rtc.now(); // read current time and date from the RTC chip
RTC_display(); // display time & calendar
while(keypressed == NO_KEY){
keypressed = myKeypad.getKey();
now = rtc.now(); // read current time and date from the RTC chip
RTC_display(); // display time & calendar
static byte p_second;
if( (now.second()-1==0) || (now.second() % 5 == 0) && (p_second != now.second()) )
{ // read & print temperature value from sensor every 10 seconds
unsigned int ds18b20_temp;
char buffer1[12], buffer2[26];
bool sensor_ok = 0;
p_second = now.second();
if( ds18b20_read(&ds18b20_temp) )
{
sensor_ok = 1;
if (ds18b20_temp & 0x8000) // if temperature < 0
{
ds18b20_temp = ~ds18b20_temp + 1; // change temperature value to positive form
sprintf(buffer1, "-%02u.%04u%cC", (ds18b20_temp/16) % 100, (ds18b20_temp & 0x0F) * 625, 223);
}
else
{ // otherwise (temperature >= 0)
if (ds18b20_temp/16 >= 100) // if temperature >= 100.0 °C
sprintf(buffer1, "%03u.%04u%cC", ds18b20_temp/16, (ds18b20_temp & 0x0F) * 625, 223);
else // otherwise ( 0 <= temperature < 100.0)
sprintf(buffer1, " %02u.%04u%cC", ds18b20_temp/16, (ds18b20_temp & 0x0F) * 625, 223);
}
}
else
sprintf(buffer1, " ERROR ");
lcd.setCursor(5, 3);
lcd.print(buffer1);
sprintf( buffer2, " %02u-%02u-%04u | %02u:%02u:%02u | ", now.day(), now.month(), now.year(),
now.hour(), now.minute(), now.second() );
if(sensor_ok) {
buffer1[8] = 194; // put degree symbol
buffer1[9] = 176;
buffer1[10] = 'C'; // put 'C' letter
buffer1[11] = '0円'; // put string terminator
}
// print data on PC serial monitor
Serial.print(buffer2);
Serial.println(buffer1);
// write data to SD card
if(sd_ok)
{ // if the SD card was successfully initialized
// open Log.txt file with write permission
dataLog = SD.open("Log.txt", FILE_WRITE);
dataLog.print( buffer2 );
dataLog.println( buffer1 );
dataLog.close(); // close the file
}
}
delay(100); // wait 100ms
}
if (keypressed == '*')
{
lcd.clear();
lcd.print(" Setup");
delay(1000);
lcd.clear();
lcd.print("Setup year");
char keypressed2 = myKeypad.waitForKey();
if (keypressed2 != NO_KEY && keypressed2 !='*' && keypressed2 !='#' && keypressed2 !='A' && keypressed2 !='B' && keypressed2 !='C' && keypressed2 !='D' )
{
c1 = keypressed2;
lcd.setCursor(0, 1);
lcd.print(c1);
}
char keypressed3 = myKeypad.waitForKey();
if (keypressed3 != NO_KEY && keypressed3 !='*' && keypressed3 !='#' && keypressed3 !='A' && keypressed3 !='B' && keypressed3 !='C' && keypressed3 !='D' )
{
c2 = keypressed3;
lcd.setCursor(1, 1);
lcd.print(c2);
}
char keypressed4 = myKeypad.waitForKey();
if (keypressed4 != NO_KEY && keypressed4 !='*' && keypressed4 !='#' && keypressed4 !='A' && keypressed4 !='B' && keypressed4 !='C' && keypressed4 !='D' )
{
c3 = keypressed4;
lcd.setCursor(2, 1);
lcd.print(c3);
}
char keypressed5 = myKeypad.waitForKey();
if (keypressed5 != NO_KEY && keypressed5 !='*' && keypressed5 !='#' && keypressed5 !='A' && keypressed5 !='B' && keypressed5 !='C' && keypressed5 !='D' )
{
c4 = keypressed5;
lcd.setCursor(3, 1);
lcd.print(c4);
}
i1=(c1-48)*1000; //the keys pressed are stored into chars I convert them to int then i did some multiplication to get the code as an int of xxxx
i2=(c2-48)*100;
i3=(c3-48)*10;
i4=c4-48;
int N_year=i1+i2+i3+i4;
delay(500);
lcd.clear();
lcd.print("Setup month");
////////////////////////////////////////////////////////////////
char keypressed6 = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above
if (keypressed6 != NO_KEY && keypressed6 !='*' && keypressed6 !='#' && keypressed6 !='A' && keypressed6 !='B' && keypressed6 !='C' && keypressed6 !='D' )
{
c1 = keypressed6;
lcd.setCursor(0, 1);
lcd.print(c1);
}
char keypressed7 = myKeypad.waitForKey();
if (keypressed7 != NO_KEY && keypressed7 !='*' && keypressed7 !='#' && keypressed7 !='A' && keypressed7 !='B' && keypressed7 !='C' && keypressed7 !='D' )
{
c2 = keypressed7;
lcd.setCursor(1, 1);
lcd.print(c2);
}
i1=(c1-48)*10;
i2=c2-48;
int N_month=i1+i2;
delay(500);
lcd.clear();
lcd.print("Setup Day");
////////////////////////////////////////////////////////////////
char keypressed8 = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above
if (keypressed8 != NO_KEY && keypressed8 !='*' && keypressed8 !='#' && keypressed8 !='A' && keypressed8 !='B' && keypressed8 !='C' && keypressed8 !='D' )
{
c1 = keypressed8;
lcd.setCursor(0, 1);
lcd.print(c1);
}
char keypressed9 = myKeypad.waitForKey();
if (keypressed9 != NO_KEY && keypressed9 !='*' && keypressed9 !='#' && keypressed9 !='A' && keypressed9 !='B' && keypressed9 !='C' && keypressed9 !='D' )
{
c2 = keypressed9;
lcd.setCursor(1, 1);
lcd.print(c2);
}
i1=(c1-48)*10;
i2=c2-48;
int N_day=i1+i2;
delay(500);
lcd.clear();
lcd.print("Setup hour");
////////////////////////////////////////////////////////////////////////////////////:
char keypressed10 = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above
if (keypressed10 != NO_KEY && keypressed10 !='*' && keypressed10 !='#' && keypressed10 !='A' && keypressed10 !='B' && keypressed10 !='C' && keypressed10 !='D' )
{
c1 = keypressed10;
lcd.setCursor(0, 1);
lcd.print(c1);
}
char keypressed11 = myKeypad.waitForKey();
if (keypressed11 != NO_KEY && keypressed11 !='*' && keypressed11 !='#' && keypressed11 !='A' && keypressed11 !='B' && keypressed11 !='C' && keypressed11 !='D' )
{
c2 = keypressed11;
lcd.setCursor(1, 1);
lcd.print(c2);
}
i1=(c1-48)*10;
i2=c2-48;
int N_hour=i1+i2;
delay(500);
lcd.clear();
lcd.print("Setup minutes");
////////////////////////////////////////////////////////////////////////////////////:
char keypressed12 = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above
if (keypressed12 != NO_KEY && keypressed12 !='*' && keypressed12 !='#' && keypressed12 !='A' && keypressed12 !='B' && keypressed12 !='C' && keypressed12 !='D' )
{
c1 = keypressed12;
lcd.setCursor(0, 1);
lcd.print(c1);
}
char keypressed13 = myKeypad.waitForKey();
if (keypressed13 != NO_KEY && keypressed13 !='*' && keypressed13 !='#' && keypressed13 !='A' && keypressed13 !='B' && keypressed13 !='C' && keypressed13 !='D' )
{
c2 = keypressed13;
lcd.setCursor(1, 1);
lcd.print(c2);
}
i1=(c1-48)*10;
i2=c2-48;
int N_minutes=i1+i2;
delay(500);
lcd.clear();
rtc.adjust(DateTime(N_year, N_month, N_day, N_hour, N_minutes, 0));
RTC_display();
lcd.setCursor(0, 3); // move cursor to column 0, row 3 (last row)
lcd.print("Temp:");
//myRTC.setDS1302Time(22, N_minutes, N_hour, 1, N_day, N_month, N_year); //once we're done setting the date and time we transfer to values to the RTC module
//the 22 stands for seconds you can add a setup for it too if you want
//the 1 stands for day of the week, as long I don't show it on the screen I don't change it
keypressed=NO_KEY; //the "*" key is stored in "keypressed" so I remove that value from it otherwise it will get me in the setup again
}
}
//////////////////////////////////////// RTC functions ////////////////////////////////////////
void RTC_display()
{
char _buffer[17];
char dow_matrix[7][10] = {" SUNDAY ", " MONDAY ", " TUESDAY ", "WEDNESDAY",
"THURSDAY ", " FRIDAY ", "SATURDAY "};
lcd.setCursor(4, 0);
lcd.print( dow_matrix[now.dayOfTheWeek()] );
// print time
sprintf( _buffer, "TIME: %02u:%02u:%02u", now.hour(), now.minute(), now.second() );
lcd.setCursor(0, 1);
lcd.print(_buffer);
// print date
sprintf( _buffer, "DATE: %02u-%02u-%04u", now.day(), now.month(), now.year() );
lcd.setCursor(0, 2);
lcd.print(_buffer);
}
////////////////////////////////////// end RTC functions //////////////////////////////////////
////////////////////////////////// DS18B20 sensor functions ///////////////////////////////////
bool ds18b20_start()
{
bool ret = 0;
digitalWrite(DS18B20_PIN, LOW); // send reset pulse to the DS18B20 sensor
pinMode(DS18B20_PIN, OUTPUT);
delayMicroseconds(500); // wait 500 us
pinMode(DS18B20_PIN, INPUT);
delayMicroseconds(100); // wait to read the DS18B20 sensor response
if (!digitalRead(DS18B20_PIN))
{
ret = 1; // DS18B20 sensor is present
delayMicroseconds(400); // wait 400 us
}
return(ret);
}
void ds18b20_write_bit(bool value)
{
digitalWrite(DS18B20_PIN, LOW);
pinMode(DS18B20_PIN, OUTPUT);
delayMicroseconds(2);
digitalWrite(DS18B20_PIN, value);
delayMicroseconds(80);
pinMode(DS18B20_PIN, INPUT);
delayMicroseconds(2);
}
void ds18b20_write_byte(byte value)
{
byte i;
for(i = 0; i < 8; i++)
ds18b20_write_bit(bitRead(value, i));
}
bool ds18b20_read_bit(void)
{
bool value;
digitalWrite(DS18B20_PIN, LOW);
pinMode(DS18B20_PIN, OUTPUT);
delayMicroseconds(2);
pinMode(DS18B20_PIN, INPUT);
delayMicroseconds(5);
value = digitalRead(DS18B20_PIN);
delayMicroseconds(100);
return value;
}
byte ds18b20_read_byte(void)
{
byte i, value;
for(i = 0; i < 8; i++)
bitWrite(value, i, ds18b20_read_bit());
return value;
}
bool ds18b20_read(int *raw_temp_value)
{
if (!ds18b20_start()) // send start pulse
return(0);
ds18b20_write_byte(0xCC); // send skip ROM command
ds18b20_write_byte(0x44); // send start conversion command
while(ds18b20_read_byte() == 0); // wait for conversion complete
if (!ds18b20_start()) // send start pulse
return(0); // return 0 if error
ds18b20_write_byte(0xCC); // send skip ROM command
ds18b20_write_byte(0xBE); // send read command
// read temperature LSB byte and store it on raw_temp_value LSB byte
*raw_temp_value = ds18b20_read_byte();
// read temperature MSB byte and store it on raw_temp_value MSB byte
*raw_temp_value |= (unsigned int)(ds18b20_read_byte() << 8);
return(1); // OK --> return 1
}
RTC shield with LCD and Keypad
when the problem is with the SD card?