Assignment 10.2

Post date: Aug 01, 2010 5:44:2 PM

Move your mouse to the right in the processing sketch, the LED gets bright. Move it to the left, it gets darker. Genius.

Also the background in processing gets brighter and darker along with the LED. I thought that was fun.

PROCESSING CODE:

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

Serial serPort; // Create object from Serial class

void setup() {

size(600, 150);

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

}

void draw() {

int briteness = int(map(mouseX, 0, 600, 0, 255)); //set variable relative to mouse X

serPort.write(briteness); //write value to serial

background(0, briteness, 0); //set background green to value

delay(50); //delay 50 ms so arduino can catch up

}

ARDUINO CODE:

const int led1 = 9;

int brightness;

void setup() {

Serial.begin(9600);

}

void loop() {

if (Serial.available() > 0) { //check if serial isn't empty

brightness = Serial.read(); //set varible to last byte in serial

analogWrite(led1, brightness); //write value to LED

}

delay(50);

}