# How to read multiple sensors in Processing using Arduino I2C

**URL:** <https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [June 19, 2021, 6:48pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799 "2021-06-19T18:48:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 19, 2021, 6:48pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/1 "2021-06-19T18:48:53Z")

</div>

hello everyone ive got 2 angle sensors that read accel gyro and mag o have all the code work done on arduino and i have both sensors working and feeding data to the serial monitor so i want to get 2 seperate tracking blocks on processing i have one sketch coded to run a flat tracking block that follows the sensors but it put both sensors on top of eachother and it doesnt work right is there and way to unfix it from center screen to follow the sensors all around screen and how do i split the screen to have 2 sensors on there  
not sure what all you will need for help so lmk

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [June 19, 2021, 10:56pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/2 "2021-06-19T22:56:41Z")

</div>

hi,  
it’ s difficult to figure out so far, if i well understand, your sketch receive data without problems, but when you use those values the drawing is not what you expect?  
you can help us by pasting here the processing code you made

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 19, 2021, 11:10pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/4 "2021-06-19T23:10:57Z")

</div>

```auto
import java.awt.event.KeyEvent;
import java.io.IOException;
Serial myPort;
String data="";
float roll, pitch,yaw;
void setup() {
  size (600, 600, P3D);
  myPort = new Serial(this, "COM5", 115200); // starts the serial communication
  myPort.bufferUntil('\n');
}
void draw() {
  translate(width/2, height/2, 0);
  background(233);
  textSize(22);
  text("Roll: " + int(roll) + " Pitch: " + int(pitch), -100, 265);
  // Rotate the object
  rotateX(radians(-pitch));
  rotateZ(radians(roll));
  rotateY(radians(yaw));
  
  // 3D 0bject
  textSize(30);  
  fill(0, 76, 153);
  box (386, 40, 200); // Draw box
  textSize(25);
  fill(255, 255, 255);
  text(" Cody Galley", -183, 10, 101);
  //delay(10);
  //println("ypr:\t" + angleX + "\t" + angleY); // Print the values to check whether we are getting proper values
}
// Read data from the Serial Port
void serialEvent (Serial myPort) { 
  // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
  data = myPort.readStringUntil('\n');
  // if you got any bytes other than the linefeed:
  if (data != null) {
    data = trim(data);
    // split the string at "/"
    String items[] = split(data, '/');
    if (items.length > 1) {
      //--- Roll,Pitch in degrees
      roll = float(items[0]);
      pitch = float(items[1]);
      yaw = float(items[2]);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 19, 2021, 11:13pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/5 "2021-06-19T23:13:00Z")

</div>

thats my processing code it tracks great but idk how to split it up for the different sensors and i also want to make it follow around the screen instead of being fixed to center screen

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 19, 2021, 11:13pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/6 "2021-06-19T23:13:42Z")

</div>

```auto
#include <WT50.h>
#include <WT49.h>

void setup() 
{
	Serial.begin(115200);
	WT50.startIIC();
  WT49.startIIC();
} 

void loop() 
{ 
/*	Serial.print("Time:20");
	Serial.print(WT50.getTime("year"));
	Serial.print("-");
	Serial.print(WT50.getTime("month"));
	Serial.print("-");
	Serial.print(WT50.getTime("day"));
	Serial.print(" ");
	Serial.print(WT50.getTime("hour"));
	Serial.print(":");
	Serial.print(WT50.getTime("minute"));
	Serial.print(":");
	Serial.println((float)WT50.getTime("second")+(float)WT50.getTime("milisecond")/1000);

							 
	Serial.print("Acc:");
	Serial.print(WT50.getAccX());
	Serial.print(" ");
	Serial.print(WT50.getAccY());
	Serial.print(" ");
	Serial.print(WT50.getAccZ());
	Serial.print("\n");

	
	Serial.print("Gyro:");
	Serial.print(WT50.getGyroX());
	Serial.print(" ");
	Serial.print(WT50.getGyroY());
	Serial.print(" ");
	Serial.print(WT50.getGyroZ());
	Serial.print("\n");
	
	Serial.print("Mag:");
	Serial.print(WT50.getMagX());
	Serial.print(" ");
	Serial.print(WT50.getMagY());
	Serial.print(" ");
	Serial.print(WT50.getMagZ());
	Serial.print("\n");

*///	Serial.print("Angle:");
	Serial.print(WT50.getRoll());
	Serial.print("/");
	Serial.print(WT50.getPitch());
	Serial.print("/");
	Serial.println(WT50.getYaw());

	/*
	Serial.print("DStatus:");
	Serial.print(WT50.getD0Status());
	Serial.print(" ");
	Serial.print(WT50.getD1Status());
	Serial.print(" ");
	Serial.print(WT50.getD2Status());
	Serial.print(" ");
	Serial.print(WT50.getD3Status());
	Serial.print("\n");

  Serial.println("WT49");
  
  Serial.print("Time:20");
  Serial.print(WT49.getTime("year"));
  Serial.print("-");
  Serial.print(WT49.getTime("month"));
  Serial.print("-");
  Serial.print(WT49.getTime("day"));
  Serial.print(" ");
  Serial.print(WT49.getTime("hour"));
  Serial.print(":");
  Serial.print(WT49.getTime("minute"));
  Serial.print(":");
  Serial.println((float)WT49.getTime("second")+(float)WT49.getTime("milisecond")/1000);

               
  Serial.print("Acc:");
  Serial.print(WT49.getAccX());
  Serial.print(" ");
  Serial.print(WT49.getAccY());
  Serial.print(" ");
  Serial.print(WT49.getAccZ());
  Serial.print("\n");

  
  Serial.print("Gyro:");
  Serial.print(WT49.getGyroX());
  Serial.print(" ");
  Serial.print(WT49.getGyroY());
  Serial.print(" ");
  Serial.print(WT49.getGyroZ());
  Serial.print("\n");
  
  Serial.print("Mag:");
  Serial.print(WT49.getMagX());
  Serial.print(" ");
  Serial.print(WT49.getMagY());
  Serial.print(" ");
  Serial.print(WT49.getMagZ());
  Serial.print("\n");
*/
 // Serial.print("Angle:");
  Serial.print(WT49.getRoll());
  Serial.print("/");
  Serial.print(WT49.getPitch());
  Serial.print("/");
  Serial.println(WT49.getYaw());

  /*
  Serial.print("DStatus:");
  Serial.print(WT49.getD0Status());
  Serial.print(" ");
  Serial.print(WT49.getD1Status());
  Serial.print(" ");
  Serial.print(WT49.getD2Status());
  Serial.print(" ");
  Serial.print(WT49.getD3Status());
  Serial.print("\n");

  
  

  
  Serial.println("");
 */

}

```

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 19, 2021, 11:14pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/7 "2021-06-19T23:14:25Z")

</div>

thats my arduino code

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [June 20, 2021, 12:21am UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/8 "2021-06-20T00:21:34Z")

</div>

ok,  
if i remove the commented part in your arduino code is :

```auto
#include <WT50.h>
#include <WT49.h>

void setup() 
{
  Serial.begin(115200);
  WT50.startIIC();
  WT49.startIIC();
} 

void loop() 
{ 
// Serial.print("Angle:");
  Serial.print(WT50.getRoll());
  Serial.print("/");
  Serial.print(WT50.getPitch());
  Serial.print("/");
  Serial.println(WT50.getYaw());

 // Serial.print("Angle:");
  Serial.print(WT49.getRoll());
  Serial.print("/");
  Serial.print(WT49.getPitch());
  Serial.print("/");
  Serial.println(WT49.getYaw());

}

```

here are some strange format: first 3 values separated with “/” third value is followed by “\r” due to [println](https://www.arduino.cc/reference/en/language/functions/communication/serial/println/) in Serial.println(WT50.getYaw());  
than 3 others values with print and finally println  
so it’s two lines sent by arduino code

on processing side

you read until end of line, so only one set of value is read

i think first is to have this more coherent:

```auto
// Serial.print("Angle:");
  Serial.print(WT50.getRoll());
  Serial.print("/");
  Serial.print(WT50.getPitch());
  Serial.print("/");
  Serial.print(WT50.getYaw());
  Serial.print("/");

 // Serial.print("Angle:");
  Serial.print(WT49.getRoll());
  Serial.print("/");
  Serial.print(WT49.getPitch());
  Serial.print("/");
  Serial.println(WT49.getYaw());

}

