import processing.serial.*; Serial myPort; // Create object from Serial class int val; // Data received from the serial port int state = 0; int old_state = 0; void setup() { size(200, 200); myPort = new Serial(this, "/dev/ttyACM0", 9600); } void draw() { if ( myPort.available() > 0) { // If data is available, val = myPort.read(); // read it and store it in val background(val); // Set background to white println(val); } if (mouseX > 20 && mouseX < 60 && mouseY > 20 && mouseY < 60) { fill(255,0,0); state = 1; } else { fill(255); state = 0; } if (old_state == 0 && state == 1) myPort.write('H'); if (old_state == 1 && state == 0) myPort.write('L'); rect(20,20,40,40); old_state = state; }