Send ArrayList elements to Arduino over serial, and have Arduino store them in it's own array?
in
Core Library Questions
•
1 year ago
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:
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;
}
}
}
1