WiTilt v2.5 is a 2.4GHz Wireless 3-axis Tilt Sensor from Sparkfun

無標題

WiTilt v2.5 is a 2.4GHz Wireless 3-axis Tilt Sensor from Sparkfun

You can have more technology information from here.

In this article I just talk about how to use it with processing.

First~

Pair it!

 

無標題

 

chose the Set Up Bluetooth Device

無標題

you will see a lot of different devices, chose “BlueRadios

無標題

Than you will see this information!!!!!!!DON’T BE DESPAIR! That’s normal!

Chose the “Passcode Options

無標題

Chose don’t use passcode this time.

YOU GOT IT!

無標題

 

Now Open the CoolTerm or any other Software.

Open the option and change it to BlueRadios!
無標題

Now you got it!
無標題

I chose one~~than i got the value from X, Y, Z

There is a lot of different to come out the value, and you can change it. Here is the Datasheet.

 

無標題

 

Here is the last important thing.

Every time when you turn on and off you have to PRESS THE RESTART!!!!!!!!

if you dont do that, you can’t go back to main menue!

RESET

Now after that.

you just need serial communication to the processing.

And you can play with your ball now!!!啾咪!

Here is the code.

 

/**
* Esfera
* by David Pena.
*
* Frame Differencing
* by Golan Levin.
*
Delay
by Maria Paula Saba
* Distribucion aleatoria uniforme sobre la superficie de una esfera.
*/
//float MX=mouseX;
import processing.video.*;

//—–serial
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
float xpos;
float ypos;
float zpos;
int xValue=0, yValue=0, zValue=0;

//the second xpos value
//———–
int numPixels;
int[] previousFrame;
//this is for capture background
int[] backgroundPixels;

float movementX;

int cuantos = 1600;
pelo[] lista ;
float[] z = new float[cuantos];
float[] phi = new float[cuantos];
float[] largos = new float[cuantos];
float radio = 200;
float rx = 0;
float ry =0;
PImage img;

Capture video;
boolean showObject = false;

void setup() {
size(1280, 720, P3D);
video = new Capture(this, width, height);
//,”Logitech Camera”,30);
video.start();

//img = loadImage(“tree.jpg”);
noSmooth();
frameRate(120);
//float r=map(mouseX,0,width,1.5,10);
//println(r);
radio = height/6;
//——————–serial communication
println(Serial.list());
String portName = Serial.list()[6];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil(‘\n’);
//——————-
lista = new pelo[cuantos];
for (int i=0; i<cuantos; i++) {
lista[i] = new pelo();
}
noiseDetail(3);
numPixels = video.width * video.height;
// Create an array to store the previously captured frame
previousFrame = new int[numPixels];
backgroundPixels=new int[numPixels];

loadPixels();
}

void draw() {
//background(255);
//xpos=mouseX;
/*for (int i = 0; i <= 10; i++) {

float Lxpos=lerp(xpos,Axpos,i/10.00);
float Lypos=lerp(ypos,Aypos,i/10.00);
float Lzpos=lerp(zpos,Azpos,i/10.00);
}*/

movementX=(zpos/2);

int s=(second()/5);
//if (s==2||s==4||s==6||s==7||s==9||s==11) {
video.read();
//}
video.loadPixels();

//image(img, 0, 0);
//tint(255,0,255,40);
//noCursor();
//tint(mouseX,mouseY,0,50);
//image(video,0,0);
//————————
int movementSum = 0; // Amount of movement in the frame
int difference=0; // Amount of movement in the background

for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame…
color currColor = video.pixels[i];
color prevColor = previousFrame[i];
// Extract the red, green, and blue components from current pixel
int currR = (currColor >> 16) & 0xFF; // Like red(), but faster
int currG = (currColor >> 8) & 0xFF;
int currB = currColor & 0xFF;
// Extract red, green, and blue components from previous pixel
int prevR = (prevColor >> 16) & 0xFF;
int prevG = (prevColor >> 8) & 0xFF;
int prevB = prevColor & 0xFF;
// Compute the difference of the red, green, and blue values
int diffR = abs(currR – prevR);
int diffG = abs(currG – prevG);
int diffB = abs(currB – prevB);
// Add these differences to the running tally
difference = diffR + diffG + diffB;
movementSum = difference+1;
previousFrame[i] = currColor;
}
tint(255, 10);
//tint(xpos,ypos,zpos);
image(video, 0, 0);

translate(width/2, height/2);
//—————————–ball
//translate(width/2,height/2);

float rxp = ((xpos-(width/2))*0.01);
float ryp = ((ypos-(height/2))*0.01);
rx = (rx*0.9)+(rxp*0.1);
ry = (ry*0.9)+(ryp*0.1);
rotateY(rx);
rotateX(ry);
fill(0);
noStroke();
//sphere(radio,50);

//if(mousePressed){
//if(showObject){
for (int i=0;i<cuantos;i++) {
lista[i].dibujar();
}
//}

if (frameCount % 10 == 0) {
//println(frameRate);
}
//———————————————
}
//void mousePressed(){
//showObject = !showObject;
//}

 

class pelo
{
float z = random(-radio, radio);
float phi = random(TWO_PI);
float largo = random(1.15, 1.2);
float theta = asin(z/radio);

void dibujar() {

float off = (noise(millis() * 0.0005, sin(phi))-0.5) * 0.3;
float offb = (noise(millis() * 0.0007, sin(z) * 0.01)-0.5) * 0.3;

float thetaff = theta+off;
float phff = phi+offb;
float x = radio * cos(theta) * cos(phi)+movementX;
float y = radio * cos(theta) * sin(phi)+movementX;
float z = radio * sin(theta);

float xo = radio * cos(thetaff) * cos(phff);
float yo = radio * cos(thetaff) * sin(phff);
float zo = radio * sin(thetaff);

float xb = xo * largo+movementX;
float yb = yo * largo+movementX;
float zb = zo * largo;

float sw=map(zpos, 0, width, 0, 5);
strokeWeight(sw);
beginShape(LINES);
stroke(0);
vertex(x, y, z);
stroke(200, 150);
vertex(xb, yb, zb);
endShape();
}
}

void serialEvent(Serial myPort) {

// read the serial buffer:
String myString = myPort.readStringUntil(‘\n’);
if (myString != null) {
println(myString);
}
// X=234 Y=393 Z=383
myString = trim(myString);
// X=234 Y=393 Z=383
String sensors[] = split(myString, ‘ ‘);
//sensors[0] = “X=234 ”
// sensors[1] = “Y=393 “;
// sensors[2] = “Z=383”;
sensors[0] = trim(sensors[0]);
sensors[1] = trim(sensors[1]);
sensors[2] = trim(sensors[2]);
//println(sensors);
for (int i = 0; i < sensors.length; i++){
int eachSensor[] = int(split(sensors[i], ‘=’));
// eachSensor[0] = X or Y or Z
// eachSensor[1] = the number we need!!!
println(eachSensor);

if (i==0){
// this is X value
xValue = eachSensor[1];
//println(xValue);
}
else if (i==1){
// this is X value
yValue = eachSensor[1];
//println(yValue);

}
else if (i==2){
// this is X value
zValue = eachSensor[1];
//println(zValue);
}
}
xpos = map(xValue, 0, 1023, 0, width);
ypos = map(yValue, 0, 1023, 0, height);
zpos= (zValue/2);

//xpos =lerp(xpos,sensors[0],.1);
//ypos =lerp(ypos,sensors[1],.1);
//zpos =lerp(zpos,sensors[2],.1);
}

Leave a Comment