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:


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