Chau Geeling Lab 5


Lab Report 5: Geeling Chau

Part A. Graphical LCD
3. Try the code from Sketchbook->Strings (instruction for getting this sketch are here: Graphical LCD). Unlike your LCD (where the LCD controller controls the font rendering), the graphical lcd is controlled pixel by pixel, so that the Arduino Micro handles what the text will look like. (So you can get different fonts!). You may have to adjust the contrast.
3a. With the standard font, what is the longest message you can write across one line of the display? How many lines can you write?
I can write “Meep Meep Meep” (approx 14 characters) along one line in the original font and font size. I can write 6 lines of “Meep.”

5. (OPTIONAL) Modify the code to display a logo for yourself (the screen is 84x48 pixels, though we have found that graphics at 64x48 work better). You can either use the graphical library's functions (to make circles, rectangles, etc), or you can use whatever program you like to create an approximately 84x48 monochrome bitmap file (.bmp), such as in MS Paint or the GIMP. You can also convert another image into a monochrome .bmp format suitable for our display.
5a. Upload a photo of your personal logo, shown on your LCD screen, to your Lab 5 page.


Part B. microSD Card
3. Use this code to read the datalog.txt file from the microSD card and print out the contents to the serial terminal.  
Modify the code to: (a) append "He who must not be named!" (or another suitable quote!) to the end of the file's current contents, and (b) print out the updated file to the serial terminal. You'll want to use some of the functions from the example code in the Examples->SD folder. This should only take about 3-5 lines of code.
3a. 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("Bring us a... SHRUBBERY!");
    //Serial.println(dataFile.available());
    dataFile.close();
  }  

I put this writing portion before the “reading” portion so the text I added would immediately show in the Serial Monitor.

3b. 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.
Using only the “seek” function, we can move the position to the start (0). If we wanted to enter some data at that position, it would overwrite the bytes it would be taking up. 
An option that should not overwrite the bytes at the beginning is to copy the original data into a new file (titled “PreviousData”), then delete the original file (dataFile) and create a new file (with the same name as the original file: dataFile) and write the message you want and then print whatever was saved in PreviousData after the new text you entered.

3c. 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?
The file size of my approach is only limited to how much the SD card can hold. Copying the previous data into a new file will duplicate or double the space that is being used. However, if the SD card can handle it, my approach should work.

4. Merge your code from the Part B above and your code from step 3 above to output data from the text file on the microSD card to your graphical LCD.
4a. Post your code.
My Code:
// Prints a certain number of bytes from SD card to the Graphical LCD
#include <SD.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

#define CONTRAST 50
Adafruit_PCD8544 display = Adafruit_PCD8544(7,6,5,-1,-1);
const int chipSelect = 17;

void setup()
{
  display.begin();
  display.setContrast(CONTRAST);
  display.clearDisplay();
  display.setCursor(0, 0); //set top-left corner at text at positon 0,0 (upper left most section of screen).
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, 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.");
 
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println("hola");
    Serial.println(dataFile.available());
    dataFile.close();
  }

  dataFile = SD.open("datalog.txt");
  // if the file is available, write to it:
  if(dataFile) {
    while (dataFile.available()) {
      Serial.write(dataFile.read());
      //display.print(dataFile.read());
    }
    dataFile.close();
  }  
  else {
    Serial.println("error opening datalog.txt");
  }
  dataFile = SD.open("datalog.txt");
  if(dataFile)
  {
    while (dataFile.available()) {
      display.write(dataFile.read());
      display.display();
      delay(500);
    }
    dataFile.close();
  }
  else {
    Serial.println("error printing to LCD");
  }
}

void loop()
{
}


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.
5a. Tell us what you had to change to make this work.
To set up, I included the SD.h library, made a new variable under the type “File,” made a new variable for chipSelect and set that to equal 4 (not sure why 17 also works (18 also works)), and copied in the initializing code from “DumpFile.” Inside my code, I found a nice place to record data: when the LCD is displaying the lucky number. Inside there, I made a directory called “LuckyNum” (because the name could not be more than 8 characters long, otherwise I would have named it LuckyNums b/c it shows all the previous lucky numbers since plugging it in). Then the File variable I declared before (PrevLuckyNums) was opened to File Write: PrevLuckyNums = SD.open(“LuckyNum/PrevNums.txt”, FILE_WRITE), and made it print the new RandNum to that file. To see the data inside PrevLuckyNums, I had to open the file again and Serial.write(PrevLuckyNums.read()). And that was it!  

Part D. Create an Etch-a-Sketch!
a. Upload video of your Etch-a-Sketch in action!

Etch-a-Sketch: http://youtu.be/7CoGooFcaBE 



b. Post a link to the Lab 5 Etch-a-Sketch Hall of Fame.
kk.