Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Network mouse control of laser pointer.
- This sketch uses mouse to send commands over network to Arduino.
- Version 1.0 created on 10/30/2013
- Current Version 1.0
- steve.a.mccluskey@gmail.com
- Code adapted from sketches found at http://www.instructables.com/id/Overview/
- Revisions:
- V1.0.1 Changed port to 5000 to see if lag is reduced. 11/2/13
- V1.0.2 Added door sensor and yellow LED. Modified and corrected crosshair. 11/28/13.
- Arduino pin layout is as follows:
- 0):
- 1):
- 2): Garage door sensor.
- 3): Servo X.
- 4): SD Card CS.
- 5): Servo Y.
- 6): Servo A, not connected.
- 7): Buzzer.
- 8): Laser.
- 9): Servo B, not connected.
- 10-13): Ethernet shield.
- A4): Yellow connection status LED.
- A5): Garage door open status LED.
- */
- import processing.net.*;
- Client myClient;
- int xpos = 90;
- int ypos = 90;
- int a = 320; // position of green servoA position bar on left side of screen.
- int b = 320; //position of red servoB position bar on bottom of screen.
- boolean laser = false;
- boolean ab = true;
- void setup() {
- size(640, 640); //sets size of window
- frameRate(75);
- myClient = new Client(this, "192.168.1.176", 5000); //internal
- }
- void draw() { //draws window and graphics.
- if (mousePressed && (mouseButton == RIGHT)) { //sends buzzer on command while pressed.
- myClient.write("b");
- }
- else {
- myClient.write("n"); //sends buzzer off command.
- }
- if (mousePressed && (mouseButton == LEFT)) { //sends laser on command while pressed.
- myClient.write("l");
- }
- else {
- myClient.write("k"); //sends laser off command.
- }
- if (keyPressed) { //press "a" to control servoA with mouse wheel, "b" for servoB.
- if (key == 'a') {
- ab = true;
- }
- else if (key == 'b') {
- ab = false;
- }
- }
- if (myClient.available() > 0) { //check to see if any thing sent back from arduino over network.
- int ls = myClient.read();
- switch (ls) {
- case '1': //turn laser on and draw dot/line.
- laser = true;
- break;
- case '0': //turn laser off.
- laser = false;
- break;
- case '2': //move servoA position bar down.
- a = a + 7;
- break;
- case '3': //move servoA position bar up.
- a = a - 7;
- break;
- case '4': //move servoB position bar left.
- b = b - 7;
- break;
- case '5': //move servoB position bar right.
- b = b + 7;
- break;
- }
- }
- fill(175);
- rect(0, 0, 640, 640); // grey rectangle.
- fill(0, 0, 0);
- textSize(12);
- text("Click and hold left mouse for laser. Click and hold right mouse for buzzer.", 20, 30);
- //text("Press 'a' to control Servo A with mouse wheel. Press 'b' for Servo B.", 20, 60);
- if (myClient.ip() != null) { //checks to see if there is a connection.
- text("Connected.", 30, 600);
- }
- else {
- text("Connection failed.", 30, 600);
- }
- fill(150, 150, 150);
- ellipse(mouseX, mouseY, 40, 40); //outer target ring.
- fill(0, 0, 0);
- line(mouseX - 30, mouseY, mouseX + 30, mouseY);
- line(mouseX, mouseY - 30, mouseX, mouseY + 30);
- if (laser) { //checks to see if arduino sent laser on command, if it did, draw dot and line to it.
- stroke(140, 0, 0);
- line(320, 640, mouseX, mouseY); //line to red dot
- //noStroke();
- fill(255, 0, 0);
- ellipse(mouseX, mouseY, 10, 10); //red dot
- stroke(0, 0, 0);
- }
- fill(0, 255, 0); //draws green servoA position bar
- rect(0, a + 10, 20, 10);
- fill(255, 255, 0); //draws servoB position bar.
- rect(b + 10, 620, 10, 20);
- update(mouseX, mouseY); //sends mouse position to update() function
- }
- void update(int x, int y) { //calculates servo position from mouseX/Y and send to arduino:
- xpos = x / 4;
- ypos = y / 4;
- //output to servo (0-180):
- myClient.write(xpos + "x");
- myClient.write(ypos + "y");
- }
- /*void mouseClicked() { //sends laser command to arduino, never worked consistently
- if (mouseButton == LEFT) {
- myClient.write("l");
- }
- }
- */
- void mouseWheel(MouseEvent event) { //sends commands to arduino to move servoA/B depending on state of boolean ab.
- float e = event.getAmount();
- if (ab == true) { //activate servoA control.
- if (e > 0) {
- myClient.write("u"); //move servoA up.
- }
- if (e < 0) {
- myClient.write("d"); //move servoA down.
- }
- }
- else if (ab == false) { //activate servoB control.
- if (e > 0) {
- myClient.write("a");
- }
- if (e < 0) {
- myClient.write("s");
- }
- }
- }
Add Comment
Please, Sign In to add comment