Assignment 7.1

Post date: Jul 03, 2010 2:13:52 AM

This one actually turned out quite swell - it's almost like I intended the animation to be used with a potentiometer from the start. After figuring out a little algebra I set the angle of the eyes rotating to be controlled by the value of the potentiometer's output through the serial port. The only tricky part was, while my delay(16) in the arduino code may seem like an arbitrary number, I actually had to tinker around with that for a while - if the number was too high, the eyes would flicker, while if it was too low (ie the data was being sent too fast), the sketch couldn't process it quickly enough and it was slow to respond, so as it turns out, the magic number is sixteen. I'm not exactly equipped to take a video right now but considering the simple nature of the program and the fact that I've already posted 2 similar sketches, you should be able to imagine with your brains how it went down. Code:

(my fancy text boxes don't seem to want to work right now so I guess I'll do this one the old-fashioned way...)

ARDUINO CODE

//I love how succinct this code is

const int pot1 = 5;

void setup () {

Serial.begin(9600); //begin serial communication at 9600 bps

}

void loop () {

//print to serial port (the /4 is there so it all fits in 1 byte for better speed)

Serial.print((analogRead(pot1)/4), BYTE);

delay(16); //<<magic number

}

PROCESSING CODE

import processing.serial.*; //import all serial libraries

Serial serPort; // Create object from Serial class

PImage smileyImage; //declare image

float angleA = 0;

void setup() {

size(600, 600); //set size to 600 x 600 pixels

background(255); //set background to white

stroke(0); //set stroke color ot black

fill(0); //set fill color to black

smileyImage = loadImage("smiley.png"); //load smiley image

serPort = new Serial(this, Serial.list()[0], 9600);

}

void draw() {

image(smileyImage, 150, 150); //draw image at center of screen

ellipseMode(CENTER); //set ellipses to draw from center

if (serPort.available() <= 0) { //if no data, exit function

return;

}

angleA = (serPort.read() / (255 / TWO_PI)); //set angle to pot value (with range 1 - ~6.28)

float xdist = cos(angleA) * 20; //get the measure of the x shift (opposite side) through cosine

float ydist = sin(angleA) * 20; //get the measure of the y shift (adjacent side) through sine

ellipse(215 + xdist, 255 + ydist, 30, 30); //draw right pupil, adding shift distance to x and y

ellipse(350 + xdist, 255 + ydist, 30, 30); //draw left pupil

}