```

now you have 6 values on same line

on processing side you need to read them all:

```auto
      roll50 = float(items[0]);
      pitch50 = float(items[1]);
      yaw50 = float(items[2]);

      roll49 = float(items[3]);
      pitch49 = float(items[4]);
      yaw49 = float(items[5]);

```

i recommand to put  
`println( roll50, pitch50 , yaw50, "-", roll49 , pitch49, yaw49 );`  
on your draw() to check data are correct then from here, you can draw stuff according to those values

i’m not sure my explanations are clear, don’t hesitate to ask if needed

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 20, 2021, 2:07am UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/9 "2021-06-20T02:07:22Z")

</div>

i get what you are trying to say but im unsure how to edit it into the code adding just the roll50 etc is undeclared and im not sure how to differentiate it but what you are saying is right

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 20, 2021, 2:09am UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/10 "2021-06-20T02:09:06Z")

</div>

```auto
  text("Roll: " + int(roll) + " Pitch: " + int(pitch), -100, 265);
  // Rotate the object
  rotateX(radians(-pitch));
  rotateZ(radians(roll));
  rotateY(radians(yaw));

```

how would i change this to fit that

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 20, 2021, 2:09am UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/11 "2021-06-20T02:09:42Z")

</div>

```auto

import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
Serial myPort;
String data="";
float roll50, pitch50,yaw50;
float roll49, pitch49, yaw49;
void setup() {
  size (600, 600, P3D);
  myPort = new Serial(this, "COM5", 115200); // starts the serial communication
  myPort.bufferUntil('\n');
}
void draw() {
  translate(width/2, height/2, 0);
  background(233);
  textSize(22);
  text("Roll: " + int(roll) + " Pitch: " + int(pitch), -100, 265);
  // Rotate the object
  rotateX(radians(-pitch));
  rotateZ(radians(roll));
  rotateY(radians(yaw));
  println( roll50, pitch50 , yaw50, "-", roll49 , pitch49, yaw49 );
  // 3D 0bject
  textSize(30);  
  fill(0, 76, 153);
  box (386, 40, 200); // Draw box
  textSize(25);
  fill(255, 255, 255);
  text(" Cody Galley", -183, 10, 101);
  //delay(10);
  //println("ypr:\t" + angleX + "\t" + angleY); // Print the values to check whether we are getting proper values
}
// Read data from the Serial Port
void serialEvent (Serial myPort) { 
  // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
  data = myPort.readStringUntil('\n');
  // if you got any bytes other than the linefeed:
  if (data != null) {
    data = trim(data);
    // split the string at "/"
    String items[] = split(data, '/');
    if (items.length > 1) {
      //--- Roll,Pitch in degrees
      roll50 = float(items[0]);
      pitch50 = float(items[1]);
      yaw50 = float(items[2]);

      roll49 = float(items[3]);
      pitch49 = float(items[4]);
      yaw49 = float(items[5]);
    }
  }
}

```

this is what i did so far with it

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 20, 2021, 2:22am UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/13 "2021-06-20T02:22:25Z")

</div>

the reason i have the values seperated with / is because in processing i told it to check every value after / and stop at new line

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [June 20, 2021, 11:21am UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/14 "2021-06-20T11:21:25Z")

</div>

great!

ok, first is to fix data transfer:  
did you changed your arduino code and replaced first println by print?

to check on processing side  
add println in serialevent to check what serial data are incoming

```auto
void serialEvent (Serial myPort) { 
  // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
  data = myPort.readStringUntil('\n');
  // if you got any bytes other than the linefeed:
  if (data != null) {
println("data string",data);
    data = trim(data);
    // split the string at "/"
    String items[] = split(data, '/');
    if (items.length > 1) {
println(items);
      //--- Roll,Pitch in degrees
      roll50 = float(items[0]);
      pitch50 = float(items[1]);
      yaw50 = float(items[2]);

      roll49 = float(items[3]);
      pitch49 = float(items[4]);
      yaw49 = float(items[5]);
    }
  }
}

