We are about to switch to a new forum software. Until then we have removed the registration on this forum.
A week ago I wrote a little sketch that reveals a bitmap pixel by pixel based on mouse coordinates. Thanks to the good people here it works like a charm. If you're interested, here it is:
import processing.serial.*; //import the Serial library
Serial myPort; //the Serial port object
PImage img;
void setup()
{
size(182,262); //This is the size of the canvas and the range of the printer
// Make a new instance of a PImage by loading an image file
img = loadImage("parel.bmp");
tint(255,100);
image (img,0,0);
}
void draw()
{
color pix = img.get(mouseX, mouseY);
stroke(pix);
point(mouseX, mouseY);
}
So I am trying to expand this sketch by receiving pixel data from my arduino. I wrote a little arduino sketch that systematically sends all the pixel coordinates in the bitmap mentioned above over serial. And that seems to work too. But Processing is giving me some trouble here when I try to receive the coordinates over serial. Here's the sketch:
import processing.serial.*; //import the Serial library
Serial myPort; //the Serial port object
String strIn = ""; //Declare a string with name str_in, this will store the coordinates received from the arduino
String portName = ""; //apparently this is necessary
int coordinates;
int locX= 0;
int locY= 0;
PImage img;
void setup()
{
size(182,262); //This is the size of the canvas and the range of the printer
// Make a new instance of a PImage by loading an image file
img = loadImage("parel.bmp");
tint(255,100);
image (img,0,0);
String portName = Serial.list()[0];
myPort = new Serial(this, portName,9600);
}
void draw()
{
while (myPort.available() > 0) { // If data is available,
strIn = myPort.readStringUntil('\n'); // read it and store it in a string called strIn
}
int[] coordinates = int(split(strIn, ","));
//This should cut the strIn string in two integers, separated by a space
//and called coordinates. We expect a message coming in something like "XXX YYY"
int locX = coordinates[0]; //This says that the first integer is an integer called LocX
int locY = coordinates[1]; //This says that the first integer is an integer called LocX
color pix = img.get(locX, locY);
stroke(pix);
point(locX, locY);
}
The debugger tells me it stores the incoming serial message in the strIn string, but locX and locY remain at 0. If I run the sketch without the debugger I get an arrayindexoutofboundsexception in line 34, which I think means it doesn't store anything in LocY. From what I read on this forum, I get that serial communication is troubling a lot more people than just me, but I can't find what the problem is. So if anybody has any ideas, you'd be a great help. My bitmap image is attached here as well.
Answers
Your arduino-code could help here, because we don't know what kind of message we expect. The exception in 34 should mean, that coordinates has only a length of 1. Maybe try to debug this by logging the value of strIn to the console.
Converting the string-array that you get by using split() to an int-array with using int() may also lead to problems. When you have spaces for example this won't work.
The arduino sends something like this:
Basically going over every pixel.
The arduino code is here:
I hope that helps.
Thank you, much easier to run the code and debug.
The problem is, that you messages look like this:
59,2\n
With the split-command you still have the line-break in the second part. to remove it you can use trim().
I tried it and it still doesn't parse. It reads the coordinates in strIn, but doesn't split it up to locX and locY.
Whoops. that was a typo. No, the problem is solved. Thanks a lot, Benja.