• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.


Lab 5 (4)

Page history last edited by David S 6 years, 2 months ago

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?


✔ One line of the display can write 14 characters. I found this by making it print “Hello, Worldsa5” to ensure that the exclamation mark does not extend the line by using a smaller amount of space. The display can also write 6 lines (in total).


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


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


// if the file is available, write to it:

if (dataFile) {

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

// close the file:



} else {

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

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



dataFile = SD.open("datalog.txt");

if (dataFile) {

while (dataFile.available()) {





// if the file isn't open, pop up an error:

else {

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




b. Explain what would you do differently to insert the same text string, but at the beginningof 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.


Hm, this would overwrite the contents already there. The idea is to insert a specific text string at the beginning of the file, without losing the data that's already there. You need to store that data somewhere else first, write in the new text string, and then replace the original data (now at the end).


The code to do this would be to replace the dataFile.println(“…”) to:




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?


Again, the idea is not to overwrite the data already in the file. Think about ways to do the preceding without knowing the size of the text string that you want to include: would it over-run the SRAM? Could you use any of the SD!  file commands?


When the message is created with the following code:

int counter = 0;

(int capacity = 2000;)


while(counter < capacity) {




the result is that the entire file is overwritten to contain only “H”s.


a. Post your code.


✔ #include <SPI.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

#include <SPI.h>

#include <SD.h>


#define CONTRAST 50

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

const int chipSelect = 17;


// pin 7 - Serial clock out (SCLK)

// pin 6 - Serial data out (DIN)

// pin 5 - Data/Command select (D/C)

// RST and CS are not connected to Arduino, and as such are listed as pin -1.



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


void setup()


// initialize the serial communications:


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


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:



Serial.println("card initialized.");

File dataFile = SD.open("test.txt");





if(dataFile) {

while(dataFile.available()) {






void loop()




Comments (1)

David S said

at 7:43 pm on Aug 19, 2014

Good job, only think about how to answer the text string question again.

You don't have permission to comment on this page.