Processing tutorial: Send data from program to Arduino

import processing.serial.*;

Serial port; // The serial port

void setup() {
size(200, 200);
println(Serial.list()); // list all available serial ports
port = new Serial(this, Serial.list()[2], 19200); // initialize serial communication
}

void draw() {
background(0);
if (mouseX>=width/4 && mouseX<=3*width/4 && mouseY>=height/4 && mouseY<=3*height/4) { // if the mouse is over the square
fill(0,255,0);
port.write(1); // write a 1 to the serial port
} else {
fill(255);
port.write(0); // write a 0 to the serial port (note: this is inefficient because we'll send lots of redundant data to the serial port... try to only do it when we're going to send a new value!)
}
rect(width/4,height/4,width/2,height/2); // draw square
}

Categories: | | |