```

we expect to see something like:  
data 0.0/1.0/2.0/3.0/4.0/5.0  
0.0 1.0 2.0 3.0 4.0 5.0

i put 0 1 2 3 … but it will be random numbers from your sensors

once you fixed that:

in you draw, you take care of only one sensor :

```auto
  rotateX(radians(-pitch));
  rotateZ(radians(roll));
  rotateY(radians(yaw));

```

but now, you have two, with different naming: (i put 50 and 49 at the end as it is your sensors naming in arduino code)

so you need to process both sensors  
rotations according to first one are now:

```auto
  rotateX(radians(-pitch50));
  rotateZ(radians(roll50));
  rotateY(radians(yaw50));

```

and second

```auto
  rotateX(radians(-pitch49));
  rotateZ(radians(roll49));
  rotateY(radians(yaw49));

```

but all rotations in processing are cumulatives so you need to do first sensors moves, then go gack to default position and draw second sensors  
this is the purpose of [pushMatrix](https://processing.org/reference/pushMatrix_.html)() and [popMatrix](https://processing.org/reference/popMatrix_.html)() in this code:

```auto

import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
Serial myPort;
String data="";
float roll50, pitch50,yaw50;
float roll49, pitch49, yaw49;
void setup() {
  size (1200, 600, P3D);
  myPort = new Serial(this, "COM5", 115200); // starts the serial communication
  myPort.bufferUntil('\n');
}
void draw() {
  background(233);
  textSize(22);
  pushMatrix(); //save current default position
  
  ///// firt sensor
  translate(width/4, height/2, 0); //move to left half of the screen 
  text("Roll: " + int(roll50) + " Pitch: " + int(pitch50), -100, 265);
  // Rotate the object
  rotateX(radians(-pitch50));
  rotateZ(radians(roll50));
  rotateY(radians(yaw50));
  fill(0, 76, 153);
  box (386, 40, 200); // Draw box
  
  popMatrix(); // go back to default position
  
  ///// second sensor
  translate(width*3/4, height/2, 0); //move to right half of the screen 
  text("Roll: " + int(roll49) + " Pitch: " + int(pitch49), -100, 265);
  // Rotate the object
  rotateX(radians(-pitch49));
  rotateZ(radians(roll49));
  rotateY(radians(yaw49));
  fill(100, 76, 153);
  box (386, 40, 200); // Draw box
  
  
  
  textSize(25);
  fill(255, 255, 255);
  text(" Cody Galley", -183, 10, 101);
  
  println( roll50, pitch50 , yaw50, "-", roll49 , pitch49, yaw49 );
  
  //delay(10);
  //println("ypr:\t" + angleX + "\t" + angleY); // Print the values to check whether we are getting proper values
}
// Read data from the Serial Port
void serialEvent (Serial myPort) { 
  // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
  data = myPort.readStringUntil('\n');
  // if you got any bytes other than the linefeed:
  if (data != null) {
    data = trim(data);
    // split the string at "/"
    String items[] = split(data, '/');
    if (items.length > 1) {
      //--- Roll,Pitch in degrees
      roll50 = float(items[0]);
      pitch50 = float(items[1]);
      yaw50 = float(items[2]);

      roll49 = float(items[3]);
      pitch49 = float(items[4]);
      yaw49 = float(items[5]);
    }
  }
}

