I’m trying to read Serial data from 3 different sensors, a DHT11, and MPU6050 and a 30Bar pressure sensor by BlueRobotics. I should mention the pressure sensor and mpu6050 are connected to my Arduino via a multiplexer. I’m just focusing on the DHT11 and MPU6050 for now, I’ve written a simple sketch in Arduino that writes a total of 8 values to processing. I’m getting weird values on the pressure sensor when using the MPU6050 but I’ll deal with that later, other than that the Arduino sketch works well.
Right now all I want is for processing to print these values so I know I’m reading them correctly. Both sketches are below btw. When I run my processing sketch I get a different error every time, I cant find any cyclicity in the error messages, sometimes its a NullPointerException on one line or another. Other times its ArrayIndexOutOfBounds. Maybe 4 out of 10 times I run the sketch in processing I will get 4 or 5 lines of the MPU6050 before getting the ArrayIndexOutOfBounds. Every time I get an error it highlights a different line of code in the void loop. I’m really not sure what is going on so any help would be appreciated!
Arduino IDE Sketch:
#include "Wire.h"
// #include "MS5837.h"
#include <MPU6050_light.h>
#include <DHT11.h>
MPU6050 mpu(Wire);
// MS5837 ms5837;
unsigned long timer = 0;
DHT11 dht11(3);
void PCA9548A(uint8_t bus) {
Wire.beginTransmission(0x70);
Wire.write(1<<bus);
Wire.endTransmission();
//Serial.print(bus);
}
void setup() {
Serial.begin(9600);
// Serial.println("Starting");
Wire.begin();
// PCA9548A(1);
// ms5837.setModel(MS5837::MS5837_30BA);
// ms5837.setFluidDensity(997); // kg/m^3 (freshwater, 1029 for seawater)
PCA9548A(2);
byte status = mpu.begin();
// mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down
mpu.calcOffsets();
}
void loop() {
//ms5837.read();
int humidity = dht11.readHumidity();
mpu.update();
if((millis()-timer)>10){ // print data every 10ms
Serial.print(mpu.getAngleX());
Serial.print(",");
Serial.print(mpu.getAngleY());
Serial.print(",");
Serial.print(mpu.getAngleZ());
//Serial.print(",");
// Serial.println(humidity);
// Serial.print("\t");
// Serial.print(ms5837.depth());
// Serial.print("\t");
// Serial.print(ms5837.pressure());
// Serial.print("\t");
// Serial.print(ms5837.altitude());
// Serial.print("\t");
// Serial.println(ms5837.temperature());
timer = millis();
}
delay(1000);
}
Processing IDE Sketch:
import processing.serial.*;
Serial myPort; // Create object from Serial class
String data;
String[] split;
float X;
float Y;
float Z;
float humidity;
void setup() {
size(200, 200);
myPort = new Serial(this, "COM3", 9600);
}
void draw() {
while(myPort.available() > 0) {
data = myPort.readStringUntil('\n');
split = data.split(",");
X = float(split[0]);
Y = float(split[1]);
Z = float(split[2]);
humidity = float(split[3]);
println(X + "\t" + Y + "\t" + Z + "\t" + humidity);
delay(1000);
}
}