So I'm just trying to set up a really simple handshake between a Processing interface and my Arduino, which is measuring data from three sensors. The Processing code I took is taken from sparkfun, it's the simplest I could find as it's the first project I'm facing
The first step it's Processing sending 1 via serial to trigger a recording from Arduino.
The board receives the input, start grabing the data and the sends three plus three arrays, the first three being the measurements and three individual numbers being the averages. I'm sending those numbers separated by commas, and using trim and split to separate them. Then those numbers are sent back through the serial.
At last Processing reads the data in a single array and just plots it for now.
It doesn't actually work, i think because of the serial writing/reading or something else, i don't actually get an error code except one related to the SerialEvent.
I tried everything I found on the internet, avoiding solutions that would complicate my code too much, but I couldn't solve it...
The processing part is:
import processing.serial.*;
int[] array0;
//mySerial = new Serial(this, myPort, 9600); //copiato
Serial mySerial; // Create object from Serial class
void setup()
{
size(200,200); //make our canvas 200 x 200 pixels big
String myPort = Serial.list()[0];
mySerial = new Serial(this, myPort, 9600);
mySerial.bufferUntil('\n');
}
void draw() {
if (mousePressed == true)
{ //if we clicked in the window
mySerial.write('1'); //send a 1
println("1");
} //else
// { //otherwise
//mySerial.write('0'); //send a 0
//}
for (int b = 0; b <= 300; b += 1) {
rect(0, b*10, array0[b], 8);
}
}
void grafico() {
fill(0);
}
void serialEvent(Serial myPort)
{
delay(2000);
String inString = myPort.readStringUntil('\n');
if (inString!=null); // this area splits the arduino inputs such that they can be received seperately
{
inString=trim(inString);
int[] sensors= int (split(inString, ","));
for (int k = 0; k <= 300; k += 1) {
{
array0[k]=sensors[k];
}
}
}
}
While that's the Arduino sendback part. The receiving part just works, but if it's helpful i'll send the whole data grabbing chunk.
void sendTheData0()
{
//Serial.print("sensor 0:");
for(int n=0; i<sizeof(sens0); n++)
{
//Serial.print(",");
Serial.print(sens0[n]);
Serial.print(",");
delay(10);
n++;
}
}
void sendTheData1()
{
//Serial.print("sensor 1:");
for(int l=0; i<sizeof(sens1); l++)
{
//Serial.print(",");
Serial.print(sens1[l]);
Serial.print(",");
delay(10);
l++;
}
}
void sendTheData2()
{
//Serial.print("sensor 2:");
for(int m=0; i<sizeof(sens2); m++)
{
//Serial.print(",");
Serial.print(sens2[m]);
Serial.print(",");
delay(10);
m++;
}
}
void setup (){
Serial.begin(9600);
}
void loop () {
// read serial data
if (Serial.available()){
incomingByte=Serial.read();
if(incomingByte== '1'){
start=1;
}
else {
start=0;
}
if(start==1){
inizio= millis();
sens0[i]=readsensor0();
if(sens0[i]>max0){
max0=sens0[i];
}
media0=media0+((sens0[i]-media0)/(i+1));
i++;
sens1[j]=readsensor1();
if(sens1[j]>max1){
max1=sens1[j];
}
media1=media1+((sens1[j]-media1)/(j+1));
j++;
sens2[k]=readsensor2();
if(sens2[k]>max2){
max2=sens2[k];
}
media2=media2+((sens2[k]-media2)/(k+1));
k++;
}
if ((millis()-inizio)>1000){
sendTheData0();
sendTheData1();
sendTheData2();
Serial.print(", ");
//Serial.print("media0:");
Serial.print(media0);
Serial.print(", ");
delay(10);
//Serial.print("media1:");
Serial.print(media1);
Serial.print(", ");
delay(10);
//Serial.print("media2:");
Serial.print(media2);
}
Serial.println(",");
delay(100);
}
}
ANY help is really appreciated!!!