```

you where close, tell me if some parts of this code is still obscur

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 20, 2021, 11:14pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/15 "2021-06-20T23:14:04Z")

</div>

wow that is great work and thank you so much after working it all in and adding it together the reading are coming in the problem im seeing now is the monitor in processing isnt being consistent im gonna try my best to make it sound the way i see it.  
168.02 -25.16 NaN - -54.82 -17.07 NaN  
168.02 -25.16 NaN - -54.82 -17.07 NaN  
168.02 -25.16 NaN - -54.82 -17.07 NaN  
168.02 -25.16 NaN - -54.82 -17.07 NaN  
is the readings im getting now but it wont stay the same some times it comes back as  
24.87 NaN -54.96 - -16.07 -17.34 0.0  
24.87 NaN -54.96 - -16.07 -17.34 0.0  
24.87 NaN -54.96 - -16.07 -17.34 0.0  
thats another random i get and  
NaN -55.1 -17.46 - 0.0 0.0 0.0  
NaN -55.1 -17.46 - 0.0 0.0 0.0  
NaN -55.1 -17.46 - 0.0 0.0 0.0  
is just three snipd from running the code 6 times it came back 2 times as the first response.  
when it does work this is what is in the processing monitor  
127.32 -20.82 -35.48 - 8.67 -82.51 66.02  
127.33/-20.82/-35.47 / 8.67/-82.51/66.02

127.33 -20.82 -35.47 8.67 -82.51 66.02  
127.33 -20.82 -35.47 - 8.67 -82.51 66.02  
127.33 -20.82 -35.47 - 8.67 -82.51 66.02  
127.33 -20.82 -35.47 - 8.67 -82.51 66.02  
127.33/-20.82/-35.47 / 8.67/-82.51/66.02

127.33 -20.82 -35.47 8.67 -82.51 66.02  
127.33 -20.82 -35.47 - 8.67 -82.51 66.02  
im confused on why the slash shows up on some of the lines and the dash comes in on some but the others are raw code  
this is arduino serial vs processing monitor  
arduino  
150.33/-19.73/-86.89 / 10.10/-80.01/63.90  
150.33/-19.73/-86.90 / 10.13/-80.01/63.85  
150.33/-19.73/-86.91 / 10.13/-80.01/63.87  
150.33/-19.73/-86.91 / 10.14/-80.01/63.86  
processing  
150.80 -19.92 -87.55 10.27 -80.08 63.72  
150.8 -19.92 -87.55 - 10.27 -80.08 63.72  
150.8 -19.92 -87.55 - 10.27 -80.08 63.72  
150.8 -19.92 -87.55 - 10.27 -80.08 63.72  
150.74/-19.93/-87.55 / 10.72/-80.08/63.34  
it will work though just crashes on start up sometimes. says null data found closing COM5 but if i reset it then try again it will work maybe put some kind of reset in the processing code for if it reads null data to reset and try again instead of crashing. i also need a little help calibrating the sensors on arduino and is there any way to make the block move around the screen to follow where the sensors are. In my yaw of the arduino code i have to add a offset to it to make it go to true dead center but i need it to do it on its own instead of me adding it in there its a magnet issue i know that much. that is the two things i have holding me up now you helped me so much i did a little work to the sketch and it tracks both blocks on left and right and rotates with the sensors perfectly i just need to follow on screen also like make it move left, right, up, and down instead of only rotating. my arduino code now is

```auto
#include <WT50.h>
#include <WT49.h>

void setup() 
{
  Serial.begin(57600);
  WT50.startIIC();
  WT49.startIIC();
} 

void loop() 
{
  Serial.print(WT50.getRoll());
  Serial.print("/");
  Serial.print(WT50.getPitch());
  Serial.print("/");
  Serial.print(WT50.getYaw()+31.30);
  Serial.print("/");
  Serial.print(WT49.getRoll());
  Serial.print("/");
  Serial.print(WT49.getPitch());
  Serial.print("/");
  Serial.println(WT49.getYaw()+8);
}

```

this is the processing sketch with your addons

```auto

