// // 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; int maxy; int calibrating = 0; int int int int int // windows sizes // nonzero during calibration, states are 0,1,2

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

float dzx, dzy; int int int int int int int int int maxservox=170; // maximum servo excursions - to be redefined in recalibration minservox=10; maxservoy=170; minservoy=10; initialservox = 90; initialservoy = 90; servox, servoy; // current servos positions -in servo rangelaseroff = 0; // laser is controlled (improperly) as a servo laseron = 170; // zero is not actually turned off, but is dimmer // room background (photo shot from laser initial

PImage room; 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) { arduino.analogWrite(11,laseron); stroke(200,200,0); fill(200,0,0); ellipse(mouseX,mouseY,5,5); delay(500); ellipse(mouseX,mouseY,10,10); arduino.analogWrite(11,laseroff); } } }

// draw shot on screen // and intensify laser

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 float float float leftx = calibrateminx - ( dzx * cx1 ); rightx = calibratemaxx + ( dzx * (maxx-cx2) ); upy = calibrateminy - ( dzy * cy1 ); 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(" println(dzx); // print(" calibrateminy:"); print(calibrateminy); print(" println(dzy); minservox = maxservox = minservoy = maxservoy = // print("2println(maxservox); // print("2println(maxservoy); int(leftx); int(rightx); int(upy); int(downy); misx: "); print(minservox); print(" masx: "); misy: "); print(minservoy); print(" masy: ");

dzx="); dzy=");

} 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

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.