/** * Simple Read * * Read data from the serial port and change the color of a rectangle * when a switch connected to a Wiring or Arduino board is pressed and released. * 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 int high, low; void setup() { size(200, 200); // 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]; myPort = new Serial(this, portName, 9600); } void draw() { } void serialEvent(Serial p) { int b = p.read(); if ((b & 0x01) == 0x01) high = b >> 1; else low = b >> 1; println(high + " " + low + " " + ((high * 256 + low - 5) * 5) + " " + ((high * 256 + low - 5) * 5) / 1.514); } /* // Wiring / Arduino Code // Code for sensing a switch status and writing the value to the serial port. int switchPin = 4; // Switch connected to pin 4 void setup() { pinMode(switchPin, INPUT); // Set pin 0 as an input Serial.begin(9600); // Start serial communication at 9600 bps } void loop() { if (digitalRead(switchPin) == HIGH) { // If switch is ON, Serial.print(1, BYTE); // send 1 to Processing } else { // If the switch is not ON, Serial.print(0, BYTE); // send 0 to Processing } delay(100); // Wait 100 milliseconds } */