import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
Serial myPort;
String data="";
float roll50, pitch50,yaw50;
float roll49, pitch49, yaw49;
void setup() {
  size (1200, 600, P3D);
  myPort = new Serial(this, "COM5", 57600); // starts the serial communication
  myPort.bufferUntil('\n');
}
void draw() {
  background(233);
  textSize(22);
  pushMatrix(); //save current default position
  
  ///// firt sensor
  translate(width/4, height/2, 0); //move to left half of the screen 
  text("Roll: " + int(roll50) + " Pitch: " + int(pitch50), -100, 265);
  // Rotate the object
  rotateX(radians(-pitch50));
  rotateZ(radians(roll50));
  rotateY(radians(yaw50));
  fill(0, 76, 153);
  box (386, 40, 200); // Draw box
  // draw name
  textSize(25);
  fill(255, 255, 255);
  text(" Tommy Collins", -183, 10, 101);
  
  popMatrix(); // go back to default position
 
  ///// second sensor
  translate(width*3/4, height/2, 0); //move to right half of the screen 
  text("Roll: " + int(roll49) + " Pitch: " + int(pitch49), -100, 265);
  // Rotate the object
  rotateX(radians(-pitch49));
  rotateZ(radians(roll49));
  rotateY(radians(yaw49));
  fill(100, 76, 153);
  box (386, 40, 200); // Draw box
  // draw name
  textSize(25);
  fill(255, 255, 255);
  text(" Cody Galley", -183, 10, 101);

  println( roll50, pitch50 , yaw50, "-", roll49 , pitch49, yaw49 );
  
  //delay(10);
  //println("ypr:\t" + angleX + "\t" + angleY); // Print the values to check whether we are getting proper values
}
// Read data from the Serial Port
void serialEvent (Serial myPort) { 
  // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
  data = myPort.readStringUntil('\n');
  // if you got any bytes other than the linefeed:
  if (data != null) {
    println("data string",data);
    data = trim(data);
    // split the string at "/"
    String items[] = split(data, '/');
    if (items.length > 1) {
      println(items);
      //--- Roll,Pitch in degrees
      roll50 = float(items[0]);
      pitch50 = float(items[1]);
      yaw50 = float(items[2]);

      roll49 = float(items[3]);
      pitch49 = float(items[4]);
      yaw49 = float(items[5]);
    }
  }
}

```

maybe the way to make it move around the screen has something o do with a camera but im not sure yet i dont know much about the camera features.  
like could i code this to make it work for what i need?

```auto
void draw() {
  lights();
  background(0);
  float cameraY = height/2.0;
  float fov = mouseX/float(width) * PI/2;
  float cameraZ = cameraY / tan(fov / 2.0);
  float aspect = float(width)/float(height);
  if (mousePressed) {
    aspect = aspect / 2.0;
  }
  perspective(fov, aspect, cameraZ/10.0, cameraZ*10.0);
  
  translate(width/2+30, height/2, 0);
  rotateX(-PI/6);
  rotateY(PI/3 + mouseY/float(height) * PI);
  box(45);
  translate(0, 0, -50);
  box(30);
}

```

again you are awesome my friend

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [June 21, 2021, 1:25am UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/16 "2021-06-21T01:25:09Z")

</div>

great,

i 'm puzzled about the nan values you get, i will understand if it was shown in arduino monitor, as gimbal calculation can fail for some peticular values and send nan value, [see this](https://en.wikipedia.org/wiki/Gimbal_lock)

if it was a problem of serial, with bad string, it will fail when processing convert to float and not giving NaN value  
so i guess it s really what arduino sent… strange

other strange part is when you get 0.0 0.0 0.0 which is quite unlikely, may be something wrong arduino side…

> [@Halfrican911](#):
>
> im confused on why the slash shows up on some of the lines and the dash comes in on some but the others are raw code

on the code i put two println(data), one with data, the raw string from arduino, with the slash  
and second is items, an array of float converted from data, it was to check where some problems occurs  
then at end of draw() you have another print with the variables

you can comment the one in draw() then check the 2 lines, with and without “/” match

do you fill data are coherent with the movements of the sensors?  
when i played with gimbal stuff, it can be tricky as the 3 rotations have to be done in good order otherwise mouvements are fine sometimes and weird others…

> [@Halfrican911](#):
>
> maybe the way to make it move around the screen has something o do with a camera but im not sure yet i dont know much about the camera features.  
> like could i code this to make it work for what i need?

ok, i don t really see what you want to do here 🙂  
and it s quite unlikely it needs to play with camera, wich is more to change perspectice, field of wiew…  
do you want to change position of your cubes according to values?

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 21, 2021, 2:47am UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/17 "2021-06-21T02:47:59Z")

</div>

ok thank you for all the info yes it does follow the rotation of the sensor exact its perfect now i lowered the baud rate and it didnt get anymore nan value i think it has something to do with the sensor itself is defaulted to be 9600 and i had it at 115200 i bumped it all back to 9600 and it only errored once and that happens sometimes with the board you just gotta reset the board itself. what im trying to do is follow its movement forward and backwards and then left and right and up and down. i want the sensor to be able to follow around and also rotation i dont know what the word im looking for id but i want it move not just rotate change positions is probably what i am trying to say but im not sure. it has an accelerometer a gyroscope and a magnetometer if that helps any

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 21, 2021, 8:08pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/18 "2021-06-21T20:08:01Z")

</div>

like when you put

```auto
translate(mouseX, mouseY)

