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.
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).
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.
a. Upload video of your Etch-a-Sketch in action!
b. Post a link to the Lab 5 Etch-a-Sketch Hall of Fame.