Code optimization: state machine for different duration events

Hi!
I have given a shot at making a state machine based on the Processing language (similar to Arduino).
The program sits in the background and waits for an input array of 4 buckets with different times.
Each solenoid is turned on if the value of that bucket is != 0. Once the solenoid is turned on for the allotted time, it is switched off, and that bucket is set to 0. This occurs for every bucket, until they are all reset to 0, and then the cycle resets (i.e., the program sits an waits for the next string of numbers).
I think I have the thing working, but I'd love to get a set of experienced eyes on the code!

Please find it here: import processing.net.*;import cc.arduino.*;import processing.serial.*;/ - Pastebin.com or below:

import processing.net.*;
import cc.arduino.*;
import processing.serial.*;
 
// Arduino arduino;
Server myServer;
 
int solenoid_0 = 2;      // the pin numbers for the solenoids
int solenoid_1 = 3;
int solenoid_2 = 4;
int solenoid_3 = 5;
 
byte solenoid_0state = 0;
byte solenoid_1state = 0;
byte solenoid_2state = 0;
byte solenoid_3state = 0;
 
int[] solenoidTime;
 
long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
long startMillis = 0;
 
void setup() {
 
  myServer = new Server(this, 5204);
 
 
  // Prints out the available serial ports.
  /*
 
  println(Arduino.list());
 
  // Modify this line, by changing the "0" to the index of the serial
  // port corresponding to your Arduino board (as it appears in the list
  // printed by the line above).
  arduino = new Arduino(this, Arduino.list()[0], 57600);
 
  // Configure digital pins 2 to 5 to control relays that control the solenoids.
  arduino.pinMode(solenoid_0, OUTPUT);
  arduino.pinMode(solenoid_1, OUTPUT);
  arduino.pinMode(solenoid_2, OUTPUT);
  arduino.pinMode(solenoid_3, OUTPUT);  
   
  // Set servo motors to drive to locked position.
  arduino.digitalWrite(solenoid_0, solenoid_0state);
  arduino.digitalWrite(solenoid_1, solenoid_1state);
  arduino.digitalWrite(solenoid_2, solenoid_2state);
  arduino.digitalWrite(solenoid_3, solenoid_3state);
 
  */
}
 
void draw() {
  Client thisClient = myServer.available();
  if (thisClient !=null){
    String whatClientSaid = thisClient.readString();
    String trimWhatClientSaid = trim(whatClientSaid);
    println(trimWhatClientSaid);
    solenoidTime = int(split(trimWhatClientSaid, ' '));
   
   startMillis=millis();
   while (max(solenoidTime) != 0){
     currentMillis = millis();
     updateSolenoid_0();
     updateSolenoid_1();
     updateSolenoid_2();
     updateSolenoid_3();
     
     switchSolenoids();
   }
   println("Next Round");
   }
}
 
void updateSolenoid_0() {
  if (solenoidTime[0] != 0){
    if (solenoid_0state == 1) {
      if (currentMillis >= startMillis + solenoidTime[0]) {
         //arduino.digitalWrite(solenoid_0, 0);        
         solenoidTime[0]=0;
         println("Solenoid 0 OFF");
      }
    }
    else {
      //arduino.digitalWrite(solenoid_0, 1);
         println("Solenoid 0 ON");
    }
  }
}
 
void updateSolenoid_1() {
  if (solenoidTime[1] != 0){
    if (solenoid_1state == 1) {
      if (currentMillis >= startMillis + solenoidTime[1]) {
         //arduino.digitalWrite(solenoid_3, 0);        
         solenoidTime[1]=0;
         println("Solenoid 1 OFF");
      }
    }
    else {
      //arduino.digitalWrite(solenoid_1, 1);
         println("Solenoid 1 ON");
    }
  }
}
 
void updateSolenoid_2() {
  if (solenoidTime[2] != 0){
    if (solenoid_2state == 1) {
      if (currentMillis >= startMillis + solenoidTime[2]) {
         //arduino.digitalWrite(solenoid_3, 0);      
         solenoidTime[2]=0;
         println("Solenoid 2 OFF");
      }
    }
    else {
      //arduino.digitalWrite(solenoid_2, 1);
         println("Solenoid 2 ON");
    }
  }
}
 
void updateSolenoid_3() {
  if (solenoidTime[3] != 0){
    if (solenoid_3state == 1) {
      if (currentMillis >= startMillis + solenoidTime[3]) {
         //arduino.digitalWrite(solenoid_3, 0);        
         solenoidTime[3]=0;
         println("Solenoid 3 OFF");
      }
    }
    else {
        //arduino.digitalWrite(solenoid_3, 1);
         println("Solenoid 3 ON");
    }
  }
}

