Wednesday, November 28, 2012

Android & Arduino: LEDS control

Hi all,
last week I have bought an OTG cable so I can interface my Android devices (Galaxy Nexus, Xoom) with the Arduino board.
My example is to turn on and off the LEDs located on Arduino by an Android device!
What we need:

  1. Arduino Uno board
  2. Android devices (version 3.0 or higher)
  3. Micro USB OTG to USB Adapter (thanks to Amazon!)
  4. Arduino Uno Communicator (avaiable on playstore)
  5. 3x leds
  6. 3x resistors
  7. 4x cable connection strips
  8. Breadboard
So make the connection as shown in the picture:


Now we have to load this code in Arduino:
/*
 * Arduino LEDs Control
 * 
 */

int val = 0;       // variable to store the data from the serial port
int ledPin1 = 2;   // LED connected to digital pin 2
int ledPin2 = 3;   // LED connected to digital pin 3
int ledPin3 = 4;   // LED connected to digital pin 4
int ledPin4 = 5;   // LED connected to digital pin 5
int ledPin5 = 6;   // LED connected to digital pin 6

void setup() {

  pinMode(ledPin1,OUTPUT);    // declare the LED's pin as output
  pinMode(ledPin2,OUTPUT);    // declare the LED's pin as output
  pinMode(ledPin3,OUTPUT);    // declare the LED's pin as output
  pinMode(ledPin4,OUTPUT);    // declare the LED's pin as output
  pinMode(ledPin5,OUTPUT);    // declare the LED's pin as output


  
  Serial.begin(9600);        // connect to the serial port
}

void loop () {
  val = Serial.read();      // read the serial port

  
  if (val !=-1){

    if (val=='1'){
      digitalWrite(ledPin1,HIGH);
    }
    
    else if (val=='A'){
      digitalWrite(ledPin1,LOW);
      }
    
    if (val=='2'){
      digitalWrite(ledPin2,HIGH);
    }

    else if (val=='B'){
      digitalWrite(ledPin2,LOW);
      }

    if (val=='3'){
      digitalWrite(ledPin3,HIGH);
    }

    else if (val=='C'){
      digitalWrite(ledPin3,LOW);
      }

    if (val=='4'){
      digitalWrite(ledPin4,HIGH);
    }

    else if (val=='D'){
      digitalWrite(ledPin4,LOW);
      }

    if (val=='5'){
      digitalWrite(ledPin5,HIGH);
    }

    else if (val=='E'){
      digitalWrite(ledPin5,LOW);
      }
   
    //Serial.println();
  }
}

This code is really easy to understand: if you get the string "1" turns ON the LED on pin 2, if you get the string "2" turns ON the LED on pin 3 ... and so on. If you get the string "A" turns OFF the LED on pin 2, if you get the string "B" turns OFF the LED on pin 3... and so on.

And this is the code for the Android app:

package com.spinettaro.arduinoledscontrol;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends Activity implements OnCheckedChangeListener {

 // set in an array the id of every switch
 private final static Integer[] ids = { R.id.switch1, R.id.switch2,
   R.id.switch3, R.id.switch4, R.id.switch5 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  registerLedChangeListener(ids);
 }

 //register all onCheckedChangeListener for all Switch
 private void registerLedChangeListener(Integer... ids) {
  for (int i = 0; i < ids.length; i++) {
   Integer id = ids[i];
   Switch led = (Switch) findViewById(id);
   led.setOnCheckedChangeListener(this);
  }
 }

 public void loop(View v) {
  try {
   // gone
   sendString("1");
   Thread.sleep(50);
   sendString("A");
   Thread.sleep(50);
   sendString("2");
   Thread.sleep(50);
   sendString("B");
   Thread.sleep(50);
   sendString("3");
   Thread.sleep(50);
   sendString("C");
   Thread.sleep(50);

   // return
   sendString("3");
   Thread.sleep(50);
   sendString("C");
   Thread.sleep(50);
   sendString("2");
   Thread.sleep(50);
   sendString("B");
   Thread.sleep(50);
   sendString("1");
   Thread.sleep(50);
   sendString("A");
   Thread.sleep(50);
  } catch (Exception e) {
   Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  switch (buttonView.getId()) {
  case R.id.switch1:
   if (isChecked)
    sendString("1");
   else
    sendString("A");
   return;
  case R.id.switch2:
   if (isChecked)
    sendString("2");
   else
    sendString("B");
   break;
  case R.id.switch3:
   if (isChecked)
    sendString("3");
   else
    sendString("C");
   break;
  default:
   break;
  }
 }

 private void sendString(String toSend) {
  Intent i = new Intent("primavera.arduino.intent.action.SEND_DATA");
  i.putExtra("primavera.arduino.intent.extra.DATA", toSend.getBytes());
  sendBroadcast(i);
 }

}

Here there's the WOW effect! Because thanks to the IPC Android system I can communicate with Arduino through the use of broadcast intent, because the part of interfacing with the ADK was handled by Arduino Uno Communicator. To send data to Arduino from your application it's only necessary broadcasting an intent with action "primavera.arduino.intent.action.SEND_DATA". Add the data to be sent as byte array extra "primavera.arduino.intent.extra.DATA".


And this is the video of the demo:

video

Here you can find the full source code for Android application.

10 comments :

  1. hello!

    What is the program used to create the android application?
    This ap is created uner windows or under android?

    Thanks

    ReplyDelete
    Replies
    1. This app was created under Windows, using Eclipse IDE with ADT. If you need anything else let me know... :)

      Delete
  2. hi
    nice work, can you send me full android source(with xml file)?

    novendeni@gmail.com
    thank's

    ReplyDelete
    Replies
    1. Hi yo_gie, I just updated the post with the link to download the code for Android. This is the link: http://www.mediafire.com/download.php?w7aao02b45l212h

      Delete
  3. Hi,

    Can you show me how do I add TCP/IP coding in Android. I need Android to be the client and arduino+Xbee WiFi as Server to received command.

    ReplyDelete
  4. thanks for your share :) but your also uploaded android code is doesnt open. your file crashed or die. do you check for me ?

    ReplyDelete
  5. Do you have instructions on how to receive characters that the Arduino Uno Communicator app has gotten from the Arduino within my own app?

    ReplyDelete
    Replies
    1. You can use a Broadcast Receiver: " Let your own Android application receive data from Arduino by listening to the "primavera.arduino.intent.action.DATA_RECEIVED" intent. This intent will contain the "primavera.arduino.intent.extra.DATA" byte array with the received data. Call getByteArrayExtra("primavera.arduino.intent.extra.DATA") to retreive the data ". Text taken from the official page project https://github.com/jeppsson/Arduino-Communicator

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. you can integrate the usb host comunication cod in your app.. just see the arduino uno comunicator for full code example. in this way you have not the needing to use an external app to use your one ;-)

    ReplyDelete