Need help on Arudino accelerometer + 3D processing code

edited July 2014 in Arduino

I already done out the accelerometer on 3 axis angle calculation with calibration on it. And i also Google out the code to run the 3D cube on processing, but the problem is how do i merge them together? Whenever i turn my accelerometer, the 3D cube will turn together as well.

Answers

  • edited July 2014

    Arduino Code:

    define X_AXIS 0

    define Y_AXIS 1

    define Z_AXIS 2

    void setup() { Serial.begin(9600); }

    void loop() { int x = analogRead(X_AXIS); int y = analogRead(Y_AXIS); int z = analogRead(Z_AXIS);

    Serial.print(x); Serial.print('|'); Serial.print(y); Serial.print(':'); Serial.println(z); }

    Processing Code: I don't have it , and i need it too :)

  • Answer ✓
    #define X_AXIS 0
    #define Y_AXIS 1
    #define Z_AXIS 2
    
    void setup() {
      Serial.begin(9600); 
    }
    
    void loop() {
      int x = analogRead(X_AXIS);
      int y = analogRead(Y_AXIS);
      int z = analogRead(Z_AXIS);
    
      Serial.print(x);
      Serial.print('|');
      Serial.print(y);
      Serial.print(':');
      Serial.println(z);
    }
    
  • edited July 2014

    I'm using the 3d cube processing code below but it does not rotate. How can i solve it?

    import processing.serial.*;
    
    Serial fd;
    
    int aX = 0;
    int aY = 0;
    
    void setup ()
    {
    size(640, 360, P3D);
    //Connect to the corresponding serial port
    fd = new Serial(this, "COM19", 9600);
    // Defer callback until new line
    fd.bufferUntil('\n');
    }
    
    void draw ()
    {
    //Set background
    background(0.5);
    
    pushMatrix();
    
    translate(width/2, height/2, -30);
    
    //Rotate
    rotateX(((float)aX)*PI/180.0);
    rotateZ(((float)aY)*PI/180.0);
    
    //Print data
    print("aX: ");
    print(aX);
    print(", aY: ");
    println(aY);
    
    
    scale(90);
    beginShape(QUADS);
    
    fill(0, 255, 0); vertex(-1, 1, 1);
    fill(0, 255, 0); vertex( 1, 1, 1);
    fill(0, 255, 0); vertex( 1, -1, 1);
    fill(0, 255, 0); vertex(-1, -1, 1);
    
    fill(0, 255, 255); vertex( 1, 1, 1);
    fill(0, 255, 255); vertex( 1, 1, -1);
    fill(0, 255, 255); vertex( 1, -1, -1);
    fill(0, 255, 255); vertex( 1, -1, 1);
    
    
    fill(255, 0, 255); vertex( 1, 1, -1);
    fill(255, 0, 255); vertex(-1, 1, -1);
    fill(255, 0, 255); vertex(-1, -1, -1);
    fill(255, 0, 255); vertex( 1, -1, -1);
    
    fill(255, 255, 0); vertex(-1, 1, -1);
    fill(255, 255, 0); vertex(-1, 1, 1);
    fill(255, 255, 0); vertex(-1, -1, 1);
    fill(255, 255, 0); vertex(-1, -1, -1);
    
    
    fill(255, 0, 0); vertex(-1, 1, -1);
    fill(255, 0, 0); vertex( 1, 1, -1);
    fill(255, 0, 0); vertex( 1, 1, 1);
    fill(255, 0, 0); vertex(-1, 1, 1);
    
    
    fill(0, 0, 255); vertex(-1, -1, -1);
    fill(0, 0, 255); vertex( 1, -1, -1);
    fill(0, 0, 255); vertex( 1, -1, 1);
    fill(0, 0, 255); vertex(-1, -1, 1);
    
    
    endShape();
    
    popMatrix();
    }
    
    void serialEvent (Serial fd)
    {
    // get the ASCII string:
    String rpstr = fd.readStringUntil('\n');
    if (rpstr != null) {
    String[] list = split(rpstr, ':');
    aX = ((int)float(list[0]));
    aY = ((int)float(list[1]));
    }
    }
    
Sign In or Register to comment.