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;
}