Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.serial.*;
- 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;
- Serial port;
- void setup() {
- size(640, 640); //sets size of window
- frameRate(100);
- println(Serial.list()); //list com ports
- port = new Serial(this, Serial.list()[0], 19200);
- }
- void draw() {//draws window and graphics.
- if (mousePressed && (mouseButton == RIGHT)) {//sends buzzer on command.
- port.write("b");
- }
- else{
- port.write("n");//sends buzzer off command.
- }
- if (keyPressed){//any key switches mouse wheel from servoA to servoB.
- if (key == 'a'){
- ab = true;
- }
- else if (key == 'b'){
- ab = false;
- }
- }
- if (port.available()>0){//check to see if any thing sent back from arduino on serial bus.
- int ls = port.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 left mouse to toggle laser on and off. Buzzer stays on while right mouse is held.", 20, 30);
- text("Press 'a' to control Servo A with mouse wheel. Press 'b' for Servo B.", 20, 60);
- fill(150, 150, 150);
- ellipse(mouseX, mouseY, 40, 40); //outer target ring.
- if (laser) {//checks to see if arduino sent laser on command, if it did, draw dot and line to it.
- fill(255, 0, 0);
- ellipse(mouseX, mouseY, 10, 10);//red dot
- stroke(150, 100, 0);
- line(320, 640, mouseX, mouseY);//line to red dot
- }
- 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) {
- //calculate servo position from mouseX/Y:
- xpos = x/4;
- ypos = y/4;
- //output to servo (0-180):
- port.write(xpos + "x");
- port.write(ypos + "y");
- }
- void mouseClicked() {//sends laser command to arduino
- if (mouseButton == LEFT){
- port.write("l");
- }
- /*if (mouseButton == CENTER){
- if (ab == false){
- ab = true;
- }
- else if (ab == true){
- ab = false;
- }
- }*/
- }
- void mouseWheel(MouseEvent event){//sends commands to arduino to move servoA
- float e = event.getAmount();
- if(ab == true){
- if (e > 0){
- port.write("u");//send up command.
- }
- if (e < 0){
- port.write("d");//send down command.
- }
- }
- else if (ab == false){
- if (e > 0){
- port.write("a");//send left command.
- }
- if (e < 0){
- port.write("s");//send right command.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement