You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

ArduinoBlinkLED.ino 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // The source for the Android application can be found at the following link: https://github.com/Lauszus/ArduinoBlinkLED
  2. // 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
  3. #include <adk.h>
  4. //
  5. // CAUTION! WARNING! ATTENTION! VORSICHT! ADVARSEL! ¡CUIDADO! ВНИМАНИЕ!
  6. //
  7. // Pin 13 is occupied by the SCK pin on various Arduino boards,
  8. // including Uno, Duemilanove, etc., so use a different pin for those boards.
  9. //
  10. // CAUTION! WARNING! ATTENTION! VORSICHT! ADVARSEL! ¡CUIDADO! ВНИМАНИЕ!
  11. //
  12. #if defined(LED_BUILTIN)
  13. #define LED LED_BUILTIN // Use built in LED
  14. #else
  15. #define LED 9 // Set to something here that makes sense for your board.
  16. #endif
  17. // Satisfy IDE, which only needs to see the include statment in the ino.
  18. #ifdef dobogusinclude
  19. #include <spi4teensy3.h>
  20. #include <SPI.h>
  21. #endif
  22. USB Usb;
  23. ADK adk(&Usb, "TKJElectronics", // Manufacturer Name
  24. "ArduinoBlinkLED", // Model Name
  25. "Example sketch for the USB Host Shield", // Description (user-visible string)
  26. "1.0", // Version
  27. "http://www.tkjelectronics.dk/uploads/ArduinoBlinkLED.apk", // URL (web page to visit if no installed apps support the accessory)
  28. "123456789"); // Serial Number (optional)
  29. uint32_t timer;
  30. bool connected;
  31. void setup() {
  32. Serial.begin(115200);
  33. #if !defined(__MIPSEL__)
  34. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  35. #endif
  36. if (Usb.Init() == -1) {
  37. Serial.print("\r\nOSCOKIRQ failed to assert");
  38. while (1); // halt
  39. }
  40. pinMode(LED, OUTPUT);
  41. Serial.print("\r\nArduino Blink LED Started");
  42. }
  43. void loop() {
  44. Usb.Task();
  45. if (adk.isReady()) {
  46. if (!connected) {
  47. connected = true;
  48. Serial.print(F("\r\nConnected to accessory"));
  49. }
  50. uint8_t msg[1];
  51. uint16_t len = sizeof(msg);
  52. uint8_t rcode = adk.RcvData(&len, msg);
  53. if (rcode && rcode != hrNAK) {
  54. Serial.print(F("\r\nData rcv: "));
  55. Serial.print(rcode, HEX);
  56. } else if (len > 0) {
  57. Serial.print(F("\r\nData Packet: "));
  58. Serial.print(msg[0]);
  59. digitalWrite(LED, msg[0] ? HIGH : LOW);
  60. }
  61. if (millis() - timer >= 1000) { // Send data every 1s
  62. timer = millis();
  63. rcode = adk.SndData(sizeof(timer), (uint8_t*)&timer);
  64. if (rcode && rcode != hrNAK) {
  65. Serial.print(F("\r\nData send: "));
  66. Serial.print(rcode, HEX);
  67. } else if (rcode != hrNAK) {
  68. Serial.print(F("\r\nTimer: "));
  69. Serial.print(timer);
  70. }
  71. }
  72. } else {
  73. if (connected) {
  74. connected = false;
  75. Serial.print(F("\r\nDisconnected from accessory"));
  76. digitalWrite(LED, LOW);
  77. }
  78. }
  79. }