Brody Cameron Lab 5


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

The display holds 14 characters, with up to 6 lines

 

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

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

dataFile.println("He who must not be named.");

dataFile.close();

 

Correct but the better answer is :  (-0.1)

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

 

if (dataFile) {

  dataFile.println("He who must not be named!");

 

  dataFile.seek(0);

  while (dataFile.available()) {

    Serial.write(dataFile.read());

  }

  dataFile.close();

}

 

 

b. Explain what would you do differently to insert the same text string, but at the beginning of the file (without over-writing the current contents). You don't have to code this: just explain the process. If you're interested and have time make it work, show us your program.

To do this, save what's currently on the SD card as a variable, then concatenate what you want to add on to the beginning of the original.

c. Now tell us if your approach would work if the file were larger than your Arduino's memory (which is 2.5KB). If not, how could you work around that limitation?

To work around the memory limitation, you have to cut up the file into chunks first in order to add them one by one.

 

a. Post your code.

 

#include <SPI.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

#include <SD.h>

 

#define CONTRAST 50

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

int counter;

const int chipSelect = 17;

 

void setup() {

  display.begin();

  display.setContrast(CONTRAST);

  display.setContrast(50);

  counter = 0;

  display.clearDisplay();

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

  pinMode(17, OUTPUT);

 

  if (!SD.begin(chipSelect)) {

    display.println("Card failed, or not present");

    // don't do anything more:

    return;

  }

  display.println("card initialized.");

 

 

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

  dataFile.println("He who must not be named.");

  dataFile.close();

  if (dataFile) {

    while (dataFile.available()) {

      display.write(dataFile.read());

    }

    dataFile.close();

  }  =

  else {

    display.println("error opening datalog.txt");

  } 

}

 

 

 

void loop() {

}

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

Instead of writing to the EEPROM, I write to the SD card.