Hardware Reference
In-Depth Information
In Part 8, the last function included in the Arduino sketch handles the Open Accessory connection. This function
is pulled at a regular interval to check for incoming data from the Android device. When the Android device is
connected, a Boolean flag is set to true to avoid running the isConnected function too often by other functions that
need to know when the Android device is connected. A predetermined set of bytes are used as commands from the
Android device to allow for syncing of the log information, deleting the log, or disconnecting the Android device
from the Arduino. The command for syncing the data is an ASCII a ; when this command is issued from the Android
device, the Arduino will read the log file 34 bytes at a time and send the information to the Android device for further
processing. When a command of a b is received, the Arduino will stop sending updated information to the Android.
The log file will be deleted when a command of c is received. If the Android device is not connected, the two flags that
control the sending of updated data to the Android device are set to false .
Listing 8-3. Data Logger and ADK Handler, Part 8 of 8
void HandleADK(){
if (ADK.isConnected()) {
delay (100);
ADKisConnected = true;
if (ADK.available() > 0){ // check for incoming data
switch (ADK.read()){
case 'a': {
Serial.println('a');
File LogFile = SD.open("sensor.log");
If (LogFile) {
while (LogFile.available()) { // read bytes into buffer
for (int i = 0; i < 34; i ++){
SDinBuffer[i] = LogFile.read();
}
ADK.write (SDinBuffer, 34);
} // end while (LogFile.available())
LogFileSyncADK = true;
LogFile.close();
} // end if (LogFile)
break;
} // end case 'a':
case 'b':
LogFileSyncADK = false;
break;
case 'c':
SD.remove("sensor.log");
break;
}// end switch (ADK.read())
} // end if (ADK.available() > 0)
} // end if (acc.isConnected())
else{
ADKisConnected = false;
LogFileSyncADK = false;
}
}// end HandleADK()
 
Search WWH ::




Custom Search