//Simple Distance Bar - Used to display the distance measured by a ToF device //Mauro Herrero Fab Academy 2021 //Portions of code (serialEvent) Marta Verde import processing.serial.*; //load library to read from serial port float distance; // value read from serial int dispDist = floor(distance);//displayed value take rid of decimals; Serial myPort; // port opened for serial communication float barLength = map (distance, 0, 320, 0, 800);//map max displayed lenght with max measured lenght; String inString; //incoming data through myPort float [] values; //array of incoming data, comma separated values int displayFontSize = 32; // size of length display font float textY = (3*height/4) + displayFontSize; // Y position of length text void setup (){ size(800,400); //set window size myPort = new Serial(this, "/dev/ttyUSB0", 115200); //set myPort to connected dev/ttyUSB0 @115200 bauds } void serialEvent(Serial myPort) { //function used to capture data from serial inString = myPort.readStringUntil('\n'); // read incoming data up to line next line break if (inString != null){ // if there is data... inString = trim(inString); // remove whitespace characters from the beginning and end values = float(split(inString, ",")); //assemble an array of elements, of the data separated by commas if (values.length >=1) { // if the array has data... distance = values[0]; // asign data on index 0 to variable distance } } } void draw(){ // looping function background(220); //set background color fill(100,140,140);// set fill for measured bar rect(0, height/4, barLength, height/2);//draw measuring bar fill(180,220,240);//set fill for rest of bar rect(barLength, height/4, width-barLength, height/2);//draw with another color to the end fill(80,120,120);//set text color textSize(displayFontSize); if(barLength > 60 && barLength < width-60){//display text moving along measuring bar text(dispDist, barLength, textY); }else if(barLength < 60){//display text at a minimum of 60px of the edge text(dispDist, 60, textY); }else { text(dispDist, width-60, textY); } }