Sunday, March 31, 2013

Control Arduino Due LED from Android using ADK

In this exercise, we are going to implement a app on Android, it have two RadioButtons to control the LED on Arduino Due, by sending command via USB with ADK support.


Arduino code.
#include "variant.h"
#include <stdio.h>
#include <adk.h>
 
 
// Accessory descriptor. It's how Arduino identifies itself to Android.
char applicationName[] = "HelloADKLED"; // the app on your phone
char accessoryName[] = "Arduino Due"; // your Arduino board
char companyName[] = "Arduino-er";
 
// Make up anything you want for these
char versionNumber[] = "0.1";
char serialNumber[] = "1";
char url[] = "https://sites.google.com/site/arduinosite/exercise/androidadkled/AndroidADKLED_0.1.apk";
 
USBHost Usb;
ADK adk(&Usb, companyName, applicationName, accessoryName,versionNumber,url,serialNumber);
 
// Pin 13 has an LED connected on most Arduino boards.
int led = 13;
 
void setup() {
    Serial.begin(9600);
    cpu_irq_enable();
   
    pinMode(led, OUTPUT);
    //Indicate start of program
    digitalWrite(led, LOW);
    delay(2000);
    digitalWrite(led, HIGH);
    for(int i = 0; i <= 2; i++){
        digitalWrite(led, HIGH);
        delay(250);
        digitalWrite(led, LOW);
        delay(250);
    }
}
 
#define RCVSIZE 128
 
void loop() {
   
    char helloworld[] = "Hello World!\r\n";
   
    uint8_t buf[RCVSIZE];
    uint32_t nbread = 0;
   
    Usb.Task();
     
    if (adk.isReady()){
      
        adk.read(&nbread, RCVSIZE, buf);
        if (nbread > 0){
            adk.write(nbread, buf);
            
            //Convert nbread to String
            String s = "";
            for (uint32_t i = 0; i < nbread; ++i) {
              s += (char)buf[i];
            }
            
            if(s == "LEDON"){
              digitalWrite(led, HIGH);
            }else if(s == "LEDOFF"){
              digitalWrite(led, LOW);
            }
        }
         
    }
     
}

Download the source code for Arduino.

The Android code refer to the next post.


Updated@2015-10-15: Android control Arduino Due LED, using ADK (Accessory Development Kit)

14 comments:

  1. Will it work on a Arduíno uno? Where CA i get the libraries for variante.h and usbhost? Thanks

    ReplyDelete
    Replies
    1. hello,

      The IDE I used is Arduino 1.5.2 from Arduino site (http://arduino.cc/en/Main/Software), the libraries for variante.h and usbhost are already included.

      But I think it cannot work on uno directly! When I tried to change the Tools->Board to Arduino Uno, errors of variant.h, USBHost, ADK... are reported.

      Delete
    2. I had try to use Arduino 1.5.2 and your sketch but return many errors most on library variant.h.
      Why? Thanks

      Delete
    3. Can I know your setup? What error returned?

      Did you select Tools->Board->Arduino Due (Programming Port) in Arduino software?

      I test on Ubuntu.

      Delete
    4. My setup is :
      - Arduino 1.5.2
      - Your Sketch get on this page
      - Arduino Mega 2560 ADK

      After paste this code and compiling as Arduino Due(Programming Port), this is the errors :

      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.cpp:248: error: too many initializers for 'const PinDescription'
      -/Users/mino/Documents/Arduino/libraries/adk2/variant.cpp:233: error: 'NO_PWM' was not declared in this scope
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.cpp:233: error: 'NO_TC' was not declared in this scope
      Users/mino/Documents/Arduino/libraries/adk2/variant.cpp:19:
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:92: error: redefinition of 'const uint8_t SS'
      -/Users/xxx/Downloads/Arduino 2.app/Contents/Resources/Java/hardware/arduino/sam/variants/arduino_due_x/variant.h:105: error: 'const uint8_t SS' previously defined here
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:93: error: redefinition of 'const uint8_t MOSI'
      -/Users/xxx/Downloads/Arduino 2.app/Contents/Resources/Java/hardware/arduino/sam/variants/arduino_due_x/variant.h:109: error: 'const uint8_t MOSI' previously defined here
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:94: error: redefinition of 'const uint8_t MISO'
      -/Users/xxx/Downloads/Arduino 2.app/Contents/Resources/Java/hardware/arduino/sam/variants/arduino_due_x/variant.h:110: error: 'const uint8_t MISO' previously defined here
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:95: error: redefinition of 'const uint8_t SCK'
      /Users/xxx/Downloads/Arduino 2.app/Contents/Resources/Java/hardware/arduino/sam/variants/arduino_due_x/variant.h:111: error: 'const uint8_t SCK' previously defined here
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:133: error: redefinition of 'const uint8_t A0'
      /Users/xxx/Downloads/Arduino 2.app/Contents/Resources/Java/hardware/arduino/sam/variants/arduino_due_x/variant.h:150: error: 'const uint8_t A0' previously defined here
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:134: error: redefinition of 'const uint8_t A1'

      and many others but similar.

      Delete
    5. This exercise run on Arduino Due, I haven't Mega 2560 to test it.

      Sorry!

      Delete
    6. hello, how did you connect arduino with htc oneX? they are all micro-usb connector

      Delete
    7. Hello, How did you connected with HTC OneX & Arduino Duo??
      Dual Micro USB connector ??

      Thanks

      Delete
    8. http://arduino-er.blogspot.com/2013/03/cabling-to-connect-arduino-due-with.html

      Delete
  2. hi,
    i have arduino DUE and i want to show my work in my phone.
    can you help me and you tell me about the steps for do that? what i put in my phone ? please help me.

    ReplyDelete
    Replies
    1. hello,
      what you means "show my works in my phone"?

      Delete
    2. I'm working with Arduino DUE for signal decomposition to find the harmonics and show the signal and the spectrum of this signal and I used the software processing before for display, but this time I want to show this work in the phone (galaxy) or tablet,i mean (phone+processing+arduino due).

      Delete
    3. Great project!

      may be you can read here: http://arduino-er.blogspot.com/2013/04/read-analog-input-at-arduino-due.html

      wish you success:)

      Delete
    4. hi again , I've found the solution to this problem, but I have another problem appeared my i conneced my arduino in my phone with USB host but he tell me that he doesn't accept.why ?? and what is the adk ? need some help

      Delete