Hi, i have and LCD 16x2 connected to my Leonardo. I works properly with all the examples son the connection is ok. But I add the LCD to another sketch and it's just showing garbage, and i have no idea why. I just want to show "Writing:" in the first row, and the variable nombrearchivo in the second row. All of this works using serial, so my only problem is the LCD.
Here is the code
#include <LiquidCrystal.h>
#include <SdFat.h>
SdFat sd;
SdFile myFile;
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
// Global Variables
const int chipSelect = 4;
int command = 0; // This is the command char, in ascii form, sent from the serial port
int i;
int second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
byte zero;
char nombrearchivo[13];
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{ return ( (val/10*16) + (val%10) );}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{ return ( (val/16*10) + (val%16) );}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.
// setDateDs1307() solo se necesita una vez, se mantiene el el codigo, por si se debe cambiar la bateria del RTC o modificar la fecha u hora.
/*void setDateDs1307()
{
second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.
minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
dayOfWeek = (byte) (Serial.read() - 48);
dayOfMonth = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(zero);
Wire.write(decToBcd(second) & 0x7f); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}*/
// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.read() & 0x7f);
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
// if (hour < 10)
// Serial.print('0');
// Serial.print(hour, DEC);
// Serial.print(":");
// if (minute < 10)
// Serial.print("0");
// Serial.print(minute, DEC);
// Serial.print(":");
// if (second < 10)
// Serial.print("0");
// Serial.print(second, DEC);
// Serial.print(", ");
// if (dayOfMonth < 10)
// Serial.print('0');
// Serial.print(dayOfMonth, DEC);
// Serial.print(" ,");
// Serial.print(month, DEC);
// Serial.print(" 20");
// if (year < 10)
// Serial.print("0");
// Serial.println(year);
snprintf(nombrearchivo,13,"%02d%02d20%02d.txt", second, month, year); //merge together
Serial.println(nombrearchivo);
}
void setup() {
lcd.begin(16, 2);
Wire.begin();
zero=0x00;
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// set up the LCD's number of columns and rows:
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
}
void loop() {
/*if (Serial.available()) { // Look for char in serial que and process if found
command = Serial.read();
if (command == 84 || command == 116) { //If command = "Tt" Set Date
//setDateDs1307();
getDateDs1307();
if (!myFile.open(nombrearchivo, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening test.txt for write failed");
}
// if the file is available, write to it:
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
}
else if (command == 82 || command == 114) { //If command = "Rr" Read Date ... BBR
getDateDs1307();
Serial.println(" ");
}
Serial.print("Command: ");
Serial.println(command); // Echo command CHAR in ascii that was sent
}
command = 0; // reset command
delay(100);*/
getDateDs1307();
year+=2000;
if (!myFile.open(nombrearchivo, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening test.txt for write failed");
}
// if the file is available, write to it:
Serial.print("Writing");
lcd.setCursor(0, 0);
lcd.print("Writing:");
lcd.setCursor(0, 1);
lcd.print(nombrearchivo);
myFile.println("testing 1, 2, 3.");
if(!myFile.timestamp(T_WRITE,year,month,dayOfMonth,hour,minute,second)){
Serial.println("Mod date failed");
}
// close the file:
myFile.close();
Serial.println("done.");
lcd.clear();
delay(250);
}
I have a lot of commented sections...
Thanks in advance.