// 2009 mlab.uiah.fi/paja // Processing <-> Arduino with SimpleMessageSystem library /* Protocal using SimpleMessageSystem Arduino library. Each string needs to end with carrige return (13 in ASCII) Read analog values: r a [a/d] [pin] Read digital pins: r d [pin] Write digital pin: w d [pin] [value] Write analog pins: w a [pin] [value] */ import processing.serial.*; Serial myPort; String valueStr; String portnumStr; float valueFloat = 0; HScrollbar slider; void setup(){ // set window size size(400, 300); // create a font with the second font available to the system: PFont myFont = createFont(PFont.list()[2], 14); textFont(myFont); // show avialable ports println(Serial.list()); // open a serial port with 9600 baude rate portnumStr = Serial.list()[1]; myPort = new Serial(this, portnumStr, 9600); // buffer until carrige return (13 in ASCII) myPort.bufferUntil(10); // setup for line for incoming value stroke(255); // setup the slider for outgoing value slider = new HScrollbar(0, 200, width, 10, 1); } void draw(){ // clear the screen background(100); // setup text color; fill(255, 255, 255); // send a command to read analog values myPort.write("r a d 7"); myPort.write(13); // update incoming values from the serial if(valueStr != null){ // convert the value valueFloat = map(float(valueStr), 1000, 0, 0, width); } // Send values to the serial port myPort.write("w a 9 "); // Note that send value as a string int i = int(slider.getPos()); myPort.write(str(i)); myPort.write(13); // Print values on the screen text("Serial port: " + portnumStr, 10, 50); text("From serial port: " + int(valueFloat), 10, 110); text("To serial port: \t\t" + i, 10, 130); // update the line line(0, valueFloat, width, valueFloat); // update the slider slider.update(); slider.display(); } void serialEvent(Serial p){ String bufferStr = myPort.readString(); if (bufferStr != null){ int l = bufferStr.length(); valueStr = bufferStr.substring(0, l-1); println(valueStr); } } //////////////////////////// // Slider class //////////////////////////// class HScrollbar { int swidth, sheight; // width and height of bar int xpos, ypos; // x and y position of bar float spos, newspos; // x position of slider int sposMin, sposMax; // max and min values of slider int loose; // how loose/heavy boolean over; // is the mouse over the slider? boolean locked; float ratio; HScrollbar (int xp, int yp, int sw, int sh, int l) { swidth = sw; sheight = sh; int widthtoheight = sw - sh; ratio = (float)sw / (float)widthtoheight; xpos = xp; ypos = yp-sheight/2; spos = xpos + swidth/2 - sheight/2; newspos = spos; sposMin = xpos; sposMax = xpos + swidth - sheight; loose = l; } void update() { if(over()) { over = true; } else { over = false; } if(mousePressed && over) { locked = true; } if(!mousePressed) { locked = false; } if(locked) { newspos = constrain(mouseX-sheight/2, sposMin, sposMax); } if(abs(newspos - spos) > 1) { spos = spos + (newspos-spos)/loose; } } int constrain(int val, int minv, int maxv) { return min(max(val, minv), maxv); } boolean over() { if(mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight) { return true; } else { return false; } } void display() { fill(255); rect(xpos, ypos, swidth, sheight); if(over || locked) { fill(153, 102, 0); } else { fill(102, 102, 102); } rect(spos, ypos, sheight, sheight); } float getPos() { // Convert spos to be values between // 0 and the total width of the scrollbar return spos * ratio; } }