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]);
}
}