/** * Bounce. * * When the shape hits the edge of the window, it reverses its direction. * * Updated 1 September 2002 */ import processing.serial.*; String portname = "/dev/tty.usbserial-A4001qa8"; Serial port; // Create object from Serial class int size = 60; // Width of the shape float xpos, ypos; // Starting position of shape float xspeed = 3.9; // Speed of the shape float yspeed = 3.1; // Speed of the shape int xdirection = 1; // Left or Right int ydirection = 1; // Top to Bottom void setup() { size(400, 400); colorMode(HSB, 255); noStroke(); frameRate(30); ellipseMode(CENTER); // draw from center out smooth(); // Set the starting position of the shape xpos = width/2; ypos = height/2; // Open the port that the board is connected to and use the same speed (19200 bps) port = new Serial(this, portname, 19200); } void draw() { if (port.available() > 0) { // If data is available, size = port.read(); // read it and store it as the new size } background(102); // Update the position of the shape xpos = xpos + ( xspeed * xdirection ); ypos = ypos + ( yspeed * ydirection ); // Test to see if the shape exceeds the boundaries of the screen // If it does, reverse its direction by multiplying by -1 int halfsize = size/2; // because we're drawing from the circle's center if (xpos + halfsize > width || xpos - halfsize < 0) { xdirection *= -1; } if (ypos + halfsize > height || ypos - halfsize < 0) { ydirection *= -1; } // Draw the shape fill(size,255,255); // we're in HSB mode, so first value is color ellipse(xpos, ypos, size, size); }