| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Lab 5 (4)

Page history last edited by David S 9 years, 8 months ago

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?

 

✔ One line of the display can write 14 characters. I found this by making it print “Hello, Worldsa5” to ensure that the exclamation mark does not extend the line by using a smaller amount of space. The display can also write 6 lines (in total).

 

a. 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(“He who must not be named!”);

// close the file:

dataFile.close();

Serial.println("done.");

} else {

// if the file didn't open, print an error:

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

}

 

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

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

}

}

 

b. Explain what would you do differently to insert the same text string, but at the beginningof 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.

 

Hm, this would overwrite the contents already there. The idea is to insert a specific text string at the beginning of the file, without losing the data that's already there. You need to store that data somewhere else first, write in the new text string, and then replace the original data (now at the end).

 

The code to do this would be to replace the dataFile.println(“…”) to:

dataFile.seek(0);

dataFile.print(“…”);

 

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?

 

Again, the idea is not to overwrite the data already in the file. Think about ways to do the preceding without knowing the size of the text string that you want to include: would it over-run the SRAM? Could you use any of the SD!  file commands?

 

When the message is created with the following code:

int counter = 0;

(int capacity = 2000;)

dataFile.seek(0);

while(counter < capacity) {

dataFile.print(“H”);

counter++;

}

the result is that the entire file is overwritten to contain only “H”s.

 

a. Post your code.

 

✔ #include <SPI.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

#include <SPI.h>

#include <SD.h>

 

#define CONTRAST 50

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

const int chipSelect = 17;

 

// pin 7 - Serial clock out (SCLK)

// pin 6 - Serial data out (DIN)

// pin 5 - Data/Command select (D/C)

// RST and CS are not connected to Arduino, and as such are listed as pin -1.

 

 

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

 

void setup()

{

// initialize the serial communications:

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

File dataFile = SD.open("test.txt");

display.begin();

display.clearDisplay();

display.setContrast(CONTRAST);

display.setCursor(0,0);

if(dataFile) {

while(dataFile.available()) {

display.print(dataFile.read());

}

}

display.display();

}

void loop()

{

}

 

Comments (1)

David S said

at 7:43 pm on Aug 19, 2014

Good job, only think about how to answer the text string question again.

You don't have permission to comment on this page.