harrymj Lab 5


Parts A/B: N/A

 

Part C:

 

I chose to implement a etch-a-sketch with simple non-volatile quicksave functionality (only a single image stored).

 

 

Code:

 


/*

Etch-A-Sketch

EE47, Spring 2013, Lab 5

Harry Johnson

This example code is released into the public domain, but it'd be nice if you reference me if you use it. Thanks!

*/

 

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

#include <math.h>

#include <SPI.h>

#include <SD.h>

 

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

//I used a voltage divider for the reset line to improve reliability. Might be something to look at for the future because it doesn't need to be particularly fast.

 

File myFile;

 

const int savePin = 8;

const int loadPin = 9;

 

const int xPin = 1; //analog 1

const int yPin = 0; //analog 0

 

void setup() {

  analogReference(EXTERNAL); //I'm running the potentiometer on 3.3V, so I ran 3.3V to the external reference pin. Now I have full range reading on my pots!

 

  display.begin();

  display.clearDisplay();

  display.display();

 

  pinMode(8, INPUT_PULLUP);

  pinMode(9, INPUT_PULLUP);

 

  pinMode(17, OUTPUT);

  SD.begin(17);

}

 

uint8_t displayCounter = 0;

 

void loop() {

  display.drawPixel(map(analogRead(xPin), 0, 1023, 0, 83), map(analogRead(yPin), 0, 1023, 0, 47), 1); //draw pixel at current location specified by pots. 

 

  displayCounter++;

  if(displayCounter == 10) { //update the display approximately every 10ms. 

    displayCounter = 0; 

    display.display();

  }

 

  if(sqrt(pow(analogRead(2), 2) + pow(analogRead(3), 2) + pow(analogRead(3), 2)) > 1200) display.clearDisplay(); //Shake detection for display clearing.

  //Powers and square root roughly determine magnitude of accel seen. Values over 1200 indicate vigorous shakiing. 

 

  if(digitalRead(8) == LOW) { //Save the display buffer to SD.

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

    myFile.seek(0); //save to the beginning of the file, if it already exists. 

    myFile.write(display.pcd8544_buffer, 504); //write 504 bytes (size of display). Saved as binary data. 

    myFile.close(); //Memory leaks, YOU SHALL NOT PASS!

  }

 

  if(digitalRead(9) == LOW) { //put the saved data from SD back on the screen. 

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

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

    myFile.close();

  }

 

  delay(1);

}