import processing.serial.*; private static final String PORT = "COM9"; //For serial communication and transformation between counter and cm. Serial myPort; int measure = 0; int byte1, byte2, byte3, byte4, low, high; float min = 512.0; int max = 1023; PImage fish; PImage lolli; float mouthX= 500; float mouthY=350; float lolliXinit=10; float lolliYinit= 560; float fishXinit=690; float fishYinit=560; float lolliX=lolliXinit; float lolliY=lolliYinit; float fishX=fishXinit; float fishY=fishYinit; boolean on = true; public void setup(){ //Setup serial communication String portName = Serial.list()[0]; println("Port Name "+ portName); myPort = new Serial(this, PORT, 9600); //setup window // Background is light gray background(200); // Window's drawing area is 800x600 pixels (XxY) //size(800, 600); size(1000,750); //images fish = loadImage("fish.png"); lolli = loadImage("lollipop.png"); } public void draw(){ println("MeSURE is "+ measure); // Use thick black borders on shapes stroke(0); strokeWeight(5); // Use white fill fill(255); background(200); drawFace(); drawMouth(); drawThings(); } /* * Draw a smiley face in the middle of the window */ void drawFace(){ // Large circle centered at 500,300 ellipse(500,300,400,400); // Two small black-filled ellipses fill(0); ellipse(425,225,25,60); ellipse(575,225,25,60); } /* * Draw a mouth as an arc, with variable height */ void drawMouth(){ // Black-filled arc (part of an ellipse) float height = (measure/min)*100; println("measure/min is "+ measure/min); println("height is "+ height); arc(mouthX,mouthY,150,height,0,PI,CHORD);//arc (x,y,w,h,anglestart,anglestop, mode) } /* * Put the images in certain positions, which are variable */ void drawThings(){ //initial:lower-left corner image(lolli, lolliX, lolliY, width/4, height/4);//image(img, x, y, width, height); //initiallower-right corner image(fish, fishX, fishY, width/4, height/4); } /* *Called on moused pressed */ void mousePressed(){ println("MOUSE PRESSED"); if(on){ myPort.write(0); on = false; println("MOUSE CLICKED to 0"); } else{ println("MOUSE CLICKED to 1"); myPort.write(1); on = true; } myPort.write(10); } void serialEvent(Serial p){ byte1 = byte2; byte2 = byte3; byte3 = byte4; byte4 = low; low = high; high = p.read(); if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 ==4)){ //Reset the preamble. byte1=byte2=byte3=byte4=0; int counter = ((256*high)+low); updateValue(counter); } } /* *Function called in serialEvent to update the images position */ void updateValue(int value){ measure = value; if(0 < measure&& measure<= 512){ //FISH CLOSE lolliX=lolliXinit; lolliY=lolliYinit; fishX=fishXinit+(((mouthX-fishXinit)/511)*(511-measure)); fishY=fishYinit+(((mouthY-fishYinit)/511)*(511-measure)); } else if(512 < measure&& measure<= 1023){ //LOLLIPOP CLOSE fishX=fishXinit; fishY=fishYinit; lolliX=lolliXinit-(((mouthX-lolliXinit)/511)*(511-measure)); lolliY=lolliYinit-(((mouthY-lolliYinit)/511)*(511-measure)); println("lolliX is "+ lolliX); println("lolliY is "+ lolliY); } }