Advertisement
3rdWard_Arduino

class4_processingCode_rotateRect

Jun 24th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. /**
  2.  * Control Ellispe Diameter with values sent from Arduino.
  3.  *
  4.  * Using an Arduino and Potentiometer, contro the diameter of an ellipse
  5.  * Recieve values using Serial I/O library
  6.  */
  7.  
  8. import processing.serial.*;// import serial library
  9.  
  10.  
  11. float diameter = 0.0; // the size of the ellipse
  12. float maxDiameter = 200.0;
  13. float minDiameter = 1.0;
  14.  
  15.  
  16.  
  17. Serial myPort; // The serial port
  18.  
  19. int dpi = 300;
  20. void setup() {
  21.   size(600, 600);
  22.   smooth();
  23.   background(0);
  24.   println(Serial.list());
  25.   myPort = new Serial(this, Serial.list()[0], 9600);
  26. }
  27.  
  28. void draw() {
  29.   rectMode(CENTER);
  30.   pushMatrix();
  31.   translate(mouseX, mouseY);
  32.   rotate(radians(diameter));
  33.   rect(0, 0, diameter, diameter);
  34.   popMatrix();
  35. }
  36. /**
  37.  * serialEvent
  38.  *
  39.  * This function access the values coming in from the serial port
  40.  * It assigns the values to the diameter of the ellipse called, diameter
  41.  * The diamter has a range of minDiamter to maxDiameter
  42.  * These values are determined by the minDiameter and maxDiameter variables
  43.  * If you alter them you can change the range of the diameter of the ellipse
  44.  * For this to work, an Arduino must be sending one byte through the serial port
  45.  */
  46. void serialEvent (Serial myPort) {
  47.   // get the byte:
  48.   int inByte = myPort.read();
  49.   diameter = map(inByte, 0, 255, minDiameter, maxDiameter);
  50.   // print it:
  51.   println(inByte);
  52. }
  53.  
  54. // This can be tested with using the keys if you are unable to access and Arduino and potentiometer
  55. // The UP key increases the speed and the DOWN key decreases the speed
  56. void keyPressed() {
  57.   if (keyCode == UP) {
  58.     diameter += 0.1;
  59.     diameter = constrain(diameter, minDiameter, maxDiameter);
  60.   }
  61.   if (keyCode == DOWN) {
  62.     diameter -= 0.1;
  63.     diameter = constrain(diameter, minDiameter, maxDiameter);
  64.   }
  65.   println("adjusted diameter with keys to: " + diameter);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement