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)); } }
I have an ArrayList in Processing that I need to send to my Arduino, and have the Arduino store each element of this ArrayList in an array. Or, if I could send the ArrayList as a single entity to the Arduino, which would then store each of the ArrayList's elements into a corresponding position in an array, that would work as well. The main problem I'm having is that I don't know how I can tell what elements are in the Arduino's array, and Serial.println won't work because the Processing code is using the serial bus. Here's what I have:
Processing Code:
import processing.serial.*;
//The serial port that the Arduino is on Serial port; //The ArrayList ArrayList ar;
void setup() { size(200,200); //Initialize the serial port at 9600 baud port = new Serial(this, Serial.list()[0], 9600); //Set initial size of ArrayList ar ar = new ArrayList(10); //Add numbers 0-9 to ar for (int i=0; i<10; i++) { ar.add(i); } }
void draw() { //Loop through each element of ar for (int i=0; i<ar.size(); i++) { //Print the current element out below println(ar.get(i)); //Convert the object cast by ar into an integer int curInt = (Integer)ar.get(i); //Write that integer to port port.write(curInt); }
And the Arduino code:
int ar[10];
void setup() { Serial.begin(9600); }
void loop() { //If there is incoming data to be read if (Serial.available() > 0) { //Store data in the integer in int in = Serial.read(); //Assign the value being read to it's respective position in //the array for (int i=0; i<sizeof(ar); i++) { ar[i] = in; } } }
I've tried a combination of other posts, the Learning section, and the book Processing, by Reas and Fry. Try as I might, I just can't figure out how to do this. I need Processing to load a black and white image, and store the coordinates of all black pixels in 2 arrays: 1 for their x-axis, and the other for their y-axis. How can I do this?
//Load the pixels loadPixels(); //Loop through each pixel for (int ix = 0; ix < pixels.length; ix++) { for (int iy = 0; iy < pixels.length; iy++) { //Set col to the color at that pixel int col = color(get(ix,iy)); //If the color is less than 20 on an RGB scale (to include dark gray) if (col < 20) { //Add the X and Y coordinates to their respective array append(xArray,ix); append(yArray,iy); } } } }