LABORATORY 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?

a 14 characters phrase, 6 lines

 

PART B - MICRO SD CARD

 

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 the file is available, write to it:

  if (dataFile) {

    dataFile.println("The treacherous are ever distrustful.");

    dataFile.close();

    Serial.println("The file has been modified");

  }  

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

  else {

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

  } 

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

 if (dataFile)

 {

   Serial.println("Modified File: ");

   while(dataFile.available())

   {

     Serial.write(dataFile.read());

   }

   dataFile.close();

 }

 else

 {

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

 }

}

void loop()

{

}

b. Explain what would you do differently (or show us your code!) to insert the same text string, but at the beginning of the file (without over-writing the current contents).

 

would use the seek() and position() functions on the arduino SD library to start writing on the beginning of the file (position 0) 

4.

a. Post your code.

#include <SPI.h>

#include <SD.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

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

//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 the file is available, write to it:

  if (dataFile) {

    dataFile.println("The treacherous are ever distrustful.");

    dataFile.close();

    Serial.println("The file has been modified");

  }  

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

  else {

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

  } 

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

 display.begin();

 display.setContrast(50);

 display.setCursor(0,0);

 display.clearDisplay();

 if (dataFile)

 {

    while(dataFile.available())

    {

   display.write(dataFile.read());

    } 

   display.display();  

   dataFile.close();

 }

 else

 {

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

 }

}

void loop()

{

}