vulture2600

Network Mouse Control v 1.0.2 for Processing

Jan 10th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1. /* Network mouse control of laser pointer.
  2. This sketch uses mouse to send commands over network to Arduino.
  3.  
  4. Version 1.0 created on 10/30/2013
  5. Current Version 1.0
  6.  
  7. Code adapted from sketches found at http://www.instructables.com/id/Overview/
  8.  
  9. Revisions:
  10. V1.0.1 Changed port to 5000 to see if lag is reduced. 11/2/13
  11. V1.0.2 Added door sensor and yellow LED. Modified and corrected crosshair. 11/28/13.
  12.  
  13. Arduino pin layout is as follows:
  14. 0):
  15. 1):
  16. 2): Garage door sensor.
  17. 3): Servo X.
  18. 4): SD Card CS.
  19. 5): Servo Y.
  20. 6): Servo A, not connected.
  21. 7): Buzzer.
  22. 8): Laser.
  23. 9): Servo B, not connected.
  24. 10-13): Ethernet shield.
  25. A4): Yellow connection status LED.
  26. A5): Garage door open status LED.
  27. */
  28.  
  29.  
  30. import processing.net.*;
  31.  
  32. Client myClient;
  33. int xpos = 90;
  34. int ypos = 90;
  35. int a = 320; // position of green servoA position bar on left side of screen.
  36. int b = 320; //position of red servoB position bar on bottom of screen.
  37. boolean laser = false;
  38. boolean ab = true;
  39.  
  40. void setup() {
  41.   size(640, 640); //sets size of window
  42.   frameRate(75);
  43.  
  44.   myClient = new Client(this, "192.168.1.176", 5000); //internal
  45. }
  46.  
  47. void draw() { //draws window and graphics.
  48.   if (mousePressed && (mouseButton == RIGHT)) { //sends buzzer on command while pressed.
  49.     myClient.write("b");
  50.   }
  51.   else {
  52.     myClient.write("n"); //sends buzzer off command.
  53.   }
  54.  
  55.   if (mousePressed && (mouseButton == LEFT)) { //sends laser on command while pressed.
  56.     myClient.write("l");
  57.   }
  58.   else {
  59.     myClient.write("k"); //sends laser off command.
  60.   }
  61.  
  62.   if (keyPressed) { //press "a" to control servoA with mouse wheel, "b" for servoB.
  63.     if (key == 'a') {
  64.       ab = true;
  65.     }
  66.     else if (key == 'b') {
  67.       ab = false;
  68.     }
  69.   }
  70.  
  71.   if (myClient.available() > 0) { //check to see if any thing sent back from arduino over network.  
  72.     int ls = myClient.read();
  73.    
  74.     switch (ls) {
  75.     case '1': //turn laser on and draw dot/line.
  76.       laser = true;
  77.       break;
  78.      
  79.     case '0': //turn laser off.
  80.       laser = false;
  81.       break;
  82.      
  83.     case '2': //move servoA position bar down.
  84.       a = a + 7;
  85.       break;
  86.      
  87.     case '3': //move servoA position bar up.
  88.       a = a - 7;
  89.       break;
  90.      
  91.     case '4': //move servoB position bar left.
  92.       b = b - 7;
  93.       break;
  94.      
  95.     case '5': //move servoB position bar right.
  96.       b = b + 7;
  97.       break;
  98.     }
  99.   }
  100.  
  101.   fill(175);
  102.   rect(0, 0, 640, 640); // grey rectangle.
  103.   fill(0, 0, 0);
  104.   textSize(12);
  105.   text("Click and hold left mouse for laser. Click and hold right mouse for buzzer.", 20, 30);
  106.   //text("Press 'a' to control Servo A with mouse wheel. Press 'b' for Servo B.", 20, 60);
  107.  
  108.   if (myClient.ip() != null) { //checks to see if there is a connection.
  109.     text("Connected.", 30, 600);
  110.   }
  111.   else {
  112.     text("Connection failed.", 30, 600);
  113.   }
  114.  
  115.   fill(150, 150, 150);
  116.   ellipse(mouseX, mouseY, 40, 40); //outer target ring.
  117.   fill(0, 0, 0);
  118.   line(mouseX - 30, mouseY, mouseX + 30, mouseY);
  119.   line(mouseX, mouseY - 30, mouseX, mouseY + 30);
  120.  
  121.   if (laser) { //checks to see if arduino sent laser on command, if it did, draw dot and line to it.
  122.     stroke(140, 0, 0);
  123.     line(320, 640, mouseX, mouseY); //line to red dot
  124.     //noStroke();
  125.     fill(255, 0, 0);
  126.     ellipse(mouseX, mouseY, 10, 10); //red dot
  127.     stroke(0, 0, 0);
  128.   }
  129.  
  130.   fill(0, 255, 0); //draws green servoA position bar
  131.   rect(0, a + 10, 20, 10);
  132.   fill(255, 255, 0); //draws servoB position bar.
  133.   rect(b + 10, 620, 10, 20);
  134.  
  135.   update(mouseX, mouseY); //sends mouse position to update() function
  136. }
  137.  
  138. void update(int x, int y) { //calculates servo position from mouseX/Y and send to arduino:
  139.   xpos = x / 4;
  140.   ypos = y / 4;
  141.   //output to servo (0-180):
  142.   myClient.write(xpos + "x");
  143.   myClient.write(ypos + "y");
  144. }
  145.  
  146. /*void mouseClicked() { //sends laser command to arduino, never worked consistently
  147.   if (mouseButton == LEFT) {
  148.     myClient.write("l");
  149.   }
  150. }
  151.  */
  152. void mouseWheel(MouseEvent event) { //sends commands to arduino to move servoA/B depending on state of boolean ab.
  153.   float e = event.getAmount();
  154.  
  155.   if (ab == true) { //activate servoA control.
  156.     if (e > 0) {
  157.       myClient.write("u"); //move servoA up.
  158.     }
  159.    
  160.     if (e < 0) {
  161.       myClient.write("d"); //move servoA down.
  162.     }
  163.   }
  164.  
  165.   else if (ab == false) { //activate servoB control.
  166.     if (e > 0) {      
  167.       myClient.write("a");
  168.     }
  169.    
  170.     if (e < 0) {
  171.       myClient.write("s");
  172.     }
  173.   }
  174. }
Add Comment
Please, Sign In to add comment