lab5 batu


 

 

 

 

 

 

 

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?

14 characters message in 6 lines.

 

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

#include <SPI.h>

#include <SD.h>

 

const int chipSelect = 17;

 

void setup()

{

 // Open serial communications and wait for port to open:

  Serial.begin(9600);

  delay(1000);

  }

 

  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("You shall not pass!");

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

               use seek and position functions