Ho provato a modificare il codice in questo modo
import processing.serial.*;
int width = 800;
int height = 600;
String inByte = null;
String a;
int lf = 10;
int button1X;
int button1Y;
int button1Width = width/10;
int button1Height = height/20;
int button2X;
int button2Y;
int button3X;
int button3Y;
int button2Width = button1Width;
int button2Height = button1Height;
int button3Width = button1Width;
int button3Height = button1Height;
float buttonFont = button1Height/1.5;
float wordFont = button1Height;
Serial arduino;
color buttonColor, buttonHighlight, buttonPressed;
PFont font;
boolean button1Over = false;
boolean button2Over = false;
boolean button3Over = false;
void setup() {
size(width,height);
smooth();
font = loadFont("CourierNew36.vlw");
buttonColor = color(255);
buttonHighlight = color(200);
buttonPressed = color(50);
button1X = width/10 - button1Width/2;
button1Y = height/5 - button1Height/2;
button2X = 2*width/10 - button1Width/2;
button2Y = height/5 - button1Height/2;
button3X = 3*width/10 - button1Width/2;
button3Y = height/5 - button1Height/2;
arduino = new Serial(this,"COM3", 9600);
arduino.bufferUntil('\n'); // SerialEvent for new line
arduino.clear();
}
void draw() {
update(mouseX,mouseY);
background(0);
if(button1Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(button1X,button1Y, button1Width, button1Height);
if(button2Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(button2X,button2Y, button2Width, button2Height);
if(button3Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(button3X,button3Y, button3Width, button3Height);
textFont(font, buttonFont);
fill(0);
text("On t",button1X + button1Width/3.9,button1Y + button1Height/1.3);
text("On nt",button2X + button1Width/10.9,button1Y + button1Height/1.3);
text("Off nt",button3X + button1Width/10.9,button1Y + button1Height/1.3);
synchronized(a){
text(a,width/2, height/2);
}
textFont(font, wordFont);
fill(255);
text("Accendi/Spegni Led ",button3X + button1Width + width/20,button1Y + button1Height/1.2);
}
///////////////////////////////////
void update(int x, int y) {
if(overButton(button1X,button1Y,button1Width,button1Height)){
button1Over = true;
} else {
button1Over = false;
}
if(overButton(button2X,button2Y,button2Width,button2Height)){
button2Over = true;
} else {
button2Over = false;
}
if(overButton(button3X,button3Y,button3Width,button3Height)){
button3Over = true;
} else {
button3Over = false;
}
}
void mousePressed() {
if(button1Over) {
println("Button1 pressed");
arduino.write('S');
fill(buttonPressed);
rect(button1X,button1Y, button1Width, button1Height);
}
if(button2Over) {
println("Button2 pressed");
arduino.write('N');
fill(buttonPressed);
rect(button2X,button2Y, button2Width, button2Height);
}
if(button2Over) {
println("Button3 pressed");
arduino.write('Z');
fill(buttonPressed);
rect(button3X,button3Y, button3Width, button3Height);
}
}
boolean overButton(int x, int y, int width, int height) {
if(mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
void serialEvent (Serial arduino) {
inByte = arduino.readStringUntil(lf); // Receive one reading from Arduino
if (inByte != null) {
synchronized(a){
a = inByte;
}
println(a);
}
}
leggendo anche un pò il codice di http://scuola.arduino.cc/en/content/how-create-graphical-interface-tinkerkit-gyro-module
con risultato una schermata grigia senza nulla all'interno.
In processing mi da questi errori:
Exception in thread "Animation Thread" java.lang.NullPointerException
at sketch_130203abbbbbb.draw(sketch_130203abbbbbb.java:107)
at processing.core.PApplet.handleDraw(PApplet.java:2142)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:193)
at processing.core.PApplet.run(PApplet.java:2020)
at java.lang.Thread.run(Thread.java:662)
error, disabling serialEvent() for //./COM3
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at processing.serial.Serial.serialEvent(Unknown Source)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Caused by: java.lang.NullPointerException
at sketch_130203abbbbbb.serialEvent(sketch_130203abbbbbb.java:173)
... 8 more
(Guarda allegato)

