Send ArrayList to Arduino, and have the Arduino store them in it's own array?
in
Programming Questions
•
1 year ago
I have the Processing sketch below, which scans through an image, storing the X and Y coordinates of each black pixel into 2 respective ArrayLists - xList, and yList. This sketch currently prints out these coordinates, but I now need to send each element of each of these arrays to the Arduino, and have the Arduino store them in it's own array (or arrays). How?
- import javax.swing.*;
PImage img;
//Threshold determines what is considered a "black" pixel
//based on a 0-255 grayscale.
int threshold = 50;
void setup() {
// File opener
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
if (file.getName().endsWith("jpg")) {
img = loadImage(file.getPath());
if (img != null) {
size(img.width,img.height);
image(img,0,0);
}
} else {
println("Please select a .jpg image file.");
}
}
//I added noLoop() here, because the program would output
//the coordinates continuously until it was stopped.
noLoop();
}
void draw() {
//Here it scans through each pixel of the image.
ArrayList xList = new ArrayList();
ArrayList yList = new ArrayList();
for (int ix = 0; ix < img.width; ix++) {
for (int iy = 0; iy < img.height; iy++) {
//If the brightness of the pixel at (ix,iy) is less than
//the threshold
if (brightness(get(ix,iy)) < threshold) {
//Add ix, and iy to their respective ArrayList.
xList.add(ix);
yList.add(iy);
}
}
}
//Print out each coordinate
//For this statement, I use xList.size() as the limiting
//condition, because xList and yList should be the same size.
for (int i=0; i<xList.size(); i++) {
println("X:" + xList.get(i) + " Y:" + yList.get(i));
}
}
1