Advertisement
vulture2600

processing serial mouse control

Oct 25th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. import processing.serial.*;
  2.  
  3. int xpos=90;
  4. int ypos=90;
  5. int a=320; // position of green servoA position bar on left side of screen.
  6. int b=320; //position of red servoB position bar on bottom of screen.
  7. boolean laser = false;
  8. boolean ab=true;
  9. Serial port;
  10.  
  11. void setup() {
  12.   size(640, 640); //sets size of window
  13.   frameRate(100);
  14.   println(Serial.list()); //list com ports
  15.   port = new Serial(this, Serial.list()[0], 19200);
  16. }
  17.  
  18. void draw() {//draws window and graphics.
  19.   if (mousePressed && (mouseButton == RIGHT)) {//sends buzzer on command.
  20.     port.write("b");
  21.   }
  22.   else{
  23.     port.write("n");//sends buzzer off command.
  24.   }
  25.  
  26.   if (keyPressed){//any key switches mouse wheel from servoA to servoB.
  27.     if (key == 'a'){
  28.       ab = true;
  29.     }
  30.     else if (key == 'b'){
  31.       ab = false;
  32.     }
  33.   }
  34.  
  35.   if (port.available()>0){//check to see if any thing sent back from arduino on serial bus.
  36.     int ls = port.read();
  37.     switch(ls){
  38.       case '1'://turn laser on and draw dot/line.
  39.         laser = true;
  40.         break;
  41.       case '0': // turn laser off.
  42.         laser = false;
  43.         break;
  44.       case '2'://move servoA position bar down.
  45.         a = a + 7;
  46.         break;
  47.       case '3'://move servoA position bar up.
  48.         a = a - 7;
  49.         break;
  50.       case '4'://move servoB position bar left.
  51.         b = b - 7;
  52.         break;
  53.       case '5'://move servoB position bar right.
  54.         b = b + 7;
  55.         break;        
  56.     }
  57.   }
  58.   fill(175);
  59.   rect(0, 0, 640, 640);// grey rectangle.
  60.   fill(0,0,0);
  61.   textSize(12);
  62.   text("Click left mouse to toggle laser on and off. Buzzer stays on while right mouse is held.", 20, 30);
  63.   text("Press 'a' to control Servo A with mouse wheel. Press 'b' for Servo B.", 20, 60);
  64.   fill(150, 150, 150);
  65.   ellipse(mouseX, mouseY, 40, 40); //outer target ring.
  66.  
  67.   if (laser) {//checks to see if arduino sent laser on command, if it did, draw dot and line to it.
  68.     fill(255, 0, 0);
  69.     ellipse(mouseX, mouseY, 10, 10);//red dot
  70.     stroke(150, 100, 0);
  71.     line(320, 640, mouseX, mouseY);//line to red dot
  72.   }
  73.   fill(0, 255, 0); //draws green servoA position bar
  74.   rect(0, a+10, 20, 10);
  75.   fill(255, 255, 0);//draws servoB position bar.
  76.   rect(b+10, 620, 10, 20);
  77.   update(mouseX, mouseY);//sends mouse position to update() function
  78. }
  79. void update(int x, int y) {
  80.   //calculate servo position from mouseX/Y:
  81.   xpos = x/4;
  82.   ypos = y/4;
  83.   //output to servo (0-180):
  84.   port.write(xpos + "x");
  85.   port.write(ypos + "y");
  86. }
  87. void mouseClicked() {//sends laser command to arduino
  88.   if (mouseButton == LEFT){
  89.     port.write("l");
  90.   }
  91.   /*if (mouseButton == CENTER){
  92.     if (ab == false){
  93.       ab = true;
  94.     }
  95.     else if (ab == true){
  96.       ab = false;
  97.     }
  98.   }*/
  99. }
  100.  
  101. void mouseWheel(MouseEvent event){//sends commands to arduino to move servoA
  102.   float e = event.getAmount();
  103.   if(ab == true){
  104.     if (e > 0){
  105.       port.write("u");//send up command.
  106.     }
  107.     if (e < 0){
  108.       port.write("d");//send down command.
  109.     }
  110.   }
  111.   else if (ab == false){
  112.     if (e > 0){
  113.       port.write("a");//send left command.
  114.     }
  115.     if (e < 0){
  116.       port.write("s");//send right command.
  117.     }
  118.   }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement