Accelerometer data STK8231 to 3d view

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.

Hi
You can use map to shift your reading

And you can use map in Arduino same way

The Arduino Uno ADC is of 10 bit resolution (so the integer values from (0-(2^10) 1023)). This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023.

Esp resolution of 12 bits , which means you can get values from 0 to 4095.

ADC in ESP32 have a maximum resolution of ADC of 12-bits and yes, the resolution of ADC is configurable with possible values include 9-bit, 10-bit, 11-bit and 12-bit. Usually, the resolution is set to 12-bit, if not changed

Google how to set Esp adc resolution

Yes, that I understand well & already I mentioned the same in my post,
but my issue is : even after trying everything changes per the above knowledge, I’m not able to see 3d of my accelerometer, as I’m not good at processing sketches…

"We can see total byte is 4*3 = 12 + 2 spaces, so 15 bytes 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 as i’m good at Arduino coding

Hi @narendok, I copied the data you posted into Notepad++ and formatted it to see the pattern:

         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

In each set there are 13 numbers + 2 spaces = 15 as you say. I think your code is getting out of step with which 15 chars you are reading. I don’t know what range of numbers are possible. If e.g. the first number can go down to 999 your digit counting will go wrong. I think in the Ard sketch you should print a carriage return after each set of numbers, and in Processing read-until cr. Then you will be correctly synchronised.

1 Like

Closely scrutinize this line of code:

Take a look at the reference:
https://processing.org/reference/libraries/serial/Serial_readBytesUntil_.html

The original source of this code (10 years old) had no comments and ASCII 10 (LF or '\n') in place of the 15 for the inByte (see reference).

:)