USB_Host_Shield_2.0/examples/adk/ArduinoBlinkLED/ArduinoBlinkLED.ino

69 lines
2.3 KiB
Arduino
Raw Normal View History

2012-10-17 00:43:34 +02:00
// The source for the Android application can be found at the following link: https://github.com/Lauszus/ArduinoBlinkLED
// The code for the Android application is heavily based on this guide: http://allaboutee.com/2011/12/31/arduino-adk-board-blink-an-led-with-your-phone-code-and-explanation/ by Miguel
#include <adk.h>
USB Usb;
2013-10-30 15:21:50 +01:00
ADK adk(&Usb, "TKJElectronics", // Manufacturer Name
"ArduinoBlinkLED", // Model Name
"Example sketch for the USB Host Shield", // Description (user-visible string)
"1.0", // Version
"http://www.tkjelectronics.dk/uploads/ArduinoBlinkLED.apk", // URL (web page to visit if no installed apps support the accessory)
"123456789"); // Serial Number (optional)
2013-11-15 23:20:15 +01:00
#define LED LED_BUILTIN // Use built in LED - note that pin 13 is occupied by the SCK pin on a normal Arduino (Uno, Duemilanove etc.), so use a different pin
uint32_t timer;
boolean connected;
2013-10-30 15:21:50 +01:00
void setup() {
Serial.begin(115200);
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
if (Usb.Init() == -1) {
Serial.print("\r\nOSCOKIRQ failed to assert");
2013-10-30 15:21:50 +01:00
while (1); // halt
}
2012-04-11 01:48:46 +02:00
pinMode(LED, OUTPUT);
Serial.print("\r\nArduino Blink LED Started");
}
2013-10-30 15:21:50 +01:00
void loop() {
Usb.Task();
2013-10-30 15:21:50 +01:00
if (adk.isReady()) {
if (!connected) {
connected = true;
Serial.print(F("\r\nConnected to accessory"));
}
uint8_t msg[1];
uint16_t len = sizeof(msg);
uint8_t rcode = adk.RcvData(&len, msg);
if (rcode && rcode != hrNAK) {
Serial.print(F("\r\nData rcv: "));
Serial.print(rcode, HEX);
} else if (len > 0) {
Serial.print(F("\r\nData Packet: "));
Serial.print(msg[0]);
2013-10-30 15:21:50 +01:00
digitalWrite(LED, msg[0] ? HIGH : LOW);
}
if (millis() - timer >= 1000) { // Send data every 1s
timer = millis();
rcode = adk.SndData(sizeof(timer), (uint8_t*)&timer);
if (rcode && rcode != hrNAK) {
Serial.print(F("\r\nData send: "));
Serial.print(rcode, HEX);
} else if (rcode != hrNAK) {
Serial.print(F("\r\nTimer: "));
Serial.print(timer);
}
}
} else {
if (connected) {
connected = false;
Serial.print(F("\r\nDisconnected from accessory"));
digitalWrite(LED, LOW);
}
2013-10-30 15:21:50 +01:00
}
}