Hi Spankyty,
Try something like this:
#define MAX_CHANNELS 8
#define TIMEOUT 5000UL
int channels[MAX_CHANNELS] = {5, 6, 7, 8, 9, 10, 11, 12};
int incomingByte[MAX_CHANNELS];
void setup()
{
int i;
Serial.begin(9600); // set up Serial at 9600 bps
for ( i = 0; i < MAX_CHANNELS; i ++ ) pinMode(channels[i], OUTPUT);
}
void loop()
{
int i;
static int byteSum = 0;
static unsigned long lastReadTime = 0;
if (Serial.available() >= MAX_CHANNELS)
{
byteSum = 0;
for (i = 0; i < MAX_CHANNELS; i ++) {
incomingByte[i] = Serial.read();
byteSum += incomingByte[i];
}
lastReadTime = millis();
}
if ((millis() - lastReadTime >= TIMEOUT) && (byteSum == 0)) {
for (i = 0; i < MAX_CHANNELS; i ++ ) {
analogWrite(channels[i], 255);
}
}
else {
for (i = 0; i < MAX_CHANNELS; i ++ ) {
analogWrite(channels[i], incomingByte[i]);
}
}
}
I think it will do what you want. The TIMEOUT value is set to 5 seconds, but can be changed to whatever you like
Also - for "code safety" you should get into the habit of always using braces { } in your if and for-loop statements, even if you only have one statement.
Pat.