/** * 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, lastval; // Data received from the serial port void setup() { size(200, 500); // 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); lastval=0; } void draw() { background(0); val = mouseOverRect(); if(val!=lastval) { myPort.write(val); // send an H to indicate mouse is over square lastval=val; } noFill(); stroke(255, 0, 0); rect(50, 25, 100, 100); // Draw a square stroke(255, 255, 0); rect(50, 150, 100, 100); // Draw a square stroke(0, 255, 0); rect(50, 275, 100, 100); // Draw a square } int 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 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 } */