CD Lab 5


Part A. Graphical LCD

a. With the smaller font, what is the longest message you can write across one line of the display? How many lines can you write?

The longest message has 14 characters and  you can write 5 lines.

 

a. Upload a photo of your personal logo, shown on your LCD screen, to your Lab 5 page.

a. Include the code that you had to insert to do this in your lab writeup. 

// insert before the code that reads the file.

 File dataFile = SD.open("datalog.txt",FILE_WRITE);

 if(dataFile){

   dataFile.println();

   dataFile.println("Not my daughter, you bitch!");

  dataFile.close();

 }

 

b. Explain what would you do differently (or show us your code!) to insert the same text string, but at the beginning of the file (without over-writing the current contents).

I would iterate over the old file to first store its contents into a string. Then I would use dataFile.seek(0), to get to the start of the file. And finally I would write the new text string into the file first, and then write in the string with the old contents.

 

a. Post your code.

#include <SPI.h>

#include <SD.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

 

//On the Arduino Micro, the SS pin is defined in software as pin 17. 

 

Adafruit_PCD8544 display = Adafruit_PCD8544(7,6,5,-1,-1);

const int chipSelect = 17;

File myFile;

void setup()

{

 

 

 

 // Open serial communications and wait for port to open:

  Serial.begin(9600);

  delay(1000); //this 1 second delay isn't strictly speaking necessary, but it seems to smooth over the USB serial monitor a bit. 

   while (!Serial) {

    ; // wait for serial port to connect. Needed for Leonardo only

  }

  display.begin();

 

 

  Serial.print("Initializing SD card...");

  pinMode(17, OUTPUT); //set SS pin as 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.");

  display.begin();

  display.setContrast(40);

 

    myFile = SD.open("test.txt", FILE_WRITE);

 

  // if the file opened okay, write to it:

  if (myFile) {

    Serial.print("Writing to test.txt...");

    myFile.println("testing 1, 2, 3.");

// close the file:

    myFile.close();

    Serial.println("done.");

  } else {

    // if the file didn't open, print an error:

    Serial.println("error opening test.txt");

  }

 

}

 

 

void loop() {

  Serial.println("Working..");

  display.clearDisplay();

  display.setCursor(0, 0); //set top-left corner at text at positon 0,0 (upper left most section of screen). 

  display.print("Hello, World!");

 

  display.setCursor(0, 10);

  myFile = SD.open("test.txt");

  if (myFile) {

 

    // read from the file until there's nothing else in it:

    while (myFile.available()) {

    //Serial.write(myFile.read());

        display.write(myFile.read());

    }

    // close the file:

    myFile.close();

  } else {

  // if the file didn't open, print an error:

    Serial.println("error opening test.txt");

  }

 

 

  display.display();

  delay(500);  

 

 

}

a. Tell us what you had to change to make this work.

To get the display to work I needed to change the library and setup to be for the graphical lcd. Then I had to change all of the lcd. functions to be display.functions. I also had to add display.display() so that it would show up on the screen. 

 

To get the SD card to work, I had to first initialize the SD in the setup. Then I had to change all of the EEPROM.functions to be dataFile.functions. In each function that used the Sd card, I had to first open and then close the file. I also no longer need to keep track of the memory addresses. To move to the different laps I would have to use the seek() function instead.

 

Etch-a-Sketch!