We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Having trouble starting out with Arduino
Page Index Toggle Pages: 1
Having trouble starting out with Arduino (Read 3784 times)
Having trouble starting out with Arduino
May 25th, 2009, 6:29pm
 
I'm trying to write some code to start out with using the Arduino with Processing. I decided to start out by alering the beginners' exaple of integrating a mouse to move a line with a small thumb pad.
The vertical and horizontal potentiometers are attached to analog pins 1 and 0.

I've worked through a number of bugs on my own, but I don't quite know what the problem is here.


Code:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int verPin = 0;
int horPin = 1;
float ver = 1;
float hor = 2;

void setup() {
 size(400, 400);
 stroke(255);
 arduino.pinMode(verPin, Arduino.INPUT);
 arduino.pinMode(horPin, Arduino.INPUT);
}

void draw() {
 background(192, 64, 0);
 ver = arduino.analogRead(verPin);
 ver = map(ver, 0, 1023, 0, height);
 hor = arduino.analogRead(horPin);
 hor = map(hor, 0, 1023, 0, width);
 line(width/2, height/2, hor, ver);
}


I keep getting the error:


NullPointerException

Exception in thread "Animation Thread" java.lang.NullPointerException
     at sketch_may25b.setup(sketch_may25b.java:32)
     at processing.core.PApplet.handleDraw(PApplet.java:1400)
     at processing.core.PApplet.run(PApplet.java:1328)
     at java.lang.Thread.run(Unknown Source)




Whats up? What am I doing wrong?
Re: Having trouble starting out with Arduino
Reply #1 - May 26th, 2009, 9:40am
 
Looks like you need to initialize your arduino object.
i.e.

arduino = new Arduino(this, Arduino.list()[0], 9600);
Re: Having trouble starting out with Arduino
Reply #2 - Jun 2nd, 2009, 7:30pm
 
So that stopped the errors, but when I run the following code I get nothing.

Code:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int verPin = 0;
int horPin = 1;
float ver = 1;
float hor = 2;

void setup() {
size(400, 400);
stroke(255);
arduino = new Arduino(this, Arduino.list()[0], 9600);
arduino.pinMode(verPin, Arduino.INPUT);
arduino.pinMode(horPin, Arduino.INPUT);
}

void draw() {
background(192, 64, 0);
ver = arduino.analogRead(verPin);
ver = map(ver, 0, 1023, 0, height);
hor = arduino.analogRead(horPin);
hor = map(hor, 0, 1023, 0, width);
line(width/2, height/2, hor, ver);
}


I have the following code running on my Arduino, and the serial monitor on the Arduino's GUI is spitting out proper numbers.

Code:


int verPin = 0;
int horPin = 1;
int ver = 234;
int hor = 456;

void setup()
{
Serial.begin(9600);
pinMode(verPin, INPUT);
pinMode(horPin, INPUT);
}

void loop()
{
ver = analogRead(verPin);
ver = map(ver, 0, 1023, 0, 100);
hor = analogRead(horPin);
hor = map(hor, 0, 1023, 0, 100);
Serial.print("Vertical ");
Serial.println(ver);
Serial.print("Horizontal ");
Serial.println(hor);
delay(100);
}


Nothing happens to the line in the Processing window, what's wrong now that there's not an error message?
Re: Having trouble starting out with Arduino
Reply #3 - Jun 2nd, 2009, 11:02pm
 
Are you using the same serial ports for both the monitor and Processing?
(getting too late hook anything up here)
It looks like your code should do what you expect.
Re: Having trouble starting out with Arduino
Reply #4 - Jun 3rd, 2009, 8:42am
 
The Tx and Rx indicators on my Arduino do nothing when I run the sketch. Where do I find out or set which port Processing reads the Arduino from?
Re: Having trouble starting out with Arduino
Reply #5 - Jun 3rd, 2009, 9:39am
 
Sorry, I meant in the Processing sketch.

You are using Arduino.list()[0] as your serial port name, but does that name match the one you used in the Arduino IDE?

What does "println(Arduino.list());" display and what is selected under Tools->Serial Port (in Arduino IDE)?
Re: Having trouble starting out with Arduino
Reply #6 - Jun 3rd, 2009, 10:23am
 
In Arduino I've it hooked up to COM4.

In Processing, when I run the following of code:
Code:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int verPin = 0;
int horPin = 1;
float ver = 1;
float hor = 2;

void setup() {
size(400, 400);
stroke(255);
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 9600);
arduino.pinMode(verPin, Arduino.INPUT);
arduino.pinMode(horPin, Arduino.INPUT);
}

void draw() {
background(192, 64, 0);
ver = arduino.analogRead(verPin);
ver = map(ver, 0, 1023, 0, height);
hor = arduino.analogRead(horPin);
hor = map(hor, 0, 1023, 0, width);
line(width/2, height/2, hor, ver);
}


I no longer get an error message, and it tells me that there's an Arduino connected to COM4, but the Processing sketch still wont do anything.

When I run it, the Tx/Rx lights don't flash.
Re: Having trouble starting out with Arduino
Reply #7 - Jun 3rd, 2009, 10:46pm
 
Have you uploaded Firmata as mentioned here:
http://www.arduino.cc/playground/Interfacing/Processing

Also, Firmata v2 is expecting 115200 as the data rate.

Forgot to do that myself. Wink
Re: Having trouble starting out with Arduino
Reply #8 - Jun 5th, 2009, 4:09pm
 
Alright so I uploaded the Firmata, apparently that was one of my problems as well.

Unfortunately... it still doesn't work.
Is there a place in the program where I have to specify which COM port the arduino is attached to? Processing picks it up, because at teh bottom it shows the following, but the program still doesn't respond to the hardware.

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
[0] "COM3"
[1] "COM4"

Re: Having trouble starting out with Arduino
Reply #9 - Jun 5th, 2009, 4:18pm
 
Hmm... I was able to get it to work (even found a pot. to hook up).

I didn't have to do anything with Firmata.

I did change the constructor to:
arduino = new Arduino(this, "COM5", 115200);

Replace "COM5" with whichever comm. port your Arduino is on.
( i.e. Arduino.list()[0] or Arduino.list()[1] )
Re: Having trouble starting out with Arduino
Reply #10 - Jun 5th, 2009, 4:23pm
 
HAHAHAHAHA IT WORKS!

Thanks man, all I needed were the quotation marks around COM4. I am eternally grateful, as you've just helped me over a huge hurdle for me (and my college major).
Page Index Toggle Pages: 1