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

Page history last edited by Ashley Mills 10 years, 10 months ago

Graphical LCD

a. With the smaller font, what is the longest message you can write across one line of the display? How many lines can you write?

a. Upload a photo of your personal logo, shown on your LCD screen.

 

microSD Card

a. Include the code that you had to insert to do this.

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.print(" Whoops!");

    dataFile.close();

  }  

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

  else {

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

  }

 

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

 

  // if the file is available, read it:

  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 (or show us your code!) to insert the same text string, but at the beginning of the file (without over-writing the current contents).

  • save the old content into a string, set position to 0 using seek, print the text and then the old content to the data file.

a. Post your code.

#include <SPI.h>

#include <SD.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

 

const int chipSelect = 17;

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

char character;

String oldMessage = "";

String newMessage = "";

 

void setup()

{

  Serial.begin(9600);

  delay(1000); 

   while (!Serial) {

    ;

  }

 

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

  pinMode(17, OUTPUT);

 

  if (!SD.begin(chipSelect)) {

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

    return;

  }

  Serial.println("card initialized.");

 

  display.begin();

  createMessage(); 

}

 

void createMessage()

{

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

 

  // if the file is available, read it and save the message to be modified as a string:

  if (dataFile) {

    while (dataFile.available()) {

      char character = dataFile.read();

      oldMessage.concat(character);

    }

    dataFile.close();

  }  

  else {

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

  }  

 

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

 

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

  if (dataFile) {

    dataFile.seek(0);

    dataFile.print("Whoops! ");

    dataFile.print(oldMessage);

    dataFile.print(" Whoops!");

    dataFile.close();

  }  

 

   //reads new message into string

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

 

  if (dataFile) {

    while (dataFile.available()) {

      character = dataFile.read();

      newMessage.concat(character);

    }

    dataFile.close();

  }  

}

 

void loop()

{

  //displays new message on graphical LCD

  display.clearDisplay();

  display.setCursor(0,0);

  display.print(newMessage);

}

 

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

 

 

 

  String oldMessage = "";

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

 

  // if the file is available, read it and save the message to be modified as a string:

  if (dataFile) {

    while (dataFile.available()) {

      char character = dataFile.read();

      oldMessage.concat(character);

    }

    dataFile.close();

  }  

  else {

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

  }  

 

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

 

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

  if (dataFile) {

    dataFile.seek(0);

    dataFile.print("Whoops! ");

    dataFile.print(oldMessage);

    dataFile.close();

  }  

 

   //read the new message to the serial monitor

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

  }  

}

 

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

  • I would still have to convert from analog (1024) to byte sized (256), so that would remain the same. however, we now have a LOT more memory to work with and 512 bytes is no longer our cap. The SD card has 2 GB of memory, and acccording to google calculator/conversion magic, 2 GB is equivalent to 2,147,483,648 bytes. So instead of having 512 iterations, I would need to use a while loop that uses the time it took for the song to be recorded. This means I would need to start a timer or counter when they press down the record button and end it when they release. Using this counter, I can play it back for however many iterations the loop went through.

 

Etch-a-Sketch!

 

a. Upload video of your Etch-a-Sketch in action!

b. Post a link to the Lab 5 Etch-a-Sketch Hall of Fame.

 

Comments (0)

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