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

Wang_Tien-Sheng_Lab5

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

Make an Etch-a-Sketch!

 

In The Report

I was told to embed a video of the data logger in this section.

Here it is:

 

 

Part A. Graphical LCD

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 you can write across one line can only be 14 characters long.

     You can write 5 lines with the standard font.

 

a. (OPTIONAL) Upload a photo of your personal logo, shown on your LCD screen, to your Lab 5 page.

     Gucci

 

Part B. microSD Card

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

     Any changed parts are highlighted below.

 

#include <SPI.h>

#include <SD.h>

 

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

const int chipSelect = 17;

File dataFile;

 

 

void setup()

{

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

  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for Leonardo only

  }

  delay(1000);

 

  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.

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

 

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

  if (dataFile) {

    dataFile.println("Lord Voldemort. Remember Cedric Diggory.");

    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 (dataFile) {

    while (dataFile.available()) {

      Serial.write(dataFile.read());

    }

    // close the file:

    dataFile.close();

  }

}

 

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.

     To do this, you would need to find a way to add data to the beginning instead of the end of the file. FILE_READ as an argument of SD.open() opens the file for reading, starting from the beginning of the file. FILE_WRITE as an argument opens the file for reading and writing,      starting from the end of  the file. 

     We need to find a way to open the file for reading and writing from the beginning of the file in order to append a string at the beginning of the file.

The better answer is to copy the contents of the file into a string variable, move the file pointer to the start of the file, write the new text to the start of the file, write the contents of the string variable to the end the file, and close the file.   -0.1

 

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?

     The approach would not work if the file were larger than 2.5KB. To work around this limitation, you need to make use of the Arduino's memory as a que. You would need to split up the data and process it in smaller portions.

 

 

a. Post your code.

     

#include <SPI.h>

#include <SD.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

 

#define CONTRAST 50

 

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

const int chipSelect = 17;

File dataFile;

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

 

void setup()

{

  display.begin();

  display.setContrast(CONTRAST);

  display.clearDisplay();

  display.setCursor(0, 0);

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

  Serial.begin(9600);

  while (!Serial) {

      ; // wait for serial port to connect. Needed for Leonardo only

    }

  delay(1000);

  Serial.println("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.print("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.

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

 

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

  if (dataFile) {

    dataFile.println("Lord Voldemort. Remember Cedric Diggory.");

    dataFile.close();

  }  

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

   if (dataFile) {

      while (dataFile.available()) {

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

        display.display();

      }

      // close the file:

      dataFile.close();

    }

      else {

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

    }

  display.display();

}

 

void loop()

{ 

}

 

 

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

     To make this work, I had to include the libraries for SPI and SD.

     In the void setup(){}, I had to include the standard SD initialization code. That includes stuff to check if the SD is there and working.

     In the void loop(){}, I had to create a variable dataString. dataString is just so we can convert analogRead(sensorPin); to a string. We can only write strings to the SD card file.

     Next, we add a block of code that opens the file datalog.txt and prints dataString, then closes datalog.txt

 

     Here is the code:

 

//Jonathan Wang

//lab 5

 //Create your data logger with SD

 //you can use DumpFile.ino to read datalog.txt

 

#include <SPI.h>

#include <SD.h>

 

const int sensorPin = A1;          //sensorPin is A1

const int chipSelect = 17;

 

void setup()

{

  //I was too lazy to move the potentiometer(sensor) to another position, so I just made

  //A0 and A2 as power and ground.

  pinMode(A0, OUTPUT);

  pinMode(A2, OUTPUT);

  digitalWrite(A0, LOW);              //potentiometer is attached to this

  digitalWrite(A2, HIGH);            //potentiometer is attached to this

 

  Serial.begin(9600);                //start the serial port

  while (!Serial) {

    ; // wait for serial port to connect. Needed for Leonardo only

  }

  pinMode(sensorPin, INPUT);         //set the sensor as an input

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

  pinMode(10, 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.");

}

 

void loop()

{

  String dataString = "";

 

  int sensor = analogRead(sensorPin);

  dataString += String(sensor);

 

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

  if (dataFile) {

    dataFile.println(dataString);

    dataFile.close();

    // print to the serial port too:

    Serial.println(dataString);

  }

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

  else {

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

  }

}

 

 

Part D. Create an Etch-a-Sketch!

 

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

     

     Here is a code because it took so long:

#include <SPI.h>

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

#include <SD.h>

 

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

 

const int chipSelect = 17;

const int ENC_A = 9;

const int ENC_B = 10;

 

const int erasePin = 11;

const int savePin = 8;

const int recallPin = 12;

int rows;

 

File myFile;

 

void setup()

{

  pinMode(erasePin, INPUT_PULLUP);

  pinMode(savePin, INPUT_PULLUP);

  pinMode(recallPin, INPUT_PULLUP);

  pinMode(ENC_A, INPUT_PULLUP);

  pinMode(ENC_B, INPUT_PULLUP);

  display.begin();

  display.setContrast(55);

  display.clearDisplay(); //clear all the screen

 

  Serial.begin(9600);

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

 

}

 

void loop()

{

  if(digitalRead(erasePin) == LOW)

  {   

    display.clearDisplay();

    display.display();

  }

  if(digitalRead(savePin) == LOW)

  {

    SD.remove("graphic.txt");

    myFile = SD.open("graphic.txt", FILE_WRITE);   

    myFile.write(display.pcd8544_buffer, 504);

    myFile.close();

  }

  if(digitalRead(recallPin) == LOW)

  {

    myFile = SD.open("graphic.txt", FILE_READ);

    myFile.readBytes((char *)display.pcd8544_buffer, 504);

    myFile.close();

  }

  static unsigned int counter4x = 0;      //the SparkFun encoders jump by 4 states from detent to detent

  static unsigned int counter = 0;

  static unsigned int prevCounter = 0;

  int tmpdata;

  tmpdata = read_encoder();

  if( tmpdata)

  {

    counter4x += tmpdata;

    counter = counter4x/4;

    if (prevCounter != counter)

    {

      if (counter == prevCounter+1)

      {

        display.drawPixel(counter, rows, BLACK);

        display.display();

      }

      if (counter == prevCounter-1)

      {

        display.drawPixel(counter, rows, WHITE);

        display.display();

      }

 

      if (counter > 84)

      {

        counter4x = 0;

        rows++;

      }

    }

    prevCounter = counter;

  }

}

 

/* returns change in encoder state (-1,0,1) */

int read_encoder()

{

  static int enc_states[] = {

    0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0  };

  static byte ABab = 0;

  ABab *= 4;                   //shift the old values over 2 bits

  ABab = ABab%16;      //keeps only bits 0-3

  ABab += 2*digitalRead(ENC_A)+digitalRead(ENC_B); //adds enc_a and enc_b values to bits 1 and 0

  return ( enc_states[ABab]);

}

 

 

 

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

     ok

 

Comments (1)

xyyue@... said

at 2:32 pm on Aug 11, 2015

Well done.

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