problem with Processing sketch page 126 ofArduino Cookbook 2nd Editon
in
Integration and Hardware
•
7 months ago
I have an Arduino UNO. I was able to write the program for Serial Mouse sketch shown on that page and it seemed to be ok and I was able to download it to the board. However when I tried to create the processing sketch it doesn't want to complete. It says:
Can not find a class or type named "Dimension"
here is the entire processing code:
// Processing Sketch
/*
* ArduinoMouse.pde (Processing sketch)
*/
/* WARNING: this sketch takes over your mouse
* Press escape to close running sketch
*/
import java.awt.AWTException;
import java.awt.Robot;
import processing.serial.*;
Serial myPort; // Create object from Serial class
arduMouse myMouse; // Craete arduino controlled mouse
public static final short LF = 10; // ASCII linefeed
public static final short portIndex = 1; // select the com port,
// 0 is the first port
int posX, posY, btn; // data from msg fields will be stored here
void setup() {
size(200, 200);
println(Serial.list());
println(" Connecting to -> " + Serial.list()[portIndex]);
myPort = new Serial(this,Serial.list()[portIndex], 9600);
myMouse = new arduMouse();
btn = 0; // turn mouse off until requested by Arduino message
}
void draw() {
if ( btn != 0)
myMouse.move(posX, posY); // move mouse to recieve x and y position
}
void serialEvent(Serial p) {
String message = myPort.readStringUntil(LF); // read serial data
if ( message != null)
{
//print(message);
String [] data = message.split(","); // Split teh comma-seperated meassage
if ( data[0].equals("Data"))// check for data header
{
if( data.length > 3 )
{
try {
posX = Integer.parseInt(data[1]);
posY = Integer.parseInt(data[2]);
btn = Integer.parseInt(data[3]);
}
catch (Throwable t) {
println("."); // parse error
print(message);
}
}
}
}
}
class arduMouse {
Robot myRobot; // create object from Robot class;
static final short rate = 4; // multiplier to adjust movement rate
int centerX, centerY;
arduMouse() {
try {
myRobot = new Robot();
}
catch (AWTException e) {
e.printStackTrace();
}
Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
centerY = (int)screen.getHeight() / 2;
centerX = (int)screen.getWidth() / 2;
}
// method to move mouse from center of screen by given offset
void move(int offsetX, int offsetY) {
myRobot.mouseMove(centerX + (rate* offsetX), centerY - (rate * offsetY));
}
}
here is the entire processing code:
// Processing Sketch
/*
* ArduinoMouse.pde (Processing sketch)
*/
/* WARNING: this sketch takes over your mouse
* Press escape to close running sketch
*/
import java.awt.AWTException;
import java.awt.Robot;
import processing.serial.*;
Serial myPort; // Create object from Serial class
arduMouse myMouse; // Craete arduino controlled mouse
public static final short LF = 10; // ASCII linefeed
public static final short portIndex = 1; // select the com port,
// 0 is the first port
int posX, posY, btn; // data from msg fields will be stored here
void setup() {
size(200, 200);
println(Serial.list());
println(" Connecting to -> " + Serial.list()[portIndex]);
myPort = new Serial(this,Serial.list()[portIndex], 9600);
myMouse = new arduMouse();
btn = 0; // turn mouse off until requested by Arduino message
}
void draw() {
if ( btn != 0)
myMouse.move(posX, posY); // move mouse to recieve x and y position
}
void serialEvent(Serial p) {
String message = myPort.readStringUntil(LF); // read serial data
if ( message != null)
{
//print(message);
String [] data = message.split(","); // Split teh comma-seperated meassage
if ( data[0].equals("Data"))// check for data header
{
if( data.length > 3 )
{
try {
posX = Integer.parseInt(data[1]);
posY = Integer.parseInt(data[2]);
btn = Integer.parseInt(data[3]);
}
catch (Throwable t) {
println("."); // parse error
print(message);
}
}
}
}
}
class arduMouse {
Robot myRobot; // create object from Robot class;
static final short rate = 4; // multiplier to adjust movement rate
int centerX, centerY;
arduMouse() {
try {
myRobot = new Robot();
}
catch (AWTException e) {
e.printStackTrace();
}
Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
centerY = (int)screen.getHeight() / 2;
centerX = (int)screen.getWidth() / 2;
}
// method to move mouse from center of screen by given offset
void move(int offsetX, int offsetY) {
myRobot.mouseMove(centerX + (rate* offsetX), centerY - (rate * offsetY));
}
}
1