Controlling servos with arduino problem
in
Integration and Hardware
•
3 years ago
Hey im using this tutorial
http://marco.guardigli.it/2010/01/arduinopc-two-axis-controlled-laser-gun.html
to make a pan/tilt servo thingy, but im running into some problems. I have windows 7 64bit.
Problem:
BLACK WINDOW AT THE BOTTOM:
ArrayIndexOutOfBoundExeption: 3
processing.app.debug.RunnerException: ArrayIndexOutOfBoundsException: 3
at processing.app.Sketch.placeException(Sketch.java:1543)
at processing.app.debug.Runner.findException(Runner.java:582)
at processing.app.debug.Runner.reportException(Runner.java:558)
at processing.app.debug.Runner.exception(Runner.java:498)
at processing.app.debug.EventThread.exceptionEvent(EventThread.java:367)
at processing.app.debug.EventThread.handleEvent(EventThread.java:255)
at processing.app.debug.EventThread.run(EventThread.java:89)
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 3
at lasser.setup(lasser.java:73)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)
CODE:
//
// 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
//
// 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());
// IMPORTANT! This code will not work if you do not write the correct //***************************************************
// id of the serial interface in the next line (in my case 3)
arduino = new Arduino(this, Arduino.list()[3], 57600); // <<<<<<processing hilights this area
arduino.analogWrite(9,initialservox); // when the problem occours
arduino.analogWrite(10,initialservoy);
arduino.analogWrite(11,laseroff); // laser off//**********************************************************************************
// put in the next line the file name of your ambient picture
// taken placing the camera in the place where the lasergun is
// so to have a correct perspective on the screen
room = loadImage("myplace.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 ( !!! )
// so I had to split the calculation int two statements
dzx = dzx / (cx2 - cx1); // dzx is now "how much servo per pixel"
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) );
minservox = int(leftx);
maxservox = int(rightx);
minservoy = int(upy);
maxservoy = int(downy);
} 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
****************END***************
Also, when I run the code, I hear the computer disconect and connect USB sound a few times, and the arduino TR&TX
lights flash.
thanks
1