星期六, 7月 03, 2010

Arduino + Processing



I made it to control my robotic arm originally. However, it's bad looking, so I used wii nunckuk instead.

##CONTINUE##
Yesterday, I was thinking how I can do to simply model hand motion and display in computer screen. I am not good at 3D painting, so I used RGB cube that I saw some people used it to demonstrate some programs. You can also find the processing code from the above link.

In this simple project, I used two potentiometers to control two axises. I am sorry that I don't have any idea to use the flex sensor, but I pasted it before, so I did not take it out.



Here is the code in arduino board:
int val_1;  //flex sensor
int val_2;  //middle potentiometer
int val_3;  //bottom potentiometer

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  val_1=analogRead(0);
  val_2=analogRead(1);
  val_3=analogRead(2);
  
  val_2=map(val_2,340,1023,0,360);
  val_3=map(val_3,0,1023,640,0);
  
  To2bytes(val_2);
  To2bytes(val_3);
  
  /*
  Serial.print(val_1);
  Serial.print("\t");
  Serial.print(val_2);
  Serial.print("\t");
  Serial.println(val_3);
  */
  delay(200);
}

void To2bytes(unsigned int inX){
  byte H_Byte;
  byte L_Byte;
 // int a=B01111111
  H_Byte=inX>>8;  
  L_Byte=(inX%256);
  Serial.print(H_Byte,BYTE);
  Serial.print(L_Byte,BYTE);
}

And I only add some codes in RGB cube code.
In void setup

println(Serial.list());// List all the available serial ports
  myPort = new Serial(this, Serial.list()[1], 9600); 
  
  //You have to import serial class before.
  //import processing.serial.*;
  //Serial myPort;  // The serial port

In void draw()

int[] inByte = new int[4];
  while (myPort.available() == 4) {  
    inByte[0]= myPort.read();
    inByte[1]= myPort.read();
    inByte[2]= myPort.read();
    inByte[3]= myPort.read();
    inY=(inByte[0]<<8)+inByte[1];
    inX=(inByte[2]<<8)+inByte[3];
     print("X" +inX);
     print("\t");
     println("Y" +inY);
  }