Pages: [1]   Go Down
Author Topic: Arduino + WiiMotionPlus + Processing  (Read 465 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, i am collecting data from the Wii Motion Plus gyros with Arduino and sending them over to Processing to plot a graph. Arduino is sending the data as a string so i need Processing to split that string and arrange it into an array. I have it working more or less but the problem is that i get only the first set of array ( println(sensors[0]) ) printed into the serial and then it stops with error.

I did some searching but everything is very vague. I assume it's something related to the serial Buffer, but I'm not sure.

Any idea on how i could get the println(sensors[1]) and println(sensors[2])?

Arduino code:

Code:
#include <Wire.h>

byte data[6];          //six data bytes
int yaw, pitch, roll;  //three axes
int yaw0, pitch0, roll0;  //calibration zeroes

void wmpOn(){
  Wire.beginTransmission(0x53);    //WM+ starts out deactivated at address 0x53
  Wire.send(0xfe);                 //send 0x04 to address 0xFE to activate WM+
  Wire.send(0x04);
  Wire.endTransmission();          //WM+ jumps to address 0x52 and is now active
}

void wmpSendZero(){
  Wire.beginTransmission(0x52);    //now at address 0x52
  Wire.send(0x00);                 //send zero to signal we want info
  Wire.endTransmission();
}

void calibrateZeroes(){
  for (int i=0;i<10;i++){
    wmpSendZero();
    Wire.requestFrom(0x52,6);
    for (int i=0;i<6;i++){
      data[i]=Wire.receive();
    }
    yaw0+=(((data[3]>>2)<<8)+data[0])/10;        //average 10 readings for each zero
    pitch0+=(((data[4]>>2)<<8)+data[1])/10;
    roll0+=(((data[5]>>2)<<8)+data[2])/10;
  }
  Serial.print("Yaw0:");
  Serial.print(yaw0);
  Serial.print("  Pitch0:");
  Serial.print(pitch0);
  Serial.print("  Roll0:");
  Serial.println(roll0);
}

void receiveData(){
  wmpSendZero();                   //send zero before each request (same as nunchuck)
  Wire.requestFrom(0x52,6);        //request the six bytes from the WM+
  for (int i=0;i<6;i++){
    data[i]=Wire.receive();
  }
  yaw=((data[3]>>2)<<8)+data[0]-yaw0;        //see http://wiibrew.org/wiki/Wiimote/Extension_Controllers#Wii_Motion_Plus
  pitch=((data[4]>>2)<<8)+data[1]-pitch0;    //for info on what each byte represents
  roll=((data[5]>>2)<<8)+data[2]-roll0;     
}

void setup(){
  Serial.begin(115200);
  Serial.println("WM+ tester");
  Wire.begin();
  wmpOn();                        //turn WM+ on
  calibrateZeroes();              //calibrate zeroes
  delay(1000);
}

void loop(){
  receiveData();    //receive data and calculate yaw pitch and roll

  yaw = map(yaw, -(yaw0), yaw0, 0, 100);
  pitch = map(pitch, -(pitch0), pitch0, 0, 100);
  roll = map(roll, -(roll0), roll0, 0, 100);
  Serial.print(yaw);  //for info on which axis is which
  Serial.print(',');
  Serial.print(pitch);
  Serial.print(',');
  Serial.println(roll);
  Serial.print('\n');
  delay(100);
}

Processing code:

Code:
import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1; // horizontal position of the graph
int[] sensors = new int [3];

void setup () {
  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Arduino, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 115200);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
}
void draw () {
  // everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);

    int sensors[] = int(split(inString, ','));
    println(sensors[0]);
    println(sensors[1]);
    println(sensors[2]);

    }
}

Thanks in advance.
Logged

Pages: [1]   Go Up
Jump to: