/************************************************* * JANI-6 * Soros porti Robotvezerlo program + * Sharp tavolsagerzekelo szenzor altal mert * ertekeket grafikusan megjelenito program * by: Fizikus 04/12/2010 *************************************************/ import processing.serial.*; Serial myPort; // Create object from Serial class int[] serialInArray = new int[2]; int val; // Data received from the serial port int numAngles=25; int szervopoz, adcertek = 0; float[] startAngs = {195, 201, 207, 213, 219, 225, 231, 237, 243, 249, 255, 261, 267, 273, 279, 285, 291, 297, 303, 309, 315, 321, 327, 333, 339}; float[] stopAngs = {201, 207, 213, 219, 225, 231, 237, 243, 249, 255, 261, 267, 273, 279, 285, 291, 297, 303, 309, 315, 321, 327, 333, 339, 345}; float[] distances = {55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55}; color currentcolor; CircleButton circle1, circle2, circle3, circle4; RectButton rect1; boolean locked = false; boolean firstContact = false; void setup() { size(900, 300); smooth(); color baseColor = color(255); currentcolor = baseColor; // I know that the first port in the serial list on my mac // is always my FTDI adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. // println(Serial.list()); // This will print a lisi of serial ports // String portName = Serial.list()[4]; myPort = new Serial(this, "COM31", 9600,'N',8,1.0); myPort.bufferUntil('\r'); // Define and create circle button color buttoncolor = color(0,0,200); color highlight = color(0,200,0); ellipseMode(CENTER); circle1 = new CircleButton(140, 60, 30, 140, 20, 180, 80, 100, 80, buttoncolor, highlight); // Define and create circle button buttoncolor = color(0,0,200); highlight = color(0,200,0); circle2 = new CircleButton(220, 140, 30, 200, 100, 260, 140, 200, 180, buttoncolor, highlight); // Define and create circle button buttoncolor = color(0,0,200); highlight = color(0,200,0); circle3 = new CircleButton(140, 220, 30, 140, 260, 180, 200, 100, 200, buttoncolor, highlight); // Define and create circle button buttoncolor = color(0,0,200); highlight = color(0,200,0); circle4 = new CircleButton(60, 140, 30, 80, 100, 80, 180, 20, 140, buttoncolor, highlight); // Define and create rectangle button buttoncolor = color(211,24,24); highlight = color(255,0,0); rect1 = new RectButton(100, 100, 80, buttoncolor, highlight); //noLoop(); } void draw() { background(currentcolor); stroke(0); update(mouseX, mouseY); circle1.display(); circle2.display(); circle3.display(); circle4.display(); rect1.display(); drawGraph(); } void update(int x, int y) { if(locked == false) { circle1.update(); circle2.update(); circle3.update(); circle4.update(); rect1.update(); } else { locked = false; } if(mousePressed) { if(circle1.pressed()) { delay(200); myPort.write(101); } else if(circle2.pressed()) { delay(200); myPort.write(106); } else if(circle3.pressed()) { delay(200); myPort.write(104); } else if(circle4.pressed()) { delay(200); myPort.write(98); } else if(rect1.pressed()) { delay(200); myPort.write(115); } } } void drawGraph () { fill(100); for (int i= 3; i < (numAngles-3); i++) { arc(600, height-20 , 6*distances[i], 6*distances[i], radians(startAngs[i]), radians(stopAngs[i])); } } void serialEvent(Serial myPort) { // if serialEvent occurs at all, contact with the microcontroller // has been made: firstContact = true; // read the serial buffer: String myString = myPort.readStringUntil('\n'); // if you got any bytes other than the linefeed: if (myString != null) { myString = trim(myString); // split the string at the commas //and convert the sections into integers: int sensors[] = int(split(myString, ',')); // if you received all the sensor strings, use them: if (sensors.length >= 2) { szervopoz = (sensors[0]); adcertek = (sensors[1]); if (adcertek >= 95) { distances[szervopoz-11]= (6400/(adcertek+1))-5; } else { distances[szervopoz-11]= 60; } println(sensors[0] + "," + sensors[1] ); } } } class Button { int x, y, u, v, l, m, o, p ; int size; color basecolor, highlightcolor; color currentcolor; boolean over = false; boolean pressed = false; void update() { if(over()) { currentcolor = highlightcolor; } else { currentcolor = basecolor; } } boolean pressed() { if(over) { locked = true; return true; } else { locked = false; return false; } } boolean over() { return true; } boolean overRect(int x, int y, int width, int height) { if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } boolean overCircle(int x, int y, int diameter) { float disX = x - mouseX; float disY = y - mouseY; if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) { return true; } else { return false; } } } class CircleButton extends Button { CircleButton(int ix, int iy, int isize, int iu, int iv, int il, int im, int io, int ip, color icolor, color ihighlight) { x = ix; y = iy; size = isize; u = iu; v = iv; l = il; m = im; o = io; p = ip; basecolor = icolor; highlightcolor = ihighlight; currentcolor = basecolor; } boolean over() { if( overCircle(x, y, size) ) { over = true; return true; } else { over = false; return false; } } void display() { stroke(0); fill(currentcolor); triangle(u, v, l, m, o, p); } } class RectButton extends Button { RectButton(int ix, int iy, int isize, color icolor, color ihighlight) { x = ix; y = iy; size = isize; basecolor = icolor; highlightcolor = ihighlight; currentcolor = basecolor; } boolean over() { if( overRect(x, y, size, size) ) { over = true; return true; } else { over = false; return false; } } void display() { stroke(0); fill(currentcolor); rect(x, y, size, size); } }