sending strings from processing

Hi. I’m having a lot of trouble sending a string from Processing to an Arduino. I am simply trying to send a string from processing (“light”) and have the arduino light up an LED. The arduino code works fine if I type in “light” as a command in the arduino environment. My actual script is longer than the one I have posted and I need to be able to send and manipulate strings for my application. I’ve looked at the few posts about troubleshooting this issue but I have been unable to make my code work properly
thanks

this is my processing code

import processing.serial.*;
import cc.arduino.*;
Serial port;    //Create object from Serial class

void setup() {
  size(200, 200);
  noStroke();
  frameRate(10);
  //Open the port that the board is connected to and use the same speed (9600bps)
  port = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
 port.write("light");
 delay (1000);
}

and this is my arduino code

#include <WString.h>                // include the String library
#define maxLength 5
String inString = String(maxLength);       // allocate a new String


int ledPinTop[] = {
  3, 5}; //3-red,5-blue,6-green
int ledPinBot[] = {
  9, 10}; //9-red,10-blue,11-green
const byte RED[] = {
  0, 255}; 
const byte BLUE[] = {
  255, 0}; 
const byte OFF[] = {
  255, 255}; 
  
    
void setup() {
  // open the serial port:
  Serial.begin(9600);
   
   for(int i = 0; i < 2; i++){
    pinMode(ledPinTop[i], OUTPUT);   
    pinMode(ledPinBot[i], OUTPUT);  
  }
  setColor(ledPinTop, OFF);       //Turn off led 1
  setColor(ledPinBot, OFF);       //Turn off led 2
  DDRC = 0xff;
}

void loop () {
  // See if there's incoming serial data:
  if(Serial.available() > 0) {
    // read the incoming data as a char:
    char inChar = Serial.read();
    if (inString.length() < maxLength) {
      inString.append(inChar);
    }    
  }
  if (inString.equals("light")){
     setColor(ledPinBot, BLUE); 
  }
}


void setColor(int* led, const byte* color){
  byte tempByte[] = {
    color[0], color[1]                                        };
  for(int i = 0; i < 2; i++){
    analogWrite(led[i], 255 - tempByte[i]);
  }
}

Hi,

if you receive a wrong character, you will never get a right result, because the string is not cleared after a string is received. I think it is best to enclose your strings with special characters for start and end, for example:

$light#

then you can receive characters and throw them away as long as you don't receive the cgaracter $. If you receive the character $, clear the string and append all next coming characters until you receive the character #. Then analyze the string ans start from the beginning to receive the next command.

good luck Michael

Thanks for the help. Adding special characters to the script has helped me troubleshoot the issue.

I also realized that I was running into to problems because I was sending too many messages through the serial cable too quickly to the series of arduinos that I have connected by rs485. I now have my code set up so that a message is only sent if it registers change (before I had it set up so that it sent a message every loop whether or not there the new message was different than the previous message).