| 
  • 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 Etch-a-Sketch

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

Part A:

     Question 4:

     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?

     The longest message I can write is 14 characters long. I wrote abcdefghijklmn. When I reach "o," the message moves down to the next line. I can write up to five liens on the LCD.

Using the standard font, the display shows 6 lines of 14 characters each. -0.25

 

 

Part B:

          Question 3:

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

          Here is the code. The part I added is in bold.          

/*

  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>

 

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

   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("datalog.txt", FILE_WRITE);

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

  dataFile.close();

 

  // open the file. note that only one file can be open at a time,

  // so you have to close this one before opening another.

 // Note that I do not I have to re-initialize the "dataFile" file. 

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

 

  // if the file is available, write to 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");

  } 

}

 

void loop()

{

}

 

 

     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.

     I could copy the original file's contents into a temporary string file. Then, I could use the seek() function to move the pointer to the beginning of the original file and write the new text. The pointer is now at the end of the new text, so I can simply add the contents of the temporary string file to the new text.

 

✔good answer

     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 current approach wouldn't work. All I need to  do is write all of the new file's contents into a new file on the SD card. The SD card has significantly more storage space than the arduino, so I can simply use it to store, and read, the contents.

 

 

     Question 4:

     a. Post your code.

     Here is the code:

    

#include <SPI.h>

#include <SD.h>

 

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

const int chipSelect = 17;

char inc = 'a';

#include <SPI.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

 

#define CONTRAST 50

 

// 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,3);

void setup() {

  // put your setup code here, to run once:

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

  //Serial.begin(9600);

  display.begin();

  display.setCursor(0, 20);

  display.setContrast(60);

  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("datalog.txt", FILE_WRITE);

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

  dataFile.close();

 

  // open the file. note that only one file can be open at a time,

  // so you have to close this one before opening another.

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

 

  display.begin();

  display.setContrast(CONTRAST);

  display.setCursor(0, 0);

  display.clearDisplay();

  Serial.println("thing");

  String retVal = "";

 

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

  if (dataFile) {

 

    while (dataFile.available()) {

      inc = dataFile.read();

      retVal += (char) inc;

    }

    Serial.println(retVal);

    display.print(retVal);

    display.display();

    dataFile.close();

  }

 

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

  else {

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

  }

}

 

void loop() {

}

 

 

          Question 5:

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

          I sent the data to the SD card instead of the EEPROM. Then, I changed the code for the graphical LCD to replace the previous LCD. I wrote the data to the SD card and immediately sent it to the LCD. I continuously changed the values to save space on the SD card.

 

          Here is the code:

 

#include <SPI.h>

#include <SD.h>

#include <LiquidCrystal.h>

#include <EEPROM.h>

 

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

const int chipSelect = 17;

char inc = 'a';

 

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

#define CONTRAST 50

 

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

 

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 

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

 

int sensorPin1 = A0;    // select the input pin for the first FSR

int sensorValue1 = 0;  // variable to store the value coming from the first sensor

int sensorValue2 = 0; // variable to store the value coming from the second sensor

int sensorPin2 = A1; // select the input pin for the second FSR

String printValue = "";

File dataLog1;

File dataLog2;

 

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

  display.begin();

  display.setContrast(CONTRAST);

  if (!SD.begin(chipSelect)) {

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

    // don't do anything more:

    return;

  }

  Serial.println("card initialized.");

}

 

void loop() {

  sensorValue1 = analogRead(sensorPin1) / 4;

  sensorValue2 = analogRead(sensorPin2) / 4;

 

  dataLog1 = SD.open("myfile.txt", FILE_WRITE);

  dataLog1.seek(0);

  dataLog1.println(sensorValue1);

  display.setCursor(0, 0);

  dataLog1.close();

 

  dataLog1 = SD.open("myfile.txt");

 

  if(dataLog1.available()){

   display.println(dataLog1.read());

   display.display();

  }

  dataLog1.close();

 

  dataLog2 = SD.open("myfile2.txt", FILE_WRITE);

  dataLog2.seek(0);

  dataLog2.println(sensorValue2);

  display.setCursor(0,10);

  dataLog2.close();

 

  dataLog2 = SD.open("myfile2.txt");

  if(dataLog2.available()){

    display.println(dataLog2.read());

    display.display();

  }

  dataLog2.close();

 

  delay(100);

 

  display.clearDisplay();

}

 

 

Part D:

 

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

             

 

 

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

              Done

                

Comments (1)

xyyue@... said

at 2:30 pm on Aug 11, 2015

Well done.

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