Introduction: ANDROID+ARDUINOADK+RGB Led

this is a project which makes an android app, then configure it to use it with android debug bridge or android accessory development kit.
for making the android app we use android mode of processing.
For hardware, i always choose arduino. here i have a Freeduino ADK from embedded market. for the communication since ADK is not that well developed for processing yet i am using ADB, which works for android OS as low as 1.6(i think).
also , i used a program that is a slider color chooser and then the same color goes to the rgb led.
the hardware can also be an arduino board with a host shield(i havent tried it, i just have a freeduino adk but it should be compatible)
we will start with making an android app on processing, then making it ready for adb, then make a compatible code for hardware and then sit back and enjoy the coffee.

Step 1: APP Making

http://www.processing.org/learning/android/
this site does all the work for me, but keep in mind, the android version of your device and API 10 must be installed otherwise you are gonna have error my friend.
before, install the latest processing version if you don't have it. i am using 2.0b7 but 8 should not be a problem.
make the simple program, run it, then run it on your device and see that it sticks!

Step 2: The Processing App

the code is based on three scroll bars and their position in x axis coordinate to an value of color. this color is displayed on the oval window next to them.
also, start processing in java mode. only then you can see the output in java.

here is the code:

HScrollbar hs1, hs2,hs3;        //initialise the scrollbar
PFont f;
void setup()
{
  size(800, 480);    //describe the size of the window
  f = loadFont("ComicSansMS-Bold-48.vlw");    //create font. just for show
  noStroke();
  int x1=height/4;
  hs1 = new HScrollbar(0, x1+30, width-220, 16, 16);
  hs2 = new HScrollbar(0, 2*x1+30, width-220, 16, 16);
  hs3 = new HScrollbar(0, 3*x1+30, width-220, 16, 16);
}

void draw() {
  background(255);
  textFont(f);
  fill(63,232,34);
  text("RGB Color Selector",5,80);
  fill(0);
  float img1Pos = hs1.getPos();
  fill(255,0,0);
  float img2Pos = hs2.getPos();
  fill(0,255,0);
  float img3Pos = hs3.getPos();
  fill(0,0,255);
  float r=map(img1Pos, 0,width-200,0,255);
  float g=map(img2Pos, 0,width-200,0,255);
  float b=map(img3Pos, 0,width-200,0,255);
  int rr,gg,bb;
  rr=int(r);
  gg=int(g);
  bb=int(b);
  hs1.update();    //to get current location
  hs2.update();
  hs3.update();
  hs3.display();
  hs1.display();
  hs2.display();
  fill(rr,gg,bb);
  strokeWeight(2);
  smooth(); 
  ellipse(700, 250, 190, 190);
  stroke(0);
}
class HScrollbar
{
  int swidth, sheight;   
  float xpos, ypos;      
  float spos, newspos;   
  float sposMin, sposMax;
  int loose;             
  boolean over;          
  boolean locked;
  float ratio;

  HScrollbar (float xp, float yp, int sw, int sh, int l)
  {
    swidth = sw;
    sheight = sh;
    int widthtoheight = sw - sh;
    ratio = (float)sw / (float)widthtoheight;
    xpos = xp;
    ypos = yp-sheight/2;
    spos = xpos + swidth/2 - sheight/2;
    newspos = spos;
    sposMin = xpos;
    sposMax = xpos + swidth - sheight;
    loose = l;
  }

  void update()
  {
    if(overEvent()) {
      over = true;
    } else {
      over = false;
    }
    if(mousePressed && over) {
      locked = true;
    }
    if(!mousePressed) {
      locked = false;
    }
    if(locked) {
      newspos = constrain(mouseX-sheight/2, sposMin, sposMax);
    }
    if(abs(newspos - spos) > 1) {
      spos = spos + (newspos-spos)/loose;
    }
  }

  float constrain(float val, float minv, float maxv)
  {
    return min(max(val, minv), maxv);
  }

