import processing.serial.*; Serial myPort; String val0, val1, val2, newline; color[] colors = {color(177, 156, 217), color(0, 255, 0)}; Block[] blocks; int blockDim = 200; int blockPad = 50; int totalX = blockDim*5 + blockPad*6; int totalY = blockDim + blockPad*2; void setup() { size(1300, 300); background(0); noStroke(); String portName = Serial.list()[0]; myPort = new Serial(this, portName, 9600); blocks = new Block[5]; blocks[0] = new Block(blockPad, blockPad); blocks[1] = new Block(blockDim + blockPad*2, blockPad); blocks[2] = new Block(blockDim*2 + blockPad*3, blockPad); blocks[3] = new Block(blockDim*3 + blockPad*4, blockPad); blocks[4] = new Block(blockDim*4 + blockPad*5, blockPad); for (int i = 0; i < blocks.length; i++) { blocks[i].display(); } } void draw() { if (myPort.available() > 0) { val0 = myPort.readStringUntil('\t'); val0 = val0.replaceFirst("\t", ""); val1 = myPort.readStringUntil('\t'); val1 = val1.replaceFirst("\t", ""); val2 = myPort.readStringUntil('\t'); val2 = val2.replaceFirst("\t", ""); newline = myPort.readStringUntil('\n'); } if ((val0 != null) && (val1 != null) && (val2 != null)) { int mag0 = int(val0); int mag1 = int(val1); int mag2 = int(val2); int result = readBlock(mag0, mag1, mag2); if (result == -1) { for (int i = 0; i < blocks.length; i++) { if (blocks[i].activated) { blocks[i].display(); } } } else { if (!blocks[result].activated) { blocks[result].activate(); } } } delay(500); } int readBlock(int mag0, int mag1, int mag2) { if ((mag0 > 800) && (mag1 <= 800) && (mag2 <= 800)) { return 0; } else if ((mag0 <= 800) && (mag1 > 800) && (mag2 <= 800)) { return 1; } else if ((mag0 <= 800) && (mag1 <= 800) && (mag2 > 800)) { return 2; } else if ((mag0 > 800) && (mag1 > 800) && (mag2 <= 800)) { return 3; } else if ((mag0 <= 800) && (mag1 > 800) && (mag2 > 800)) { return 4; } else { return -1; } } class Block { color c; float xpos; float ypos; float dim; boolean activated; Block(int _xpos, int _ypos) { c = colors[0]; xpos = _xpos; ypos = _ypos; dim = blockDim; activated = false; } void display() { activated = false; fill(c); rect(xpos, ypos, dim, dim); } void activate() { activated = true; fill(colors[1]); rect(xpos, ypos, dim, dim); } }