I'm having trouble making my code work on my Mega for connecting to my SD card reader. I can't get my code to "initialization done.".
#include <LiquidCrystal.h> // include the LiquidCrystal library
#include <SPI.h> //
#include <SD.h> // include the SD library for reading files from an SD card
const int numRows = 2; // number of rows on the display
const int numCols = 16; // number of columns on the display
File songFile; // Use for the song file.
// initialize the display with the correct number of rows and columns
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
// set the baud rate for serial communication
Serial.begin(9600);
// initialize the display
//lcd.begin(numRows, numCols);
// clear the display
//lcd.clear();
// initialize the SD card and make sure it is connected
Serial.begin(9600);
while (!Serial)
{
// Wait for serial port to connect. Needed for native USB port only.
}
// Checking if the SD card is connected.
Serial.print("Initializing SD card... ");
// Added Delay
delay(1000);
if (!SD.begin(10))
{
// SD card was not connected.
Serial.println("initialization failed!");
while (1);
}
// SD card was found.
Serial.println("initialization done.");
// SD card was connected.
File songFile = SD.open("song.txt");
Serial.println("Found SD card." + songFile);
// set the cursor position to the first row and first column
lcd.begin(0, 0);
// open the song text file in read mode
if (!songFile)
{
Serial.println("Error opening song file!");
}
else
{
// read the song text from the file and write it on the display
while (songFile.available())
{
lcd.print((char)songFile.read());
}
}
// close the file
songFile.close();
Serial.println("The song file has been closed.");
}
void loop()
{
// nothing happens after setup
}
I took my working code that reads my SD card on my Arduino Uno. I connected them the same way. And I know that code works because I can read and write to the SD card.
#include <SPI.h>
#include <SD.h>
File myFile; // How the serial displays the test.txt file.
File upLoadFile; // How I write what I want to the test2.txt
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial)
{
// Wait for serial port to connect. Needed for native USB port only.
}
// Checking if the SD card is connected.
Serial.print("Initializing SD card... ");
// Added Delay
delay(1000);
if (!SD.begin(10))
{
// SD card was not connected.
Serial.println("initialization failed!");
while (1);
}
// SD card was found.
Serial.println("initialization done.");
// Added Delay
delay(1000);
// open the file for reading:
myFile = SD.open("bionicle.txt");
if (myFile)
{
Serial.println("bionicle.txt:");
// Added Delay
delay(100);
// Read from the file until there's nothing else in it:
while (myFile.available())
{
// Writing out the text file, with a delay between each char.
Serial.write(myFile.read());
delay(75);
}
// Close the file:
myFile.close();
Serial.println(" ");
// Added Delay
delay(1000);
Serial.println("Done reading from bionicle.txt.");
Serial.println("The File is now closed.");
}
else
{
// If the file didn't open, print an error:
Serial.println("error opening bionicle.txt");
}
// Added Delay
delay(1000);
// Open the file to write to it.
Serial.println("Now opening test2.txt.");
upLoadFile = SD.open("test2.txt", FILE_WRITE);
if (upLoadFile)
{
Serial.println("The file test2.txt was open, now writing to the file.");
// This is how we write to the file.
// Added Delay
delay(1000);
upLoadFile.println("We will write something in this file cleverly some day.");
// close the up load file:
upLoadFile.close();
Serial.println("Done writing to test2.txt.");
Serial.println("The File is now closed.");
}
else
{
// If the file didn't open, print an error:
Serial.println("error opening test2.txt");
}
}
void loop()
{
// nothing happens after setup
}
I also have a working code for making my LCD display with the same connections on the Mega.
#include <LiquidCrystal.h>
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
lcd.begin(16, 2);
lcd.print("First line");
lcd.setCursor(0,1);
lcd.print("Second line");
}
void loop()
{
}
Do I need to connect my SD card on my Mega differently or declar the pins some how?