The main loop is the draw() loop, which calls each of the separate update processes of the solenoids.
Thanks for any input!

Processing is essentially Java, and Arduino is essentially C++, and these are very different languages.

Both languages support the concept of arrays, and you should learn how to use them. They will enable you to eliminate an awful lot of duplicate code from your application.

I would solve this problem by defining an array of pin numbers (one per solenoid) and an array of times (one per solenoid). The time would be set to the current time when the solenoid is turned on. Subsequently, the saved time for each solenoid can be subtracted from the current time to find how long the solenoid has been on and hence whether it is time to turn it off. Use FOR loops to iterate through the arrays and apply the same logic to each solenoid. It would probably take about a dozen lines of code to manage the solenoid timing.

Thank you, kind stranger, for your input. I have taken your suggestions and incorporated them into my code. If you have a second, any constructive criticism would be welcome.

//KP Processing Code 7/1/14

/*

 This code is to be launched before the FlashDevelop program is run.
 
 It sits in the background and listens to any input sent to it on port 5204.
 Once it receives an input, it opens each valve sequentially.
 
 After completion, it continues to listen.
 
 */

import processing.net.*;
import cc.arduino.*;
import processing.serial.*;

// Arduino arduino;
Server myServer;

int[][] solenoidData = new int[4][4];



int currentMillis = 0;    // stores the value of millis() in each iteration of loop()
int startMillis = 0;

void setup() {

  myServer = new Server(this, 5204); 

  solenoidData[0][0] = 2;      // the pin numbers for the LEDs
  solenoidData[0][1] = 3;
  solenoidData[0][2] = 4;
  solenoidData[0][3] = 5;

  solenoidData[1][0] = 0;
  solenoidData[1][1] = 0;
  solenoidData[1][2] = 0;
  solenoidData[1][3] = 0;

  // Prints out the available serial ports.
  /* 
   
   println(Arduino.list());
   
   // Modify this line, by changing the "0" to the index of the serial
   // port corresponding to your Arduino board (as it appears in the list
   // printed by the line above).
   arduino = new Arduino(this, Arduino.list()[0], 57600);
   
   // Configure digital pins 2 to 5 to control relays that control the solenoids.
   arduino.pinMode(solenoidData[0][0], OUTPUT);
   arduino.pinMode(solenoidData[0][1], OUTPUT);
   arduino.pinMode(solenoidData[0][2], OUTPUT);
   arduino.pinMode(solenoidData[0][3], OUTPUT);  
   
   // Set servo motors to drive to locked position.
   arduino.digitalWrite(solenoidData[0][0], solenoidData[1][0]);
   arduino.digitalWrite(solenoidData[0][1], solenoidData[1][1]);
   arduino.digitalWrite(solenoidData[0][2], solenoidData[1][2]);
   arduino.digitalWrite(solenoidData[0][3], solenoidData[1][3]);
   
   */
}

void draw() {
  Client thisClient = myServer.available();
  if (thisClient !=null) {

    String whatClientSaid = thisClient.readString();
    String trimWhatClientSaid = trim(whatClientSaid);
    println(trimWhatClientSaid);

    solenoidData[2] = int(split(trimWhatClientSaid, ' ')); 
    startMillis=millis();

    for (int i = 0; i < 4; i+=1) {
      solenoidData[3][i] = solenoidData[2][i] + startMillis;
    }

    while (max (solenoidData[2]) !=  0) {

      currentMillis = millis();

      for (int i = 0; i<4; i+=1) {
        if (solenoidData[2][i] != 0) {

          if (solenoidData[1][i] == 1) {

            if (currentMillis >= solenoidData[3][i]) {
              //arduino.digitalWrite(solenoidData[1][i], 0);        
              solenoidData[1][i]=0;
              solenoidData[2][i]=0;
              println("Solenoid ", i, " OFF");
            }
          
          } 
          else {
            //arduino.digitalWrite(solenoidData[1][i], 1); 
            solenoidData[1][i]=1;
            println("Solenoid ", i, " ON");
          }
        }
      }
    }
    println("Next Round");
  }
}

palanski:
and incorporated them into my code.

Does your revised code do what you want?

If not describe what it is doing wrong.

...R

Instead of this:

 // Configure digital pins 2 to 5 to control relays that control the solenoids.
   arduino.pinMode(solenoidData[0][0], OUTPUT);
   arduino.pinMode(solenoidData[0][1], OUTPUT);
   arduino.pinMode(solenoidData[0][2], OUTPUT);
   arduino.pinMode(solenoidData[0][3], OUTPUT);

try this:

for(int x=0; x<=3; x++){
 arduino.pinMode(solenoidData[0][x], OUTPUT);
}