vulture2600

Network Mouse Control v 1.0.2 for Processing

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