Trouble Communicating With Arduino

Hello, I am using Processing to send a start string to the Arduino, which then usually proceeds to send a stream of data back to the computer (it works when I send 'Start' using the serial monitor).

It's not working though and I can see the Arduino recieves the string (I physically sniffed the data on the pins, I also just got the Arduino to repeat what I'd sent) and I know that processing is holding the serial port open so it's not dropping it.

I cant for the life of me figure out why they just wont get along!
Here is the processing code:

import processing.serial.*;

Serial myPort;  // The serial port
 String b = "Start";
 char data[] = {'S', 't', 'a', 'r', 't', '\0'};
 String str2 = new String(data);
 char inByte;

void setup() {
  // List all the available serial ports
  printArray(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[2], 9600);
  myPort.buffer(1);
  myPort.write(str2);
       /*myPort.write('S');
              myPort.write('t');
                     myPort.write('a');
                            myPort.write('r');
                                   myPort.write('t');*/              
}

void draw() {
    background(0);
}

void serialEvent(Serial myPort) {
    inByte = myPort.readChar();
    println(inByte);
}

And here is the Arduino code (it is reading values from an array of thermistors):

#define M_0 8
#define M_1 9
#define M_2 10
#define M1_E 7
#define M2_E 5
#define M3_0 2
#define M3_1 3
#define M3_2 4
#define ROWS 14
#define COLS 14

const int MuxPins1[3] = {M_0, M_1, M_2};
const int MuxPins2[3] = {M3_0, M3_1, M3_2};
int Rij1[(ROWS*COLS)/2]; //resistance magnitude array
int Rij2[(ROWS*COLS)/2]; //resistance magnitude array
int state = 0;
String b="Start";
String bb = "stop";

void setup() {
// put your setup code here, to run once:
pinMode(M_0, OUTPUT);
pinMode(M_1, OUTPUT);
pinMode(M_2, OUTPUT);
pinMode(M1_E, OUTPUT);
pinMode(M2_E, OUTPUT);
pinMode(M3_0, OUTPUT);
pinMode(M3_1, OUTPUT);
pinMode(M3_2, OUTPUT);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
pinMode(A6, INPUT);
digitalWrite(M1_E,HIGH);  
digitalWrite(M2_E,HIGH); 
selectMuxPin2(0, MuxPins2); 
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  // wait for the START button
  if(!state){
  while (Serial.available() == 0){
  };
  while(Serial.available()){ 
    while (!(b==Serial.readString())){
    }
  }
  state = 1;
  }

//Do the read
selectMuxPin2(0, MuxPins2); 
for (unsigned int row=0; row<ROWS; row++){ // row
    selectMuxPin1(row, MuxPins1);
    for (unsigned int col=0; col<(COLS/2); col++){
      //delay(1);
      Rij1[(row*(COLS/2))+col] = analogRead(col); 
    }
  }
selectMuxPin2(1, MuxPins2);   
for (unsigned int row=0; row<ROWS; row++){ // row
    selectMuxPin1(row, MuxPins1);
    for (unsigned int col=0; col<(COLS/2); col++){
      //delay(1);
      Rij2[(row*(COLS/2))+col] = analogRead(col);
    }
  }
  
  //Send Values
  for (unsigned int row=0; row<ROWS; row++){ // row
    for (unsigned int col=0; col<(COLS/2); col++){
      Serial.println(Rij1[(row*(COLS/2))+col]);
    }
    for (unsigned int col=0; col<(COLS/2); col++){
      Serial.println(Rij2[(row*(COLS/2))+col]);
    }
  }

  //delay(1);

  //check for stop command
    if(state&&Serial.available()){
      if(bb==Serial.readString()){
        state=0;
      }    
    }
}

void selectMuxPin1(unsigned int pin, const int selectPins[]){
  pin = pin;
  if(pin<7){
    digitalWrite(M1_E,LOW);  
    digitalWrite(M2_E,HIGH);  
  } else{
    pin = pin+1;
    digitalWrite(M1_E,HIGH);  
    digitalWrite(M2_E,LOW); 
  }
  for (int i=0; i<3; i++)
  {
    if (bitRead(pin,i)==1)
      digitalWrite(selectPins[i], HIGH);
    else
      digitalWrite(selectPins[i], LOW);
  }

}

void selectMuxPin2(unsigned int pin, const int selectPins[]){
  pin = pin;
  for (int i=0; i<3; i++)
  {
    if (bitRead(pin,i)==1)
      digitalWrite(selectPins[i], HIGH);
    else
      digitalWrite(selectPins[i], LOW);
  }

}

I would really appreciate it if anyone can see where I might be going wrong

I would guess it's getting stuck in this while loop:

while (!(b==Serial.readString())){
    }

Try this instead:

while (true) {
  String x =Serial.readString();
  Serial.print("Received <");
  Serial.print(x);
  Serial.println(">");
  if (b == x) break;
}

You are right! The code worked cheers Paul

I physically sniffed the data on the pins

What did it smell like?

 myPort.write(str2);
       /*myPort.write('S');
              myPort.write('t');
                     myPort.write('a');
                            myPort.write('r');
                                   myPort.write('t');*/

What's
with
the
bizarre
indenting?

What DOES processing see that the Arduino received? Why do you NOT print what is in b, even if you think it is wrong?

It smelt right!

I'm now doing that with the help of PaulRB's code, and what I've discovered is that even though processing sends out 'Start' and the data is transmitted on the pins, the Arduino wasn't recieving it because of some handshake or Arduino setup time or something on port connection.

Adding a delay before sending 'Start' in the setup function has fully solved the problem.

Cheers

the Arduino wasn't recieving it because of some handshake or Arduino setup time or something on port connection.

With most Arduinos, when the code on the PC opens the serial port, the Arduino is reset. It does not start immediately. It takes a few milliseconds to get booted up. Understanding that, and expecting it, and handling it (as the delay() does, is necessary. What would be better, though, is to not use delay() on the Processing end. Since Processing woke the Arduino up, it should wait only long enough for the Arduino to say "Hey, I'm awake now, and have had a cup of coffee. Let's go". (Or something like that...)

Cheers Paul, I added a startup byte to be sent, should take care of all situations!

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R