Advertisement
Guest User

Servo Control (Processing)

a guest
Jun 15th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. /**
  2.  * Servocontrol (derived from processing Mouse 1D example.)
  3.  *
  4.  * Updated 24 November 2007
  5.  */
  6.  
  7.  
  8. // Use the included processing code serial library
  9. import processing.serial.*;        
  10.  
  11.  
  12. int gx = 15;
  13. int gy = 35;
  14. int spos=90;
  15.  
  16. float leftColor = 0.0;
  17. float rightColor = 0.0;
  18. Serial port;                         // The serial port
  19.  
  20.  
  21.  
  22. void setup()
  23. {
  24.   size(720, 200);
  25.   colorMode(RGB, 1.0);
  26.   noStroke();
  27.   rectMode(CENTER);
  28.   frameRate(100);
  29.  
  30.   println(Serial.list()); // List COM-ports
  31.  
  32.   //select second com-port from the list
  33.   port = new Serial(this, Serial.list()[0], 57600);
  34.   println("Connected to:"+port);
  35. }
  36.  
  37. void draw()
  38. {
  39.   background(0.0);
  40.   update(mouseX);
  41.   fill(mouseX/4);
  42.   rect(150, 320, gx*2, gx*2);
  43.   fill(180 - (mouseX/4));
  44.   rect(450, 320, gy*2, gy*2);
  45. }
  46.  
  47. void update(int x)
  48. {
  49.   //Calculate servo postion from mouseX
  50.   spos= x/4;
  51.  
  52.   //Output the servo position ( from 0 to 180)
  53.   port.write(spos+"s");
  54. //println(spos+"s");
  55.  
  56.  
  57.   // Just some graphics
  58.   leftColor = -0.002 * x/2 + 0.06;
  59.   rightColor =  0.002 * x/2 + 0.06;
  60.  
  61.   gx = x/2;
  62.   gy = 100-x/2;
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement