This is the code I worked on over summer and it worked perfectly fine. Now I after a few months, it magically doesn't want to work anymore. The following is my ARDUINO code:
[code]int ledPin;
int ledPin13 = 13;
int ledPin12 = 12;
int ledPin11 = 11;
int sensorPin = A2;
int sensorValue = 0;
int analogPin = 0;
char c = 0;
void setup()
{
pinMode( ledPin13, OUTPUT );
pinMode( ledPin12, OUTPUT);
pinMode( ledPin11, OUTPUT);
Serial.begin( 57600 );
}
void loop()
{
// Wait for a character to arrive at the serial port.
if( Serial.available() > 0 )
{
// Read one byte (character).
c = Serial.read();
switch( c )
{
case 'e':
Serial.println("case e received");
digitalWrite( ledPin13, HIGH );
Serial.println("Pin 13 HIGH");
break;
case 'i':
Serial.println("case i received");
digitalWrite( ledPin12, HIGH );
Serial.println("Pin 12 HIGH");
break;
case 'q':
Serial.println("case q received");
sensorValue = analogRead( sensorPin )/32;
Serial.print(sensorValue);
analogWrite(ledPin11, sensorValue);
Serial.println("Pin 11 HIGH");
break;
case 'z':
Serial.println("case z received");
digitalWrite( ledPin13, LOW );
Serial.println("Pin 13 LOW");
break;
case 'p':
Serial.println("case p received");
digitalWrite( ledPin12, LOW );
Serial.println("Pin 12 LOW");
break;
case 'l':
Serial.println("case l received");
digitalWrite( ledPin11, LOW );
Serial.println("Pin 11 LOW");
break;
}
}
}
[/code]
Next is my PROCESSING code:
import processing.serial.*; //Import the serial library into your sketch
import cc.arduino.*; //Import the Arduino-Firmata library into your sketch
Arduino arduino; //Create an instance of Arduino named arduino (can be any name)
char e;
int ledPin;
char 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, 57600);
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);
print(arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 57600); //defines arduino our board and sets the communication rate
arduino.pinMode(ledPin, Arduino.OUTPUT);
}
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;
}
};
void keyReleased() {
delay(10);
switch (key) {
case 'e':
case 'E':
scope.write('z');
break;
case 'i':
case 'I':
scope.write('p');
break;
case 'q':
case 'Q':
scope.write('l');
break;
}
}
The error I am getting is: Error inside Serial.<init.()
and it highlights the following code:
arduino = new Arduino(this, Arduino.list()[0], 57600); //defines arduino our board and sets the communication rate
You are trying to connect the same serial port to two things, one is called port the other is called arduino. There is only one port and it can only be connected to one thing.
Part of this looks like my code but the Firmata part is not. The code you have for Processing is a mess.
Is there anyway you can help assist me with getting this code corrected? I am not an expert programmer and I had help with this code a while back (when it worked, but now doesn't).
Code does not do that, it will work like it always did, what has probbly happened is that you have changed something that you do not know about.
What is the code supposed to do? Why are you calling up the Firmata libary?
What I am trying to do is send the serial 'p' or 'z' (from processing code) to my arduino where it receives the serial and performs the action based on the serial.
so when I press the 'e' key on my laptop, the action (shown in the arduino code) "ledPin13" turns on (high).
I plan on using xbee's to transfer the signal to the arduino from my laptop fyi.
I am not fully sure why the Firmata code is being imported. The reason I have it is because when I was getting help with it, I was told I needed it.
import processing.serial.*; //Import the serial library into your sketch
char e;
char 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, 57600);
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 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;
}
};
void keyReleased() {
delay(10);
switch (key) {
case 'e':
case 'E':
scope.write('z');
break;
case 'i':
case 'I':
scope.write('p');
break;
case 'q':
case 'Q':
scope.write('l');
break;
}
}
I am on my iPad at the moment so I cannot check that this is OK.
I have the arduino attached with the same code loaded as mentioned above:
=> when I upload it I get the following message:
Binary sketch size: 5676 bytes (of a 258048 byte maximum)
avrdude: stk500v2_ReceiveMessage(): timeout
Except is says done uploading..
NOW from the processing code:
Error inside Serial.()
Dec 29 04:03:36 Mac-2.local java[6161] : CGContextGetCTM: invalid context 0x0
Dec 29 04:03:36 Mac-2.local java[6161] : CGContextSetBaseCTM: invalid context 0x0
Dec 29 04:03:36 Mac-2.local java[6161] : CGContextGetCTM: invalid context 0x0
Dec 29 04:03:36 Mac-2.local java[6161] : CGContextSetBaseCTM: invalid context 0x0
WARNING: RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.1-7
[0] "/dev/tty.usbmodem1d11"
[1] "/dev/cu.usbmodem1d11"
[2] "/dev/tty.Bluetooth-PDA-Sync"
[3] "/dev/cu.Bluetooth-PDA-Sync"
[4] "/dev/tty.Bluetooth-Modem"
[5] "/dev/cu.Bluetooth-Modem"
Connected to /dev/tty.usbmodem1d11
gnu.io.PortInUseException: Unknown Application
at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:467)
at processing.serial.Serial.(Serial.java:139)
at processing.serial.Serial.(Serial.java:105)
at Newcodeold_UPDATE.portConnect(Newcodeold_UPDATE.java:50)
at Newcodeold_UPDATE.setup(Newcodeold_UPDATE.java:89)
at processing.core.PApplet.handleDraw(PApplet.java:1608)
at processing.core.PApplet.run(PApplet.java:1530)
at java.lang.Thread.run(Thread.java:680)
Exception in thread "Animation Thread" java.lang.RuntimeException: Error inside Serial.()
at processing.serial.Serial.errorMessage(Serial.java:591)
at processing.serial.Serial.(Serial.java:151)
at processing.serial.Serial.(Serial.java:105)
at Newcodeold_UPDATE.portConnect(Newcodeold_UPDATE.java:50)
at Newcodeold_UPDATE.setup(Newcodeold_UPDATE.java:89)
at processing.core.PApplet.handleDraw(PApplet.java:1608)
at processing.core.PApplet.run(PApplet.java:1530)
at java.lang.Thread.run(Thread.java:680)
My Processing code is the following:
import processing.serial.*; //Import the serial library into your sketch
import cc.arduino.*; //Import the Arduino-Firmata library into your sketch
Arduino arduino; //Create an instance of Arduino named arduino (can be any name)
char e;
int ledPin;
char 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, 57600);
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 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;
}
};
void keyReleased() {
delay(10);
switch (key) {
case 'e':
case 'E':
scope.write('z');
break;
case 'i':
case 'I':
scope.write('p');
break;
case 'q':
case 'Q':
scope.write('l');
break;
}
}
Why are you not using the code I posted for you.
Am I wasting my time trying to tell you anything?
You ask for advice and then ignore it, why should I bother?
I thought the only change was the first and 2nd rows the the setup and adding the port. I do appreciate all your help!
I copied your exact code and got the following
=> This portion was high lighted from the code:
scope = new Serial(this, portName, 57600);
Error inside Serial.()
Experimental: JNI_OnLoad called.
Dec 29 04:18:32 Mac-2.local java[6546] : CGContextGetCTM: invalid context 0x0
Dec 29 04:18:32 Mac-2.local java[6546] : CGContextSetBaseCTM: invalid context 0x0
Dec 29 04:18:32 Mac-2.local java[6546] : CGContextGetCTM: invalid context 0x0
Dec 29 04:18:32 Mac-2.local java[6546] : CGContextSetBaseCTM: invalid context 0x0
WARNING: RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.1-7
[0] "/dev/tty.usbmodem1d11"
[1] "/dev/cu.usbmodem1d11"
[2] "/dev/tty.Bluetooth-PDA-Sync"
[3] "/dev/cu.Bluetooth-PDA-Sync"
[4] "/dev/tty.Bluetooth-Modem"
[5] "/dev/cu.Bluetooth-Modem"
Connected to /dev/tty.usbmodem1d11
gnu.io.PortInUseException: Unknown Application
at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:467)
at processing.serial.Serial.(Serial.java:139)
at processing.serial.Serial.(Serial.java:105)
at Newcodeold_UPDATE.portConnect(Newcodeold_UPDATE.java:45)
at Newcodeold_UPDATE.setup(Newcodeold_UPDATE.java:57)
at processing.core.PApplet.handleDraw(PApplet.java:1608)
at processing.core.PApplet.run(PApplet.java:1530)
at java.lang.Thread.run(Thread.java:680)
Exception in thread "Animation Thread" java.lang.RuntimeException: Error inside Serial.()
at processing.serial.Serial.errorMessage(Serial.java:591)
at processing.serial.Serial.(Serial.java:151)
at processing.serial.Serial.(Serial.java:105)
at Newcodeold_UPDATE.portConnect(Newcodeold_UPDATE.java:45)
at Newcodeold_UPDATE.setup(Newcodeold_UPDATE.java:57)
at processing.core.PApplet.handleDraw(PApplet.java:1608)
at processing.core.PApplet.run(PApplet.java:1530)
at java.lang.Thread.run(Thread.java:680)