Sending an array through serial

Hello,

I am trying to develop a GUI in processing to toggle on/off sensor. For testing purposes im using it to turn a LED on/off.

Since i will have multiple led’s (sensors) I thought of creating an array for storing the states of the leds. A 1 represents on and 0 for off. I was able to create the array on processing but im not sure how i would read that array on arduino. Would be also open to other ways of sending the information over.

Processing:

import processing.serial.*;

import controlP5.*;

Serial myPort;
ControlP5 cp5;

int col = color(255);
int col2 = color(255);
  

String[] stateString = new String [2];
boolean toggleValue = false;

void setup() {
  size(400,400);
  smooth();
  myPort = new Serial(this, "COM5", 9600);
  cp5 = new ControlP5(this);
  

  // create a toggle and change the default look to a (on/off) switch look
  cp5.addToggle("LED")
     .setPosition(40,250)
     .setSize(50,20)
     .setValue(false)
     .setMode(ControlP5.SWITCH)
     ;
     
     cp5.addToggle("Photoresistor")
     .setPosition(40,200)
     .setSize(50,20)
     .setValue(false)
     .setMode(ControlP5.SWITCH)
     ;
}
  

void draw() {
  background(0);
 
  fill(col);
  ellipse(280,250,40,40);
  
  fill(col2);
  ellipse(280,200,40,40);
  printArray (stateString);
  String theStg = "<" + stateString[0]  +
                      " " + stateString[1] +
                      ">";
 myPort.write(theStg);
}




void Photoresistor(boolean theFlag) {
  if(theFlag==true) {
    col2 = color(#00FF46);
    stateString[0] = "1";
    println("ON");
    
  } else {
    col2 = color(#FF0011);
    stateString[0] = "0";
    println("OFF");
    
  }  
}

  void LED(boolean theFlag) {
  if(theFlag==true) {
    col = color(#00FF46);
    stateString[1] = "1";
  } else {
    col = color(#FF0011);
    stateString[1] = "0";
    println("OFF");
   
  }
}

Arduino:

int led = 5;
int led2 = 6;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);

}

void loop() {
if (Serial.available() > 0){
    // state = Serial.read();
  String state = Serial.readString();
}
if (state[0] == "1"){
digitalWrite(led,HIGH);
}
if (state[0] == "0"){
  digitalWrite(led,LOW);
}
if(state[1] == "1"){
  digitalWrite(led2,HIGH);
}
if (state[1] == "0"){
  digitalWrite(led,LOW);
}
}

String theStg = "<" +If that’s what you’re sending, this at the receiving endif (state[0] == "1"){doesn’t seem like a very likely occurrence, does it?

I think the 3rd example in Serial Input Basics will receive that data. There is also a parse example.

...R

Thanks Robin2,

I got it to work for the most part. The problem i’m having now, is that if i have it off on my GUI the LED will flicker on and then go off for a while and then flicker on again. I think it has to do with the delay in my code. This is my function:

void doOtherStuff() {
  if(receivedChars[0] == '1'){
    digitalWrite(led,HIGH);
  }
  if(receivedChars[0] == '0'){
    digitalWrite(led,LOW);
  }if(receivedChars[1] == '1'){
    digitalWrite(led2,HIGH);
  }if(receivedChars[1] == '0'){
    digitalWrite(led2,LOW);
  }
  delay(30); // <<===NEW - This just simulates time spent elsewhere in a big program
}

EDIT: Nevermind, i got it to work I included a delay in my processing GUI. Thanks again.