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
}