Skip to content

PComp Lab: Serial Output

December 17, 2011

http://vimeo.com/30552604
For this lab, I experimented with an ITG3200 gyroscope. The Arduino code looked like this:

#include <Wire.h>
#include <itg3200.h>

ITG3200 gyro;

void setup(){
  Serial.begin(115200);
  gyro.begin(0x69);
  delay(1000);
}

void loop(){
  Serial.println(gyro.getZ());
  delay(5);
}

Here’s my processing code:

float offset = 2.23;
float rot = 0.0;
import processing.serial.*;

Serial myPort;        // The serial port

void setup() {
  size(600, 800);
  background(255);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 115200);
  myPort.bufferUntil('\n');
}


void draw() {
  background(255);
  translate(width/2, height/2);
  rotate(rot);
  fill(200);
  rect(-100, -300, 200, 600);
}


void serialEvent (Serial myPort) {
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // convert to an int and map to the screen height:
    float inByte = float(inString); 
    inByte = inByte - offset;
    rot = inByte/-300;
  }
}
Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: