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

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.

View
 

Hamnqvist Karl Lab 5

Page history last edited by zahraa@... 5 years, 2 months ago

Part A. Graphical LCD

 

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 per line, 6 lines max

 

Part B. microSD Card

 

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

File dataFile = SD.open(“datalog.txt",FILE_WRITE);

if (dataFile) {

    while (dataFile.available()) {

 

      Serial.write(dataFile.read());

 

    }

    dataFile.write("He who must not be named!");

 

 

    dataFile.close();

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

 

    while (dataFile.available()) {

 

      Serial.write(dataFile.read());

 

    }

 

 

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.

Read all contents of the file, save into array based on the sized of the file, then seek(0) and write the new line followed by the saved data in the array.

 

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?

Make a new file, write the new content and then read the data from the other file and dump the data

 

 

a. Post your code.

/*

  SD card file dump

 

 This example shows how to read a file from the SD card using the

 SD library and send it over the serial port.

 

 The circuit:

 * SD card attached to SPI bus as follows:

 ** MOSI - pin 11

 ** MISO - pin 12

 ** CLK - pin 13

 ** CS - pin 17 (listed on Micro as SS)

 

 

 created  22 December 2010

 by Limor Fried

 modified 9 Apr 2012

 by Tom Igoe

 modified 1 May 2013

 by Harry Johnson

 This example code is in the public domain.

 

 */

 

#include <SPI.h>

#include <SD.h>

#include <SPI.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

 

#define CONTRAST 55

 

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. 

  display.begin();

  display.setContrast(55); 

   display.clearDisplay();

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

 

 

 int counter = 0;

  // if the file is available, write to it:

  if (dataFile) {

    dataFile.seek(0);

    dataFile.write("first32\n");

 

 

 

    dataFile.close();

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

 

    while (dataFile.available()) {

 

      Serial.write(dataFile.read());

 

    }

 

  }  

 

 

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

  else {

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

  } 

}

 

void loop()

{

 

 

  display.setCursor(0, 0); //set top-left corner at text at positon 0,0 (upper left most section of screen). 

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

 

    while (dataFile.available()) {

 

      display.print(char(dataFile.read()));

 

    }

 

  display.display();

  delay(500);

}

 

 

 

 

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

Make a new file for the data logging, add the basic code to init the sd card. Then instead of writing to the EEPROM I wrote to the file, when I wanted to display the data on the LCD I opened my file and read and printed the data. 

 

 

 

 

 

Comments (1)

xyyue@... said

at 2:07 pm on Aug 11, 2015

Well done.

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