I’m trying to send data to an Arduino, but I keep getting a NullPointerException. I don’t get it. I’m no coder, so I am working with examples. If someone could point me in the right direction that would be awesome!
import processing.serial.*;
import cc.arduino.*;
int xpos=90; // set x servo's value to mid point (0-180);
int ypos=90; // and the same here
Serial port; // The serial port we will be using
Arduino arduino;
void setup()
{
size(360, 360);
frameRate(100);
println(Serial.list()); // List COM-ports
//select second com-port from the list (COM3 for my device)
// You will want to change the [1] to select the correct device
// Remember the list starts at [0] for the first option.
arduino = new Arduino(this, Arduino.list()[1], 9600);
}
void draw()
{
fill(175);
rect(0,0,360,360);
fill(255,0,0); //rgb value so RED
rect(180, 175, mouseX-180, 10); //xpos, ypos, width, height
fill(0,255,0); // and GREEN
rect(175, 180, 10, mouseY-180);
update(mouseX, mouseY);
}
void update(int x, int y)
{
//Calculate servo postion from mouseX
xpos = x/2;
ypos = y/2;
//Output the servo position ( from 0 to 180)
port.write(xpos+"x");
port.write(ypos+"y");
}
in your console you should see a list or line about available ports,
on windows like COM5
for your arduino.
and you should know that number already from the arduino IDE
so if that comes up first must change in the code to
but now i see that you make the variable
port
but never define it,
but try to write to it.
so not sure about the arduino thing, can not test right now, so pls check on that and find a example for it.
when i am out of hospital i will check also._
ok, after 3 month working with one eye, yesterday got a second plastic lens and now see it all again!
that seems to be the point,
instead try one example and get it running looks like you try to mix two
-1- example from serial lib
-2- example from arduino lib
( what is not a good name as it is for arduino loaded with firmata code ONLY )
so actually besides to say again
just try one example
above code can only be changed to one or the other
knowing what you want to do?
spec. if your arduino should run / is loaded / with firmata code /
like as remote I/O for user who not want to learn arduino coding
and just use the arduino IDE to upload the firmata…
OR
you have your own code loaded to the arduino what expects the TEXT xpos180 ypos180
I’ve tried several example sketch sets, and several more from users. I’m trying to control a servo using an Arduino and mouse on a PC. I do not want to use Firmata since this is going to be used in a much larger sketch with other features. I am trying to get the basics of how to read and write data over a serial, then use the data for controlling things. The simple sketches I find aren’t what I need, and the more complex ones aren’t working.
Right now, I just want to figure out the basics coding for this setup.
Arduino Mega 2560 R3 on COM3, servo connected to pin 7.
Use mouse X or Y value sent over serial, to be read by Arduino.(without Firmata)
I’m still trying to learn this on my own, but any info would be great. Thank you, everyone, for the help and comments!
Well, I think I’ve burned myself out. I have worked with this for a few days, and I am still not getting it. Very frustrated right now, and pretty sure my thinking is wrong on how to do this. The examples I try seem to work, but they either use firmata, or use a single piece of data like 0 or 180.
Every time I try to modify, or write a similar code, it doesn’t work. The servo never moves.
Thanks for the help, and if anyone comes across a simple sketch that might work, shoot me a link.
This is my Arduino code. I keep getting the feeling that Processing isn’t sending the data in the right format or something since this works with another sketch, but nothing to back that thought up.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int ServoPin = 7; //identify which pin the servo is connected to
int ServoAngle; //create a variable
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
myservo.attach(ServoPin); // attaches the servo on pin 9 to the servo object
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
ServoAngle = Serial.read(); //read the angle that was sent from the Processing sketch
myservo.write(ServoAngle); // move the servo to the new angle
}
}
//___ hardware test
i know your arduino code looks like,
but sure you tested first the excample https://www.arduino.cc/en/Tutorial/Knob
so you tested your wiring ( ok on pin 7 not 9 )
//___ arduino processing
you send data from 0 … 180 ?
now whenever you do something in arduino you could
send feedback, some info like
1[enter]
set angle: 49 // character 1 as int
set angle: 10 // from the enter
at the same time in processing can catch that
void serialEvent(Serial myPort) { // if want to see what the arduino is answering:
String arduinoline = myPort.readStringUntil(10);
if ( arduinoline != null ) print(arduinoline);
}
import processing.serial.*;
Serial arduinoPort;
boolean diag = true;//false;//true; // print / echo diagnostic
void setup() {
printArray(Serial.list());
arduinoPort = new Serial(this, "/dev/ttyACM0", 9600 );
println("use: key [s] send random angle 0 .. 180");
}
void draw() {
}
void send_setpoint() {
int outi;
byte outb;
outi = int(random(181));
outb = byte(outi);
if (diag) println("send to arduino: i "+outi+" b "+outb);
arduinoPort.write(outb);
}
void keyPressed() {
if ( key == 's') send_setpoint();
}
void serialEvent(Serial myPort) { // if want to see what the arduino is answering:
String arduinoline = myPort.readStringUntil(10);
if ( arduinoline != null ) print(arduinoline);
}
arduino code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo, maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int ServoPin = 7; //identify which pin the servo is connected to
int ServoAngle; //create a variable
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) { ; } // for LEONARDO needed
Serial.println("<Arduino is ready>");
myservo.attach(ServoPin); // attaches the servo on pin 9 to the servo object
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
ServoAngle = Serial.read(); //read the angle that was sent from the Processing sketch
myservo.write(ServoAngle); // move the servo to the new angle
Serial.print("set angle: ");
Serial.println(ServoAngle);
}
}
Sorry for the long delay. Your code works perfectly with the servo. I just changed the serial port to “COM3”. The serial data comes back and the servo move to the proper angle.
So, now I know that it can work. I’ll try to modify your code to be able to input a specific number.
Ok, so I changed the random number to mouseX, and it works as long as I keep the S key pressed. All good there. I changed the Arduino code to incorporate a second servo (since I need to be able to do multiple servos) and started to change the Processing code to have a second value based on mouseY.
That’s where I get lost. I can’t figure out how the arduinoPort.write and the read on the Arduino end get split for 2 values. I’m reading on strings and serial data right now.
Processing code
import processing.serial.*;
Serial arduinoPort;
boolean diag = true;//false;//true; // print / echo diagnostic
boolean diag1 = true;//false;//true;
void setup() {
printArray(Serial.list());
arduinoPort = new Serial(this, "COM3", 9600 );
println("use: key [s] send random angle 0 .. 180");
}
void draw() {
}
void send_setpoint() {
int outi;
byte outb;
outi = int(mouseX);
outb = byte(outi);
if (diag) println("send to arduino: i "+outi+" b "+outb);
arduinoPort.write(outb);
int outj;
byte outc;
outj = int(mouseY);
outc = byte(outj);
if (diag1) println("send to arduino: j "+outj+" b "+outc);
arduinoPort.write(outc);
}
void data_send() {
if ( key == 's') send_setpoint();
}
void serialEvent(Serial myPort) { // if want to see what the arduino is answering:
String arduinoline = myPort.readStringUntil(10);
if ( arduinoline != null ) print(arduinoline);
}
Arduino code
#include <Servo.h>
Servo myservo; // create servo object to control a servo, maximum of eight servo objects can be created
Servo myservo1; // create servo object to control a servo, maximum of eight servo objects can be created
int posa = 0; // variable to store the servo position
int ServoPina = 7; //identify which pin the servo is connected to
int ServoAnglea; //create a variable
int posb = 0; // variable to store the servo position
int ServoPinb = 8; //identify which pin the servo is connected to
int ServoAngleb; //create a variable
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) { ; } // for LEONARDO needed
Serial.println("<Arduino is ready>");
myservo.attach(ServoPina); // attaches the servo on pin 9 to the servo object
myservo1.attach(ServoPinb); // attaches the servo on pin 9 to the servo object
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
ServoAnglea = Serial.read(); //read the angle that was sent from the Processing sketch
myservo.write(ServoAnglea); // move the servo to the new angle
myservo1.write(ServoAngleb); // move the servo to the new angle
Serial.print("set angle: ");
Serial.println(ServoAnglea);
Serial.println(ServoAngleb);
}
}