and it runs beautifully on Processing 2.2.1, except that when I press the button marked “connect to arduino”, it produces the following error message, which has me totally baffled:
################ EXCEPTION IN EVENT HANDLER ################
An error occured during execution of the eventhandler:
CLASS: knitty_main METHOD: handleButtonEvents
Caused by java.lang.ArrayIndexOutOfBoundsException: 1
knitty_main.initSerialCommunication(knitty_main.java:1987)
knitty_main.handleButtonEvents(knitty_main.java:1110)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
g4p_controls.GAbstractControl.fireEvent(Unknown Source)
g4p_controls.GButton.mouseEvent(Unknown Source)
g4p_controls.GWindowInfo.mouseEvent(Unknown Source)
sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1236)
processing.core.PApplet.handleMethods(PApplet.java:1431)
processing.core.PApplet.handleMouseEvent(PApplet.java:2826)
processing.core.PApplet.dequeueEvents(PApplet.java:2725)
processing.core.PApplet.handleDraw(PApplet.java:2397)
processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
processing.core.PApplet.run(PApplet.java:2256)
java.lang.Thread.run(Unknown Source)
##############################################################
The “handleButtonEvents” seems to work for all the other buttons, so I have been looking at the initSerialConnection function, which is reproduced here:
import drop.*; // Added this library
import test.*;
// Don't forget to place the libraries under sketchbook/libraries/G4P and sketchbook/libraries/sDrop
import g4p_controls.*; // http://sourceforge.net/projects/g4p/
//import sojamo.drop.*; // http://www.sojamo.de/libraries/drop/ // Commented this!
There is firmware that comes with Knitty:
Steps:
Uploaded one of the Knitting Machine (Arduino firmware) selections to Arduino.
It basically lists the methods executed just before the exception was encountered. I have only shown twelve lines because that is the interesting part.
Line 12 is the method called to test whether an event occurred on a G4P button. In line 11 we see that the fireEvent method is called which will invoke (execute) the event handler. Lines 7, 8 and 9 are Java functions which locate the event handler (handleButtonEvents) and execute it.
Line 6 is calling the event handler where you check which button is clicked and executes your code.
Line 5 is calling your method initSerialCommunication and line 4 shows the cause of the exception, in this case an attempt to access an array element that doesn’t exist. So now we are back at your code
One last thing all G4P controls have a tag attribute which can be used to help debug your code. For instance immediately after creating a button called my_button add a statement to give a tag value e,g,
my_button.tag = "Serial button clicked"; // or whatever you want
If you give all your buttons different tag values then you can print the tag value when a problem occurs.