'How to use data from Arduino accelerometer to control camera and perspective in Processing

I’m new to the forum, I wonder if anyone can help me with how to use data from Arduino accelerometer to control camera and perspective in Processing? I’m making a 3D model of a galaxy and wish to use accelerometer to control the perspective (zoom in or out, etc.) Here is my codes right now. The 3D model and serial part is done but I don’t know how to use the acceleration value as input of camera() and perspective() functions.

I revised in this way and I got the message of: “Error, disabling serialEvent() for (my USBport)” and null as output…

import processing.serial.*;
Serial myPort;
String inString ;
float[] data = new float [3];
float xA = int(data[0]);
float yA = int(data[1]);
float zA = int(data[2]);

float xV = xA*0.1;
float yV = yA*0.1;
float zV = zA*0.1;

float xS = 1/2*xA*0.01+xV*0.1;
float yS = 1/2*yA*0.01+yV*0.1;
float zS = 1/2*zA*0.01+zV*0.1;


Planet sun;
PImage sunTexture;
PImage[] textures = new PImage[10];


float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(fov/2.0);

void setup() {
  size(600, 375, P3D);

  myPort = new Serial(this, Serial.list()[1], 9600);
  println(Serial.list()[1]);
  myPort.bufferUntil('\n');

  
  sunTexture = loadImage("sun.jpg");
  textures[0] = loadImage("dirt.jpg");
  textures[1] = loadImage("eris.jpg");
  textures[2] = loadImage("golden.jpg");
  textures[3] = loadImage("green.jpg");
  textures[4] = loadImage("ice.jpg");
  textures[5] = loadImage("lava.jpg");
  textures[6] = loadImage("north.jpg");
  textures[7] = loadImage("ocean.jpg");
  textures[8] = loadImage("pink.jpg");
  textures[9] = loadImage("purple.jpg");


  sun = new Planet(50, 0, 0, sunTexture);
  sun.spawnMoons(10, 1);
}

void draw() {

  background(0);
  camera(xS, yS, zS, (width/2.0)-300, (height/2.0)-187, 0, 0, 1, 0);
  translate(width/2, height/2, -width);
  sun.show();
  sun.orbit();
  //perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*10.0);
  
}


void serialEvent(Serial myPort) {
  xA = int(data[0]); // float ?
  yA = int(data[1]);
  zA = int(data[2]);

  xV = xA*0.1;
  yV = yA*0.1;
  zV = zA*0.1;

  xS = 1/2*xA*0.01+xV*0.1;
  yS = 1/2*yA*0.01+yV*0.1;
  zS = 1/2*zA*0.01+zV*0.1;
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {

    inString = trim(inString);
    println(inString);
    data = float (split(inString, ','));
    
}

    class Planet {
  float radius;
  float angle;
  float distance;
  Planet[] planets;
  float orbitspeed;
  PVector v;
  
  PShape globe;
  

  Planet(float r, float d, float o, PImage img) {
    v = PVector.random3D();
    radius = r;
    distance = d;
    v.mult(distance);
    angle = random(TWO_PI);
    orbitspeed = o;
    
    noStroke();
    noFill();
    globe = createShape(SPHERE,radius);
    //globe.setTexture(img);
    globe.setTexture(img);
  }
  void orbit() {
    angle = angle + orbitspeed;
    if (planets != null) {
      for (int i=0; i<planets.length; i++) {
        planets[i].orbit();
      }
    }
  }
  void spawnMoons(int total, int level) {
    planets = new Planet[total];
    for (int i=0; i<planets.length; i++) {
      float r = radius/(level*4);
      float d = i*6 + random((radius + r), (radius+r)*6);
    
      float o = random(-0.01, 0.01);
      
      planets[i] = new Planet(r, d, o, textures[i]);
      if (level < 2) {
        int num = int(random(0,2));
        planets[i].spawnMoons(num, level+1);
      }
    }
  }


  void show() {
    pushMatrix();
    noStroke();
    fill(255);
    //rotate(angle);
    PVector v2 = new PVector(1,0,1);
    PVector p = v.cross(v2);
    rotate(angle,p.x,p.y,p.z);
    //stroke(255);
    //line(0,0,0,v.x,v.y,v.z);
    //line(0,0,0,p.x,p.y,p.z);
    
    translate(v.x,v.y,v.z);
    
    
    shape(globe);
    if(distance ==0){
      pointLight(255,255,255,300,187,0);
    }
    

    //sphere(radius);
    //ellipse(0, 0, radius*2, radius*2);

    if (planets != null) {
      //println(planets.length);
      for (int i=0; i<planets.length; i++) {
        planets[i].show();
      }
    }
    popMatrix();
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source