(HELP) Controlling rectangles with Arduino
in
Integration and Hardware
•
1 year ago
Hello,
I am a beginner with Porcessing and don't understand really well the vocabulary and the process to make the things into it.
I need to control rectangles through processing using Arduino's interface, with a triple axis accelerometer. I want to be able to take a rectangle, move it using the accelerometer coordinates, and let it where it is, then, take another one, move it, and let it where it is, and so on.
I also need to assign a different color to each square.
But now I'm a bit lost in this code.
Can somebody help me?
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
//coords
int cordx;
int cordy;
int cordz;
//floats_easing
float x = cordx;
float y = cordy;
float z = cordz;
float targetX, targetY, targetZ;
float easing = 0.1;
class Rectangle {
color c,v,g,f;
float x, y, w, h;
Rectangle( float _x, float _y, float _w, float _h ) {
x = _x;
y = _y;
w = _w;
h = _h;
c = color(240);
v = color(0,0,120);
g = color(250,210,0);
h = color(200,0,0);
}
void draw() {
pushStyle();
fill(c);
noStroke();
rect(x, y, w, h);
popStyle();
}
void setColor(color _c) {
c = _c;
}
void adjustPosition(float _x, float _y ) {
x = x + _x;
y = y + _y;
}
}
int active;
Rectangle r1, r2, r3, r4;
void setup()
{
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[1], 57600);
size(1420,820);
noStroke();
smooth();
r1 = new Rectangle( 56, 42, 293, 61 );
r2 = new Rectangle( 485, 42, 211, 61 );
r3 = new Rectangle( 358, 281, 119, 66 );
r4 = new Rectangle( 582, 355, 63, 118 );
active = 0;
}
void draw(){
background(0);
translate(300,500);
r1.draw();
r2.draw();
r3.draw();
r4.draw();
cordx = arduino.analogRead(1);
cordy = arduino.analogRead(2);
cordz = arduino.analogRead(3);
targetX = cordx;
float dx = targetX - x;
if(abs(dx)>1) {
x+= dx * easing;
}
targetY = cordy;
float dy= targetY - y;
if(abs(dy) >1) {
y += dy * easing;
}
targetZ = cordz;
float dz = targetZ - z;
if(abs(dz)>1) {
z+= dz * easing;
}
println("cordx=" +cordx);
println("cordy=" +cordy);
println("cordz=" +cordz);
if ( active == 1 ) {
r1.adjustPosition( x-cordx, y-cordy);
}
else if ( active == 2 ) {
r2.adjustPosition( x-cordx, y-cordy );
}
else if ( active == 3 ) {
r3.adjustPosition(x-cordx, y-cordy);
}
else if ( active == 4 ) {
r4.adjustPosition( x-cordx, y-cordy );
}
}
void keyPressed() {
if (key == 'a' || key == 'A') {
active = active==1?0:1;
}
if (key == 'b' || key == 'B') {
active = active==2?0:2;
}
if (key == 'c' || key == 'C') {
active = active==3?0:3;
}
if (key == 'd' || key == 'D') {
active = active==4?0:4;
}
}
1