Ge Stella Lab 5


Part A. Graphical LCD

3. Sketchbook → Strings

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?

I can write “Hello, World!1”, and can write 5 lines.

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.  

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

#include <SPI.h>

#include <SD.h>

 

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

const int chipSelect = 17;

 

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

 }

 

 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.");

 

 // 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(dataFile)

 {

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

{

}

 

 

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.

You could create a new file and write whatever you want in it, and then open the datalog file and read() the contents and store them in a variable. You could then write the variable to the new file.

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?

You could break it up into pieces,  and store them one by one so the memory used never exceeds the limit.

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.

a. Post your code.

#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("He who must not be named!");

Serial.println(dataFile.available());

dataFile.close();

 }

 

 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.

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

I would first need to add this to the beginning:

#include <SPI.h>

#include <SD.h>

#include <SPI.h>

and instead of EEPROM.write() and EEPROM.read() I would be using the file.print() and file.read() functions, and I would have to make sure to remember to open and close the file.

 

Part D. Create an Etch-a-Sketch!

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