Matt Benavente - Lab 5


Part A. Graphical LCD

 

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?  4 lines

Using the standard font, the display shows 6 lines of 14 characters each. -0.25

 

 

(a) append "He who must not be named!" (or another suitable quote!) to theend of the file's current contents, and (b) print out the updated file to the serial terminal.

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();

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

 

 

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();

}

-0.1

 

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.  

Move the print/write statement to start of the program 

Correct but not specific, One way to do that is copy the contents of the file into a string variable, move the file pointer to the start of the file, write the new text to the start of the file, write the contents of the string variable to the end the file, and close the file.   -0.25

 

 

 

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?  It would take more time to perform if it were a larger file since you would have to move everything, unless you wrote it to a separate file and read that file first.

 

 

4. Merge your code from Part A above and your code from step B3 to output data from the text file on the microSD card to your graphical LCD.

 

Note: Your fellow students have found that you have to init your SD card before your LCD in the setup function. 

 

#include <SPI.h>

#include <SD.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

#define CONTRAST 50

 

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

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

const int chipSelect = 17;

 

void setup()

{

  Serial.begin(9600);

  display.begin();

  display.setContrast(CONTRAST);

  delay(1000); 

   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:

    return;

  }

  SD.remove("datalog.txt");

  Serial.println("card initialized.");

 

 

  // open the file. note that only one file can be open at a time,

  // so you have to close this one before opening another.

   //SD

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

    dataFile.print("If the time should come when you have to make a choice between what is right, and what is easy, remember what happened to a boy who was good, and kind, and brave, because he strayed across the path of "); 

    dataFile.print("he who must not be named!");

    dataFile.close();

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

 

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

  if (dataFile) {

    while (dataFile.available()) {

      Serial.write((dataFile.read()));

 

    }

    dataFile.close();

  }  

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

  else {

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

  } 

}

 

void loop()

{

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

    dataFile.close();

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

    display.clearDisplay();

    display.setCursor(0, 0);

 

    if (dataFile) {

    while (dataFile.available()) {

      display.print(char(dataFile.read()));

 

    }

    dataFile.close();

  }  

 

 

    display.display();

    delay(500); 

 

 }

 

 

 

 

5. Modify your data logger code from last lab so that you can write to the microSD card. If you took apart your data logger already, make the changes to your old code that you would need to make it write to the SD card.

 

 

 

 

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

I added code to write to sensor values to datacard, then I added code to read data card to display.

 

 

Part D. Create an Etch-a-Sketch!

 

a. Upload video of your Etch-a-Sketch in action!

https://www.youtube.com/watch?v=vr152DswIeU