Arduino to Processing serial

edited November 2014 in Arduino

I'm having trouble reading data sent from Arduino Mega 2560 to Processing. First 5 variables are from sensors, the other 6 are from gyro and accelerometer. In separate codes I can read both, but in the code I can't. I've been stuck on this problem for quite a few hours and can't seem to figure it out. It prints out the sensors' values, and when it starts to print the gyro and acc values a NumberFormatException:null box pops out on the first value (sixth in this case).

Arduino code:

    #include<Wire.h>
    const int MPU=0x68;  // I2C address of the MPU-6050
    int AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
    void setup() {
      Wire.begin();
      Wire.beginTransmission(MPU);
      Wire.write(0x6B);  // PWR_MGMT_1 register
      Wire.write(0);     // set to zero (wakes up the MPU-6050)
      Wire.endTransmission(true);
      // initialize serial communication at 9600 bits per second:
      Serial.begin(9600);
    }
    // the loop routine runs over and over again forever:
    void loop() {
      Wire.beginTransmission(MPU);
      Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
      Wire.endTransmission(false);
      Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
      AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
      AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
      Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
      GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
      GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
      GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

      // read the input on analog pin 0:
      int sensorValue0 = analogRead(A0);
      int sensorValue1 = analogRead(A1);
      int sensorValue2 = analogRead(A2);
      int sensorValue3 = analogRead(A3);
      int sensorValue4 = analogRead(A4);

      // print out the value you read:
      Serial.println(sensorValue0);
      Serial.println(sensorValue1);
      Serial.println(sensorValue2);
      Serial.println(sensorValue3);
      Serial.println(sensorValue4);

      Serial.println(AcX);
      Serial.println(AcY);
      Serial.println(AcZ);

      Serial.println(GyX);
      Serial.println(GyY);
      Serial.println(GyZ);

      delay(200);
    }

Processing code:

import cc.arduino.*;

import processing.serial.*;

Serial myPort;  // Create object from Serial class
String val,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10;
float x, y;
float ag;
void setup() {
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}


void draw() {


  //******** IF DATA IS AVAILABLE READ UNTIL EOL AND IF DATA IS NOT NULL PARSE TO FLOAT******
  if ( myPort.available() > 0) 
    {  // If data is available,
    val = myPort.readStringUntil('\n');         // read it and store it in val
    val1= myPort.readStringUntil('\n');
    val2= myPort.readStringUntil('\n');
    val3= myPort.readStringUntil('\n');
    val4= myPort.readStringUntil('\n');
    val5= myPort.readStringUntil('\n');
    val6= myPort.readStringUntil('\n');
    val7= myPort.readStringUntil('\n');
    val8= myPort.readStringUntil('\n');
    val9= myPort.readStringUntil('\n');
    val10= myPort.readStringUntil('\n');
    print(val);
    print(val1);
    print(val2);
    print(val3);
    print(val4); 
    println(val5);
    println(val6);
    println(val7);
    println(val8);
    println(val9); 
    println(val10);

      if (val != null ) { //finger sensors
                         println(Float.parseFloat(val1));
                         println(Float.parseFloat(val1));
                         println(Float.parseFloat(val2));
                         println(Float.parseFloat(val3));
                         println(Float.parseFloat(val4));
                         //gyro 
                         println(Integer.parseInt(val5));
                         println(Integer.parseInt(val6));
                         println(Integer.parseInt(val7));
                         //acc
                         println(Integer.parseInt(val8));
                         println(Integer.parseInt(val9));
                         println(Integer.parseInt(val10));
                         }
    }  
}

Answers

  • Answer ✓

    so it can not work, for multiple sensor see the processing sketch in arduino IDE example-->communication-->virtualColorMixer,or processing sketch here http://arduino.cc/en/Tutorial/VirtualColorMixer.. remember for MEGA put the delay (2000); in the setup

  • edited November 2014

    And how do I clear the print (output buffer)? Most of the time the first character printed is some random symbol, probably from some previous unfinished print.

  • Hi, I recommend to use serialEvent(Serial p) handler function instead of reading data in the draw().

Sign In or Register to comment.