I'm sending a large volume of data over the serial port to the arduino, which uses it as commands to run stepper motors. The issue, of course, is the serial buffer. I'm having a lot of difficulty figuring out how to have my processing sketch wait until the arduino's serial buffer is empty before sending more data over the serial connection. I'm aware that there's a lot of documentation and examples that are related to the serial library, but I seem to be unable to find any examples that accomplish what I'm trying to do. Surely this has been done in many different circumstances for various projects. Can anyone provide an example?
[Edit: This question was the result of an error in my code, which caused me to make an incorrect assumption about the way that the display worked.]
I am currently using get() and set() to detect specific pixel colours in an image and change them according to specific criteria. However, once a given pixel is changed on the display, the change is not reflected in the underlying PImage unless I first save a copy of the image. The issue is that my program requires many (many) iterations, and these saves slow it down considerably. In addition, while my linux-based pc can handle this (albeit slowly), after a certain amount of time my work pc (win 7-based) crashes processing. It seems that saving that many times that quickly just proves to be too much for it.
Is there any way to detect a pixel change in the display without saving the underlying image and reloading it?
I've been attempting to come up with a way to determine the nearest black pixel to a given coordinate. Initially, I was trying to use pvectors, but I was having trouble sorting them (I'm new to Processing, and programming in general). In the end, I landed on the following:
PImage img;
int col; // variable to capture colour of pixels
int currx = 1; // current value of x coordinate
int curry = 1; // current value of y coordinate
int targx; // x coordinate of closest black pixel
int targy; // y coordinate of closest black pixel
float targdist; // distance (euclidean) to target
void setup() {
img = loadImage("test.png");
size(img.width, img.height);
targdist = dist(0,0,img.width,img.height); // set initial target distance to diagonal of image
for (int y=0; y<img.height; y++) { // loop through all pixels
for (int x=0; x<img.width; x++) {
col = img.get(x, y) & 0xFF;
float z;
if(col == 0){ // if pixel is black
z = dist(currx, curry, x, y); // calculate distance between current coord and target
if(z < targdist) { // if current black pixel closer than previous
targdist = z; // update target distance and coordinates
targx = x;
targy = y;
}
}
}
}
}
void draw() {
image(img, 0, 0);
print("(" + targx + ", " + targy + ") is " + targdist + " units away from (" + currx + ", " + curry + ")");
noLoop();
}
However, I'm wondering if there's a more efficient way of doing this. For now, this meets my needs, but in the future I may want to extend this to find, say, the closest gray -> black pixel pair for each gray pixel (not sure I worded that well, but for each gray pixel, find the closest black pixel to it). I doubt this method will work well for that. Can anyone point me in the best direction to tackle something like that?
I'm relatively new to Processing, though I've used it for a handful of arduino projects (largely to communicate simple commands via the serial library).
Here's what I'm trying to accomplish: Given an image composed (initially) of black and white pixels, find the shortest path from one given (black) pixel to another given (black) pixel (assume for the moment that a valid path exists). Do not travel on white pixels.
Note: path doesn't have to be the shortest path possible, but should be reasonable (so the A* algorithm, I guess). Though to be honest, at this point I'd be happy with any method that would produce any path that stays off the white pixels...
It seems the
Pathfinder library is perfect for this, but I just can't seem to wrap my head around it. The documentation is... dense... for a beginner. Does anyone have any simple example code that would accomplish what I'm looking to do?