Advertisement
Guest User

Processing Code

a guest
May 17th, 2014
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. /* The following Processing Sketch was created by ScottC on
  2. the 10 Nov 2012 : http://arduinobasics.blogspot.com/
  3.  
  4. Inspired by this Processing sketch by Daniel Shiffman:
  5. http://processing.org/learning/basics/sinewave.html
  6.  
  7. */
  8. import processing.serial.*;
  9.  
  10.  
  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
  14.  
  15. //Global Variables
  16. Square[] mySquares = new Square[numOfShapes];
  17. int shapeSize, distance;
  18. String comPortString;
  19. Serial myPort;
  20.  
  21. /* -----------------------Setup ---------------------------*/
  22. void setup(){
  23. size(displayWidth,displayHeight); //Use entire screen size.
  24. smooth(); // draws all shapes with smooth edges.
  25.  
  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);
  30. }
  31.  
  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, "COM8", 9600);
  35. myPort.bufferUntil('\n'); // Trigger a SerialEvent on new line
  36. }
  37.  
  38. /* ------------------------Draw -----------------------------*/
  39. void draw(){
  40. background(0); //Make the background BLACK
  41. delay(50); //Delay used to refresh screen
  42. drawSquares(); //Draw the pattern of squares
  43. }
  44.  
  45.  
  46. /* ---------------------serialEvent ---------------------------*/
  47. void serialEvent(Serial cPort){
  48. comPortString = cPort.readStringUntil('\n');
  49. if(comPortString != null) {
  50. comPortString=trim(comPortString);
  51.  
  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));
  58. if(distance<0){
  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. */
  62. distance = 0;
  63. }
  64. }
  65. }
  66.  
  67.  
  68. /* ---------------------drawSquares ---------------------------*/
  69. void drawSquares(){
  70. int oldY, newY, targetY, redVal, blueVal;
  71.  
  72. /* Set the Y position of the 1st square based on
  73. sensor value received */
  74. mySquares[0].setY((height-shapeSize)-distance);
  75.  
  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();
  81.  
  82. if(abs(oldY-targetY)<2){
  83. newY=targetY; //This helps to line them up
  84. }else{
  85. //calculate the new position of the square
  86. newY=oldY-((oldY-targetY)/shapeSpeed);
  87. }
  88. //Set the new position of the square
  89. mySquares[i].setY(newY);
  90.  
  91. /*Calculate the colour of the square based on its
  92. position on the screen */
  93. blueVal = int(map(newY,0,height,0,255));
  94. redVal = 255-blueVal;
  95. fill(redVal,0,blueVal);
  96.  
  97. /* Draw the square on the screen */
  98. rect(mySquares[i].getX(), mySquares[i].getY(),shapeSize,shapeSize);
  99. }
  100. }
  101.  
  102. /* ---------------------sketchFullScreen---------------------------*/
  103. // This puts processing into Full Screen Mode
  104. boolean sketchFullScreen() {
  105. return true;
  106. }
  107.  
  108. /* ---------------------CLASS: Square ---------------------------*/
  109. class Square{
  110. int xPosition, yPosition;
  111.  
  112. Square(int xPos, int yPos){
  113. xPosition = xPos;
  114. yPosition = yPos;
  115. }
  116.  
  117. int getX(){
  118. return xPosition;
  119. }
  120.  
  121. int getY(){
  122. return yPosition;
  123. }
  124.  
  125. void setY(int yPos){
  126. yPosition = yPos;
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement