You are on page 1of 4

//

// Processing code for interacting with arduino firmata


// to control servos connected to analog 9 and analog 10 pins
// project "lasergun" by Marco Guardigli, mgua@tomware.it�
//
// see
// http://marco.guardigli.it/2010/01/arduinopc-two-axis-controlled-laser-gun.html
//�
// this code is free software, released under the GNU GPL license
// see www.gnu.org for license details
//
// copyleft Marco Guardigli
//��� 2010 jan 11: first version
//
//

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;

int maxx; // windows sizes


int maxy;
int calibrating = 0; // nonzero during calibration, states are 0,1,2

int calibrateminx=80; // recalibration window


int calibrateminy=80;
int calibratemaxx=100;
int calibratemaxy=100;
int cx1, cy1, cx2, cy2; // screen mousex/y coords of the two calibration points

float dzx, dzy;

int maxservox=170; // maximum servo excursions - to be redefined in recalibration


int minservox=10;
int maxservoy=170;
int minservoy=10;
int initialservox = 90;
int initialservoy = 90;
int servox, servoy; // current servos positions -in servo range-
int laseroff = 0; // laser is controlled (improperly) as a servo
int laseron = 170; // zero is not actually turned off, but is dimmer

PImage room; // room background (photo shot from laser initial


position)

void setup() {
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[3], 57600);
arduino.analogWrite(9,initialservox);
arduino.analogWrite(10,initialservoy);
arduino.analogWrite(11,laseroff); // laser off
room = loadImage("office.jpg"); // put here the picture of your place
maxx = room.width; // I suggest to resize the picture to 800x600

maxy = room.height;
size(maxx,maxy);
background(120);
image(room,0,0); //show background room
}

void draw() {
switch (calibrating) {
case 0: { // no calibration in course: laser follows pointer
servox = int(map(mouseX,0,maxx,minservox,maxservox));
servoy = int(map(mouseY,0,maxy,minservoy,maxservoy));
arduino.analogWrite(9,servox);
arduino.analogWrite(10,servoy);
}
case 1: { // need to read first calibration point
cx1 = mouseX;
cy1 = mouseY;
}
case 2: { // need to read second calibration point
cx2 = mouseX;
cy2 = mouseY;
}
}
}

void mousePressed() {
if (mouseButton == LEFT) {
if (calibrating == 0) { // draw shot on screen
arduino.analogWrite(11,laseron); // and intensify laser
stroke(200,200,0);
fill(200,0,0);
ellipse(mouseX,mouseY,5,5);
delay(500);
ellipse(mouseX,mouseY,10,10);
arduino.analogWrite(11,laseroff);
}
}
}

void mouseReleased() {
if (mouseButton == RIGHT) {
switch (calibrating) {
case 0: {
calibrating = 1; // stops laser following mouse pointer
arduino.analogWrite(9,calibrateminx);
arduino.analogWrite(10,calibrateminy);
arduino.analogWrite(11,laseron); // and intensify laser

println("cx1/cy1: point mouse to where laser pointer is and RCLICK");


break;
}
case 1: { // arriving here after rclick release in calibration point 1
calibrating = 2;
arduino.analogWrite(9,calibratemaxx);
arduino.analogWrite(10,calibratemaxy);
arduino.analogWrite(11,laseron); // and intensify laser

print(" calibration point1: "); print(cx1); print(" , ");


println(cy1);
println("cx2/cy2: point mouse to where laser pointer is and RCLICK");
break;
}
case 2: { // arriving here after rclick release in calibration point 2
print(" calibration point2: "); print(cx2); print(" , ");
println(cy2);
// (cx1,cy1) corresponds to (calibrateminx, calibrateminy)
// (cx2,cy2) corresponds to (calibratemaxx, calibratemaxy)
// i will recalculate minservox, minservoy and maxservox, maxservoy
if (((cx2-cx1) != 0) && ((cy2-cy1) != 0)) {
stroke(200);
line (cx1,cy1,cx1,cy2);
line (cx1,cy2,cx2,cy2);
line (cx2,cy2,cx2,cy1);
line (cx2,cy1,cx1,cy1);

dzx = (calibratemaxx - calibrateminx); // for some reason


dzx=(calibratemaxx-calibrateminx)/(cx2-cx1) was not calculated well
// println("-------");
// print(" calibratemaxx - calibrateminx = "); println(dzx);
// print(" cx2 - cx1 ="); println(cx2-cx1);
dzx = dzx / (cx2 - cx1); // dzx is how much servo per
pixel
// print(" dzx="); println(dzx);
// println("-------");
dzy = calibratemaxy - calibrateminy;
dzy = dzy / (cy2 - cy1);

float leftx = calibrateminx - ( dzx * cx1 );


float rightx = calibratemaxx + ( dzx * (maxx-cx2) );
float upy = calibrateminy - ( dzy * cy1 );
float downy = calibratemaxy + ( dzy * (maxy-cy2) );

// print("1- misx: "); print(minservox); print(" masx: ");


println(maxservox);
// print("1- misy: "); print(minservoy); print(" masy: ");
println(maxservoy);
// print(" calibrateminx:"); print(calibrateminx); print(" dzx=");
println(dzx);
// print(" calibrateminy:"); print(calibrateminy); print(" dzy=");
println(dzy);

minservox = int(leftx);
maxservox = int(rightx);
minservoy = int(upy);
maxservoy = int(downy);
// print("2- misx: "); print(minservox); print(" masx: ");
println(maxservox);
// print("2- misy: "); print(minservoy); print(" masy: ");
println(maxservoy);

} else {
println("Invalid calibration points selected.");
}
calibrating = 0;
arduino.analogWrite(11,laseroff); // and dim laser
break;
} // end case 2
default: {
break;
} // end case default
} // end switch
} // end if mousebutton right
} // end mouseReleased

You might also like