Assignment 5.4

Post date: Jul 01, 2010 3:15:23 AM

I set up the next sketches to light up an led when I moused over the corresponding box in processor. The arduino code was set up to be easily customizable for a different amount of LEDs (might come in handy later)

Arduino Code

#define LEDCOUNT 4

const int led[LEDCOUNT] = {12, 11, 10, 9};

void setup() {

// set led pins to output

for(int i = 0; i < LEDCOUNT; i++) {

pinMode(led[i], OUTPUT);

}

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

}

void loop()

{

if (Serial.read() == 4) { //if no button pressed

for(int i = 0; i < LEDCOUNT; i++) {

digitalWrite(led[i], LOW);

}

}

else {

digitalWrite(led[Serial.read()], HIGH);

}

}

The processing code is a little messier than I like but unfortunately the library lacks the abstraction classes needed to simplify most of the code (ie a class for rectangles, and automated boundary checking function, etc)

Processing Code

import processing.serial.*;

Serial serPort; // Create object from Serial class

byte flagOn = 0; // flag set to 1 if any boxes are moused over and 0 otherwise

void setup()

{

background(255); //set bg to white

fill(0); //set fill to black

size(400, 400); //set size to 400 x 400

serPort = new Serial(this, Serial.list()[0], 9600); //create new serial port instance

}

void draw() {

//test if mouse is in boundaries of rectangle 1

if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150)) {

// If mouse is over square,

fill(204); // change color and

serPort.write(0); // write 0 to serial port

flagOn = 1; //set flag to 1

}

rect(50, 50, 100, 100); // Draw a square

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

//rectangle 2

if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 250) && (mouseY <= 350)) {

fill(204);

serPort.write(1);

flagOn = 1;

}

rect(50, 250, 100, 100);

fill(0);

//rectangle 3

if ((mouseX >= 250) && (mouseX <= 350) && (mouseY >= 50) && (mouseY <= 150)) {

fill(204);

serPort.write(2);

flagOn = 1;

}

rect(250, 50, 100, 100);

fill(0);

//rectangle 4

if ((mouseX >= 250) && (mouseX <= 350) && (mouseY >= 250) && (mouseY <= 350)) {

fill(204);

serPort.write(3);

flagOn = 1;

}

if (flagOn == 0) {

serPort.write(4);

}

rect(250, 250, 100, 100);

fill(0);

flagOn = 0;

}