```

it follows the mouse on screen i want it to follow the sensors like that and do the rotation is that possible?

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [June 22, 2021, 12:40am UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/19 "2021-06-22T00:40:17Z")

</div>

now you have the values you can try some stuff on processing

like

```auto
translate(-pitch50 ,roll50 , yaw50 );

```

just after rotations , you need to test and may be invert pitch roo yaw variables order in this to get proper effect  
you can change [box](https://processing.org/reference/box_.html) size too or colors in [fill](https://processing.org/reference/fill_.html)()

do your sensors give you other kind of data? like acceleration or magnetic north?

i suggest you, once you have the data the right way, to start another discussion with a question like " how to nicely represent data from arduino sensors?" with the code you have so far  
i suggest that because i m sure some other peoples tried that and it s unlikely they read this discussion

for really moving object accuretly, it will be tricky to convert this data to a position

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 22, 2021, 3:39am UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/20 "2021-06-22T03:39:59Z")

</div>

yes i have accelerometer and gyroscope and magnet sensor so i get all the reads to move

```auto
accX[1] = (x - offsetX);
 // Mechanical Filtering (remove some noise)
 if((accX[1] < 0.2) && (accX[1] > -0.2)) { accX[1] = 0; } 
// First X integration:
 velocityX[1] = velocityX[0] + accX[0] + (accX[1] -accX[0]);
 // Second X integration:
 positionX[1] = positionX[0] + velocityX[0] + (velocityX[1] - velocityX[0]); 
accX[0] = accX[1];
 velocityX[0] = velocityX[1];
 if(velocityX[0] < 0) debug = "left"; 
else if(velocityX[0] > 0) { debug = "right" ; } 
else { debug = "stoped"; }

```

i found this on a physics website for tracking motion but im doing research on it all now. just gotta figure it out so you said make a new post on this matter tho?

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [June 22, 2021, 12:10pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/21 "2021-06-22T12:10:08Z")

</div>

yes, get all data from arduino to processing, same way you did for angles then ask community

get positions from the data will be tricky:  
because your sensors give you acceleration, if you move your sensor 20cm for eg, sensor will send an acceleration , let say +1.0, then deceleration when the movement stop -1.0  
and it s where stuff get complicated, first problem values will not be that nice, and chance are they will not perfectly compensate and at the end you will have not zero but still some small amount acceleration,  
and even small offset will end with a high speed after some amount of time  
then even more tricky the speed (and so the amount of displacemnt) of your object on screen is the acceleration by the amount of time elapsed, your sensor give you only instant acceleration and time between sample will be difficult to know precisely, will vary, and are not representative of all values in between samples  
so this kind of conversion from acceleration to position diverge very fast

it s very easy from positions, to calculate speed and then acceleration, but the oposite is not  
anyhow it s an interesting subject to dig, let see if someone found a nice way to play with it

---

<div class="post-metadata">

**Author:** ![Halfrican911](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Halfrican911](https://discourse.processing.org/u/Halfrican911)\
**Post date:** [June 22, 2021, 1:14pm UTC](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799/22 "2021-06-22T13:14:06Z")

</div>

I’ve been looking into alot of readings and it seems possible with integration of the accel time and gyro to calculate the position and movement I’ve got all of these reading in my arduino code I would just need to calculate the velocity but do I do that in arduino or processing  
I’ve seen alot of threads saying v=v+at so would

```auto
VelocityX= velocityX+accX*time

```

Work I have the acc and time in my code already and I have the gyro I can add position if that’s how they do it.  
My code doubles the values and works in the offset with calibrations I’m not going to say it will be accurate but I’m using a WT901 and ots a high accuracy sensor so I have high hopes we could figure it out

[Next page](https://discourse.processing.org/t/how-to-read-multiple-sensors-in-processing-using-arduino-i2c/30799.md?page=2)
