/** * Simple Write. * * Check if the mouse is over a rectangle and writes the status to the serial port. * This example works with the Wiring / Arduino program that follows below. */ import processing.serial.*; Serial myPort; // Create object from Serial class int val; // Data received from the serial port void setup() { size(200, 465); // I know that the first port in the serial list on my mac // is always my FTDI adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. String portName = Serial.list()[0]; println(portName); myPort = new Serial(this, portName, 9600); } void draw() { byte val; background(0); val = mouseOverRect(); fill(204); // change color and myPort.write(val); // send an H to indicate mouse is over square fill(255, 0, 0); rect(50, 25, 100, 100); // Draw a square fill(255, 255, 0); rect(50, 150, 100, 100); // Draw a square fill(0, 255, 0); rect(50, 275, 100, 100); // Draw a square fill(128, 128, 128); rect(15, 400, 50, 50); // Draw a square rect(75, 400, 50, 50); // Draw a square rect(135, 400, 50, 50); // Draw a square } void selectLight(int addr) { // switch to unicast to addr myPort.write("+++"); delay(10); while(myPort.available() > 0) { println(myPort.readChar()); } myPort.write("ATDL"+char(addr)+"\n"); delay(10); while(myPort.available() > 0) { println(myPort.readChar()); } myPort.write("ATWR"+"\n"); delay(10); while(myPort.available() > 0) { println(myPort.readChar()); } delay(2000); }; byte mouseOverRect() { // Test if mouse is over square if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 25) && (mouseY <= 125)) return 0x04; // Red if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 150) && (mouseY <= 250)) return 0x02; // Yellow if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 275) && (mouseY <= 375)) return 0x01; // Green if ((mouseX >= 15) && (mouseX <= 65) && (mouseY >= 400) && (mouseY <= 450)) selectLight(1); // Light 1 if ((mouseX >= 75) && (mouseX <= 125) && (mouseY >= 400) && (mouseY <= 450)) selectLight(2); // Light 2 if ((mouseX >= 135) && (mouseX <= 185) && (mouseY >= 400) && (mouseY <= 450)) selectLight(3); // Light 3 return 0; } /* // Wiring/Arduino code: // Read data from the serial and turn ON or OFF a light depending on the value char val; // Data received from the serial port int ledPin = 4; // Set the pin to digital I/O 4 void setup() { pinMode(ledPin, OUTPUT); // Set pin as OUTPUT Serial.begin(9600); // Start serial communication at 9600 bps } void loop() { if (Serial.available()) { // If data is available to read, val = Serial.read(); // read it and store it in val } if (val == 'H') { // If H was received digitalWrite(ledPin, HIGH); // turn the LED on } else { digitalWrite(ledPin, LOW); // Otherwise turn it OFF } delay(100); // Wait 100 milliseconds for next reading } */