Simultaneous Logic

I was wondering if it was possible to write multiple digital pins at once based on conditions. So far I've gotten it to turn on one pin after the other but not at the same time. I'm trying to min. hard coding. It will be read as a string (on/off,time,on/off2,time2,on/off3,time3)

I'd like it to do the writes at the same time. Any help would be greatly appreciated.

void setup() { 
Serial.begin(9600); // Initialize serial port 
} 
void loop() { 
  String content = ""; 
char character; while(Serial.available()>0) { 
while (Serial.available() > 0)
  character = Serial.read(); 
content.concat(character); 
delay (10); 
} 
if (content != "") { 
Serial.println(content); } 

int commaIndex = content.indexOf(',');
//  Search for the next comma just after the first
int secondcommaIndex = content.indexOf(',', commaIndex+1);
int thirdcommaIndex = content.indexOf(',', commaIndex+1);
int fourthcommaIndex = content.indexOf(',', commaIndex+1);

String firstValue = content.substring(0, commaIndex);
String secondValue = content.substring(commaIndex+1, secondcommaIndex);
String thirdValue = content.substring(secondcommaIndex+1, thirdcommaIndex);
String fourthValue = content.substring(thirdcommaIndex+1, fourthcommaIndex);
String fifthValue = content.substring(fourthcommaIndex); // To the end of the string

int r = firstValue.toInt(); //on/off 1
int g = secondValue.toInt(); // time 1
int b = thirdValue.toInt(); //on/off 2
int y = fourthValue.toInt(); // time 2
int z = fifthValue.toInt();
Serial.println(r);
Serial.println(g);
Serial.println(b);
Serial.println(z);


//output on pin 8
if (r == 1 ) { // test for command 1 then turn on LED
pinMode(8, OUTPUT);
digitalWrite(8, HIGH); // turn on LED
delay(g);
digitalWrite(8, LOW); // turn off LED
}

//output on pin 13
if (b == 1 ) { // test for command 2 then turn on LED
pinMode(13, OUTPUT);
digitalWrite(13, HIGH); // turn on LED
delay(y);
digitalWrite(13, LOW); // turn off LED
}

Serial.flush(); // clear serial port
}

Another possible way would be to just read the times but would a time value of 0 ensure that no power is provided?

Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software

finkej4:
I'd like it to do the writes at the same time.

It's fundamentally impossible to do things literally 'at the same time'. And, most likely, your actual timing requirements are not especially demanding. The question becomes: how close together (in time) do you need these actions to occur? Are you talking seconds, milliseconds, microseconds, nanoseconds?

With all those delays and serial prints in there, speed would not appear to be of the essence.

Try using the auto-format tool in the IDE, the indenting is hard to read.

The outcome is to control multiple valves to stay open for ~.5-5 seconds. It does not need to be very precise but they do need to open at roughly the same time and turn off at different times. A friend suggested setting a time then some how adding the variable time to millis() set time to indicate when it should turn off.

In that case you want to control these things concurrently and independently but there are no special timing requirements.

Use a non-blocking approach for each piece of code, as demonstrated in the blink without delay example sketch.This sounds like what your friend suggested too.