import processing.opengl.*;
import processing.serial.*;
//Sets variables for serial port, bytes, and floats
Serial sp;
byte[] buff;
float[] r;
float OFFSET_X = 2.5, OFFSET_Y = -2.5; //These offsets are chip specific, and vary. Play with them to get the best ones for you
//This sets up the Processing 3 Java window dimension and visuals
void setup() {
size(500, 400, P3D);
sp = new Serial(this, "/dev/cu.usbserial-A50285BI", 115200); //You have to rename your port in the "" and change the baud rate.
buff = new byte[512];
r = new float[4];
}
float protz, protx;
void draw() {
//perspective( 45, 4.0/3.0, 1, 5000 );
translate(400/2, 300/2, -400); //Sets left/right, up/down, and toward/away translation
background(0); //Sets background color to black
buildShape(protz, protx); //Used to define a new shape
int bytes = sp.readBytesUntil((byte)15, buff); //It will only read until 15 bytes of data including 3 spaces
String mystr = (new String(buff, 0, bytes)).trim(); //trim removes whitespace at start of string
if(mystr.split(" ").length != 4) { //uses string to break characters by using a delimiter ""
println(mystr);
return;
}
setVals(r, mystr);
float z = r[0], x = r[1];
if(abs(protz - r[0]) < 0.05)
z = protz;
if(abs(protx - r[1]) < 0.05)
x = protx;
background(0);
buildShape(z, x);
protz = z;
protx = x;
println(r[0] + ", " + r[1] + ", " + r[2]); //prints x, y, z values in the console
}
//This sets up the 3D object inside the window
void buildShape(float rotz, float rotx) {
pushMatrix();
scale(6,6,14);
rotateZ(rotz);
rotateX(rotx);
fill(255);
stroke(0);
box(60, 10, 10);
fill(0, 255, 0);
box(10, 9, 40);
translate(0, -10, 20);
fill(255, 0, 0);
box(5, 12, 10);
popMatrix();
}
//Sets the values in an array
void setVals(float[] r, String s) {
int i = 0;
r[0] = -(float)(Integer.parseInt(s.substring(0, i = s.indexOf(" "))) +OFFSET_X)*HALF_PI/256;
r[1] = -(float)(Integer.parseInt(s.substring(i+1, i = s.indexOf(" ", i+1))) + OFFSET_Y)*HALF_PI/256;
r[2] = (float) Integer.parseInt(s.substring(i+1));
}
I’m struggling to get a 3d view of accelerometer data that i’m sending from Arduino IDE serial :
void sendXYZ(
sprintf(str, "%d %d %d ", x, y, z);
Serial.print(str);
}
On serial, i’m getting below data ::
3889 4062 30163890 4070 30253911 4081 30163896 4082 30303912 4083 30333907 4087 30323901 4079 30253905 4058 30343907 4094 30163911 4069 30293903 4055 30263899 4065 29873885 4083 30443915 4070 30303889 4076 30383900 4076 30333906 4082 30373896 4067 30343896 4080
My data is between 0-4096 for each axis as using 3.3v 12-bit ADC, esp32
can someone, help, actually I got the above processing code for 3-byte data, that comes from Adxl335 for 10-bit Arduino, but my accelerometer is STk8231 with esp32, which gives 12 bytes of data like above, so I tried to modify it, the total byte in the original code was 10 byte for adxl335 but for STK8231, we can see total byte is 4*3 = 12 + 2 spaces, so 15 byte I change, then I also changed in space elimination ( in line no. 28 ), length was for each byte was 3 in adxl335 with Arduino (10 bit ) but in STK with my 3.3 microcontroller reading was 4 byte ., don’t know if any changes required to get 3d view, that i’m not getting , ofcourse port, baud rate etc are correct.