| 
  • 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
 

Arslan_Mert_LAB5

Page history last edited by zahraa@... 4 years, 12 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?

 

Each line seems to be fourteen characters long with the standard font. There are six lines that can be written on.

 

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

 

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

  if (dataFile) {

      dataFile.write("destiny.");

      dataFile.close();

    } else {

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

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

    }

 

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.

 

To insert something to the beginning of a file, first all the contents must be stored elsewhere as variables. Then the string can be written to the file after everything has been cleared. Finally, the old contents are copied back to the 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?

 

My approach could not possibly work if the file was larger than the Arduino's memory, as the contents could not be copied over. To overcome this, it is possible to create an additional file in the SD. Then the contents of the first file could be copied over to the other file part by part. (open file 1, copy part to arduino, close file 1, open file 2, copy part to file 2, close file 2, open file 1...)

 

 

a. Post your code.

 

#include <EEPROM.h>

#include <SPI.h>

#include <SD.h>

int state = 0;

int button = 0;

int address = 0;

int address1 = 0;

int adressGet = 0;

const int chipSelect = 4;

File dataFile;

 

void setup() {

  pinMode(2, INPUT);

  Serial.begin(9600);

  while (!Serial) {

    ;

  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {

    Serial.println("Card failed, or not present");

    return;

  }

  Serial.println("card initialized.");

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

}

 

void loop() {

  state = analogRead(A1);

  while (state < 512) {

    state = analogRead(A1);

    button = digitalRead (2);

    if (button == HIGH) {

      int value = analogRead (A0) / 4;

      if (dataFile) {

      Serial.print("Writing to datalog.txt...");

      dataFile.write(value);

      dataFile.close();

      Serial.println("done.");

    } else {

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

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

    }

    while (state >= 512) {

      state = analogRead(A1);

      button = digitalRead (2);

       if (button == HIGH) {

           if (dataFile) {

                int value = dataFile.read();

                Serial.println(value);

                delay(500);

           } else {

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

         }

       }

      }

    } 

  }

}

 

 

 

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

 

First of all, I had to import additional libraries to my data logger, such as the SD library. Then of course the void setup section had to be modified so that it initialised the SD. Then I just adjusted write and read sections so that instead of the EEPROM they use the file.

 

 

Video????? 

 

 

Comments (1)

xyyue@... said

at 2:13 pm on Aug 11, 2015

Well done.

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