  boolean overEvent()
  {   
    if(mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  void display()
  {
    noStroke();
    fill(204);
    rect(xpos, ypos, swidth, sheight);
    if(over || locked)
    {
      fill(0, 0, 0);
    }
    else
    {
      fill(102, 102, 102);
    }
    rect(spos, ypos, sheight, sheight);
  }

  float getPos()
  {
    return spos * ratio;
  }
}

Step 3: Starting the Sketch on Mobile

save the sketch, open processing in android mode. then press run on device and connect your android devic with usb debugging on.

Step 4: Prepping the Code for Adb Communication

the initial adb sketch was from the example of romfont library. there the code needed some changes so that we can successfully port it to other programs.
here is the changed code and the places where the new code is to be added

for Processing unzip the code and read it thoroughly

the final code for the previous program is-(only the main file, library has no change)

import java.io.IOException;
import android.util.Log;
private Server server;
private int sensorValue;
HScrollbar hs1, hs2,hs3;

void setup()

  // Create TCP server
  size(800, 480);
  orientation(LANDSCAPE);

  noStroke();
  int x1=height/4;
  hs1 = new HScrollbar(0, x1+30, width-220, 16, 16);
  hs2 = new HScrollbar(0, 2*x1+30, width-220, 16, 16);
  hs3 = new HScrollbar(0, 3*x1+30, width-220, 16, 16);

  server = null;
  try
  {
    server = new Server(4567);
    server.start();
  }
  catch (IOException e)
  {
    println(e.toString());
  }

  this.server.addListener(new AbstractServerListener()
  {
    @Override
      public void onReceive(Client client, byte[] data)
      {
      if (data.length<3) return;
      sensorValue = (data[0] & 0xff) |(data[1] & 0xff) | ((data[2] & 0xff) << 8);           // See the change
      };
  }
  );
}

void draw()
{
  background(255);
  fill(63,232,34);
  text("RGB Color Selector Arduino",5,80);
  float img1Pos = hs1.getPos();
  float img2Pos = hs2.getPos();
  float img3Pos = hs3.getPos();
  float r=map(img1Pos, 0,width-200,0,255);
  float g=map(img2Pos, 0,width-200,0,255);
  float b=map(img3Pos, 0,width-200,0,255);
  int rr,gg,bb;
  rr=int(r);
  gg=int(g);
  bb=int(b);
  hs1.update();
  hs2.update();
  hs3.update();
  hs3.display();
  hs1.display();
  hs2.display();
  fill(rr,gg,bb);
  strokeWeight(2);
  smooth(); 
  ellipse(700, 250, 190, 190);
  stroke(0);
     try
     {
      server.send(new byte[] {(byte) (rr),(byte) (gg), (byte) ((bb))});          //See the change
     }
     catch (IOException e)
     {
      println("microbridgeproblem sending TCP message");
    }
}

class HScrollbar
{
  int swidth, sheight;   
  float xpos, ypos;      
  float spos, newspos;   
  float sposMin, sposMax;
  int loose;             
  boolean over;          
  boolean locked;
  float ratio;

  HScrollbar (float xp, float yp, int sw, int sh, int l)
  {
    swidth = sw;
    sheight = sh;
    int widthtoheight = sw - sh;
    ratio = (float)sw / (float)widthtoheight;
    xpos = xp;
    ypos = yp-sheight/2;
    spos = xpos + swidth/2 - sheight/2;
    newspos = spos;
    sposMin = xpos;
    sposMax = xpos + swidth - sheight;
    loose = l;
  }

  void update()
  {
    if(overEvent()) {
      over = true;
    } else {
      over = false;
    }
    if(mousePressed && over) {
      locked = true;
    }
    if(!mousePressed) {
      locked = false;
    }
    if(locked) {
      newspos = constrain(mouseX-sheight/2, sposMin, sposMax);
    }
    if(abs(newspos - spos) > 1) {
      spos = spos + (newspos-spos)/loose;
    }
  }

  float constrain(float val, float minv, float maxv)
  {
    return min(max(val, minv), maxv);
  }

  boolean overEvent()
  {   
    if(mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  void display()
  {
    noStroke();
    fill(204);
    rect(xpos, ypos, swidth, sheight);
    if(over || locked)
    {
      fill(0, 0, 0);
    }
    else
    {
      fill(102, 102, 102);
    }
    rect(spos, ypos, sheight, sheight);
  }

  float getPos()
  {
    return spos * ratio;
  }

Step 5: The Code in Arduino

it is important to first download the adb library. use the arduino version 22.
here is the library
here again the base code is from romfont. he has saved it in a .ino file so i am giving the code in .pde fiole with all the editing locations

here is the final code for the same program. red part connected to 11, green to 12 and blue to 13. you can change them according to the pwm pins on your arduino

here is the code:

#include <SPI.h>
#include <Adb.h>
#define PIN_MAX_GPX 7
#define PIN_MAX_RESET 8
Connection *connection;
void adbEventHandler(Connection *connection, adb_eventType event, uint16_t length, uint8_t * data)
{
  int i;
  if (event == ADB_CONNECTION_RECEIVE)
  {
    analogWrite(11,data[0]);//printing the data to rgb led
    analogWrite(12,data[1]);
    analogWrite(13,data[2]);
      } else {

    Serial.println("Error");
  }
}

void setup()
{
  Serial.begin(9600);
  ADB::init();
  connection = ADB::addConnection("tcp:4567", true, adbEventHandler); 
}

void loop()
{
  ADB::poll();
}

Step 6: Connect the Setup and Final Preview!

Comments

author
nana kesewaaD made it! (author)2016-07-29

How do I do this with an Arduino Uno? Any suggestions

author
manos.dakos.1224 made it! (author)2015-01-24

hey there can anyone help how to make processing to open it on anrdoid mode i press on the corner "the mode button" but when i searched i couldn't fond the android mode and also the ADB i'm writting it in the android programming don't I???

author
Jan_Henrik made it! (author)2014-05-06

Very cool!

author
minaa made it! (author)2014-04-05

i would an android project based on a card arduino shield ethernet module for displaying an analog input such as dimming by a ldr sensor with two LEDs

author
mechrobo made it! (author)2013-05-15

HI i used this code on processing but it displayed error on third line "private Server server;" . Do i need to add any library in processing .

About This Instructable

13,109views

28favorites

License:

Bio: just a med student with an open mind..
More by raghvmahashabde:ANDROID+ARDUINOADK+RGB ledarduino+keypad+servo control
Add instructable to: