Hardware Reference
In-Depth Information
start the SD reader. Waiting for the connection to be fully operational allows for the information about the setup of the
SD reader to be caught and printed to the Android application. The loop function checks for the connection and will
try to reestablish the connection if disconnected. The code waits for a defined incoming character from the Android
before opening and reading the test file on the SD card to the buffer. If the file is not available, an error is printed in the
Android application. Step through Listing 4-16 and upload it to the Mega ADK board.
Listing 4-16. Arduino SD Reader and ADK Sketch
#include <SD.h> // must be included before AndroidAccessory.h
#include <AndroidAccessory.h> // needed library to work with ADK connections
byte buffer[80];
// initialize the accessory object with identifying data
AndroidAccessory ADK("Manufacturer", "Model", "Description",
"1.0", "Pro Arduino", "0000000012345678");
void setup() {
ADK.begin(); // start the connection to the Android
while(!ADK.isConnected()); // wait till the ADK is connected to finish setup
pinMode(10, OUTPUT); // SD chip select
if (!SD.begin(10)) { // start the SD and check for failure
ADK.println("SD initialization failed!");
}
else
ADK.println("SD initialization done.");
} // end setup
void loop() {
if (ADK.isConnected()) { // check for connection
if (ADK.available() > 0){ //check for incoming data
if (ADK.read() == 'a') { // look for an incoming 'a' to start file transfer
File TestFile = SD.open("test.txt"); // open the test.txt
if (TestFile) { // if file did not open, throw an error
while (TestFile.available()) { // read till the file end has been reached
for (int i = 0 ; i < 80 ; i ++ ){ // read 80 bytes into buffer before sending
buffer[i] = TestFile.read();
}
ADK.write (buffer , 80); // send buffer to the Android
} // end while (TestFile.available())
TestFile.close(); // close the file no longer needed
} // end if (TestFile)
else{
ADK.println ("File Error");
}
} // end if (ADK.read() == 'a')
} // end if (ADK .available() > 0)
} // end if (ADK .isConnected())...
} // end void loop()
Once the Arduino is configured with the SD reader and programmed with the sketch, a test.txt file must be
created and located in the root directory of a FAT-formatted SD card. Copy any text readme file to the SD card from a
computer and rename it test.txt on the SD card. Plug the Arduino into the Android device, insert the SD card into
the reader, and power it on.
 
Search WWH ::




Custom Search