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!
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.
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:
Comments (1)
xyyue@... said
at 2:32 pm on Aug 11, 2015
Well done.
You don't have permission to comment on this page.