I am at chapter 6 of Getting Started with Arduino (Getting Started with Arduino Kit) and I am having problem with my Processing code. Specifically:
String arduinoPort = Serial.list()[0]; // I checked that the right port is returned
port = new Serial(this, arduinoPort, 9600); // port was previously declared as "Serial port;"
Cause this problem:
" To use the serial library, first open
Applications -> Utilities -> Terminal.app
and enter the following:
sudo mkdir -p /var/lock
sudo chmod 777 /var/lock
Exception in thread "Animation Thread" java.lang.RuntimeException: Please use Tools ? Fix the Serial Library.
at processing.serial.Serial.(Serial.java:153)
at processing.serial.Serial.(Serial.java:116)
at example_8a_lamp_sketch_processing.setup(example_8a_lamp_sketch_processing.java:67)
at processing.core.PApplet.handleDraw(PApplet.java:2103)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
at processing.core.PApplet.run(PApplet.java:2006)
at java.lang.Thread.run(Thread.java:680)
"
(the rest of the code is here: ch06/Example_08A · master · examples / Getting Started with Arduino 2nd Edition · GitLab)
To use the serial library, first open
Applications -> Utilities -> Terminal.app
and enter the following:
sudo mkdir -p /var/lock
sudo chmod 777 /var/lock
String arduinoPort = Serial.list()[0]; // I checked that the right port is returned
port = new Serial(this, arduinoPort, 9600); // port was previously declared as "Serial port;"
Is quite poor.
This is what I use to talk to an arduino through a Mac. Note that the line:-
String adaptor= "/dev/tty.usbmodem1a21";
must be changed to match what ever your arduino shows up as.
import processing.serial.*;
Serial scope;
String adaptor= "/dev/tty.usbmodem1a21";
void portConnect(){
int portNumber = 99;
String [] ports;
println(Serial.list());
ports = Serial.list();
for(int j = 0; j< ports.length; j++) {
if(adaptor.equals(Serial.list()[j])) portNumber = j;
}
if(portNumber == 99) portNumber = 0;
String portName = Serial.list()[portNumber];
println("Connected to "+portName);
scope = new Serial(this, portName, 9600);
scope.bufferUntil(10);
}
void serialEvent(Serial scope) { // this gets called every time a line feed is received
String recieved = scope.readString() ;
println(recieved + " from serial port"); // show it at the bottom of the processing window
// also do the stuff you want to do when you get things back from the arduino
}
void setup() {
size(200, 200);
portConnect();
}
void draw(){
}
void keyPressed() {
print(key);
delay(10);
switch (key) {
case 'e':
case 'E':
scope.write('e');
break;
case 'i':
case 'I':
scope.write('i');
break;
case 'q':
case 'Q':
scope.write('q');
break;
}
};
I'll respectfully disagree. Having the Processing sketch enumerate the serial ports that it knows it can successfully talk to (Serial.list()) is a good thing. Choosing the correct item from that list ([0]) is a good thing.
Now, obviously this relies on knowing that there is at least one element in the list, and, if there is more than one, which one is the correct one.
This is what I use to talk to an arduino through a Mac. Note that the line:-...must be changed to match what ever your arduino shows up as.
The name created this way should match one of the names in the list that Serial.list() generates. If it doesn't, Processing won't be able to talk to it.
Now, obviously this relies on knowing that there is at least one element in the list, and, if there is more than one, which one is the correct one
On a Mac no two devices will be given the same name. It the device is not on the list it will default to the first device in the list.
I have found that the list on a Mac is sometimes in the order the devices were added and sometimes in the reverse order depending on the version of Processing. The "bad" way just picks the first device on the list. The way I have developed attaches a specific arduino. This is usefull when there is more than one arduino in your setup. The printing of what device was chosen is a good reminder that things are set up correctly.