1 /* The following Processing Sketch was created by ScottC on
2 the 10 Nov 2012 : http://arduinobasics.blogspot.com/
4 Inspired by this Processing sketch by Daniel Shiffman:
5 http://processing.org/learning/basics/sinewave.html
8 import processing.serial.*;
11 int numOfShapes = 60; // Number of squares to display on screen
12 int shapeSpeed = 2; // Speed at which the shapes move to new position
13 // 2 = Fastest, Larger numbers are slower
16 Square[] mySquares = new Square[numOfShapes];
17 int shapeSize, distance;
21 /* -----------------------Setup ---------------------------*/
23 size(displayWidth,displayHeight); //Use entire screen size.
24 smooth(); // draws all shapes with smooth edges.
26 /* Calculate the size of the squares and initialise the Squares array */
27 shapeSize = (width/numOfShapes);
28 for(int i = 0; i<numOfShapes; i++){
29 mySquares[i]=new Square(int(shapeSize*i),height-40);
32 /*Open the serial port for communication with the Arduino
33 Make sure the COM port is correct - I am using COM port 8 */
34 myPort = new Serial(this, "/dev/ttyACM2", 9600);
35 myPort.bufferUntil('\n'); // Trigger a SerialEvent on new line
38 /* ------------------------Draw -----------------------------*/
40 background(0); //Make the background BLACK
41 delay(50); //Delay used to refresh screen
42 drawSquares(); //Draw the pattern of squares
46 /* ---------------------serialEvent ---------------------------*/
47 void serialEvent(Serial cPort){
48 comPortString = cPort.readStringUntil('\n');
49 if(comPortString != null) {
50 comPortString=trim(comPortString);
52 /* Use the distance received by the Arduino to modify the y position
53 of the first square (others will follow). Should match the
54 code settings on the Arduino. In this case 200 is the maximum
55 distance expected. The distance is then mapped to a value
56 between 1 and the height of your screen */
57 distance = int(map(Integer.parseInt(comPortString),1,200,1,height));
59 /*If computer receives a negative number (-1), then the
60 sensor is reporting an "out of range" error. Convert all
61 of these to a distance of 0. */
68 /* ---------------------drawSquares ---------------------------*/
70 int oldY, newY, targetY, redVal, blueVal;
72 /* Set the Y position of the 1st square based on
73 sensor value received */
74 mySquares[0].setY((height-shapeSize)-distance);
76 /* Update the position and colour of each of the squares */
77 for(int i = numOfShapes-1; i>0; i--){
78 /* Use the previous square's position as a target */
79 targetY=mySquares[i-1].getY();
80 oldY=mySquares[i].getY();
82 if(abs(oldY-targetY)<2){
83 newY=targetY; //This helps to line them up
85 //calculate the new position of the square
86 newY=oldY-((oldY-targetY)/shapeSpeed);
88 //Set the new position of the square
89 mySquares[i].setY(newY);
91 /*Calculate the colour of the square based on its
92 position on the screen */
93 blueVal = int(map(newY,0,height,0,255));
95 fill(redVal,0,blueVal);
97 /* Draw the square on the screen */
98 rect(mySquares[i].getX(), mySquares[i].getY(),shapeSize,shapeSize);
102 /* ---------------------sketchFullScreen---------------------------*/
103 // This puts processing into Full Screen Mode
104 boolean sketchFullScreen() {
108 /* ---------------------CLASS: Square ---------------------------*/
110 int xPosition, yPosition;
112 Square(int xPos, int yPos){