Assignment 5.2

Post date: Jun 30, 2010 11:7:6 PM

Using the same code from assignment 5.1 to read the input from arduino, I altered the processing code for my animation sketch to the following:

Animation With Arduino - 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

switch (serPort.read()) { //begin switch-case with serial code

case 1 : angleA += .5; //switch1 pressed, rotate right

break;

case 2 : angleA -= .5; //switch2 pressed, rotate left

break;

default : break;

}

if(angleA >= 360) {

angleA -= 360;

}

if(angleA <= 0) {

angleA += 360;

}

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

}

I included a short swf video of the sketch as an attachment.