/** * 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 square_size = 40; int square_gap = 4; int square_space = (square_size + square_gap); int max_distance = 650; int max_color = 650; void setup() { size(360, 360); colorMode(HSB, 1000); // 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. print(Serial.list()); String portName = Serial.list()[0]; myPort = new Serial(this, portName, 115200); } boolean record_started=false; String data=""; void draw() { if ( myPort.available() > 0) { // If data is available, String inBuffer = myPort.readString(); String[] lines = inBuffer.split("\n"); for(int index = 0; indexdistance:")){ data = lines[index]; record_started=true; continue; } if(record_started){ data += lines[index]; if(count(data) >= 64){ record_started=false; display_my_data(data); } } } } } int count(String line){ return line.length() - line.replace(",", "").length(); } void display_my_data(String data){ String distances[] = data.split(":")[1].split(","); print(distances[0]); for(int row=0; row < 8; row++){ for(int col=0; col < 8; col++){ int distance = int(distances[row+8*col]); if( distance >max_distance)distance = max_distance; fill(color(distance * max_color/ max_distance, 1000, 1000)); square(row * square_space + square_gap, col * square_space + square_gap, square_size); } } } /* // 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.write(1); // send 1 to Processing } else { // If the switch is not ON, Serial.write(0); // send 0 to Processing } delay(100); // Wait 100 milliseconds } */