I have on my arduino a 50x50 2d array that needs to be send to the computer using processing, the idea is that the bluetooth device hooked on the arduino is always on and when processing starts it should request for data.
So I started with the following code:
public int[][] imageArray;
Serial myPort;
public void setup() {
myPort = new Serial(this, "COM5", 9600);
size(50 * _pixelSize, 50 * _pixelSize);
}
public void draw() {
int startcode = 101;
// lets first request with code 101
myPort.write(startcode);
while (myPort.available() > 0) {
int code = myPort.read();
println(code);
if (code == 102) {
// we found the answer code lets recieve
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
int pixel = myPort.read();
print(pixel);
if (pixel == 103) {
drawImage();
break;
} else {
imageArray[i][j] = pixel;
}
}
}
}
}
}
And this is the code running on the arduino
byte imageArray[50][50] = ( some big data containing 0 or 1 for white or black, not include because to huge).
void setup()
{
Serial2.begin(9622);
pinMode(13, OUTPUT);
}
void loop() {
// lets first start a handshake
// we first need to recieve code 121
if(Serial2.available() > 2) {
int code = Serial2.read();
if(code == 121) {
//allright a request has been made, lets now send the sending code 122 back
Serial2.write(122);
// greate now lets start sending some image
for(int i = 2; i < 52; i++) {
for(int j = 2; j < 52; j++) {
Serial2.write((int)imageArray[i][j]);
}
}
//data send, let the other party know
Serial2.write(123);
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
}
And this totally doesnt work at all, anyone has a better solution? I feel im doing wrong things here.