I'm trying to write the most efficient code possible for my 16 channel generic serial however I am getting plugin error messages after the sequence has been running for over 80 seconds. Can someone help please.





// Global Constants
#include <avr/wdt.h> // Watchdog timer library
#define Channel_Counter 16 // How many Channels will vixen be sending.
#define Com_Speed 57600 // Com port speed
#define Timeout 2000 // Timeout waiting for serial input before switching off the relays

// Assign pins to Channels
#define Ch01 4 //relay 1
#define Ch02 5 //relay 2
#define Ch03 6 //relay 3
#define Ch04 7 //relay 4
#define Ch05 8 //relay 5
#define Ch06 9 //relay 6
#define Ch07 10 //relay 7
#define Ch08 11 //relay 8
#define Ch09 12 //relay 9
#define Ch10 13 //relay 10
#define Ch11 A0 //relay 11
#define Ch12 A1 //relay 12
#define Ch13 A2 //relay 13
#define Ch14 A3 //relay 14
#define Ch15 A4 //relay 15
#define Ch16 A5 //relay 16


// Global variables
int Channels[] = {Ch01,Ch02,Ch03,Ch04,Ch05,Ch06,Ch07,Ch08,Ch09,Ch10 ,Ch11,Ch12,Ch13,Ch14,Ch15,Ch16};
int incomingByte[Channel_Counter];
int i = 0; // loop counter
int waitTime = 500; // time to initalise relays during testSequence
volatile unsigned long timer_a = 0; // new line


// Setup the pins as outputs
void setup()
{
wdt_enable(WDTO_8S); // watchdog timer resets itself after 1 second
Serial.begin(Com_Speed); // set up serial speed
for (i=0; i < Channel_Counter; i++)
{
pinMode(Channels[i], OUTPUT); // initalize PWM and Analog In channels as outputs
digitalWrite(Channels[i], HIGH);
}
testSequence();

}


void loop()
{
if (Serial.available() >= (Channel_Counter+2))
{
wdt_reset(); // reset watchdog
timer_a = millis (); // new line
int uno = Serial.read();
if (uno == 126)
{
int dos = Serial.read();
if (dos == 33)
{
for (i=0; i < Channel_Counter; i++) // read each byte
{
incomingByte[i] = Serial.read();
}
for (i=0; i < Channel_Counter; i++)
{
int value = incomingByte[i];
if (value < 255)
{
digitalWrite(Channels[i], HIGH);
}
else
{
digitalWrite(Channels[i], LOW);
}
}
}
}
}

else // switch off relays if no serial input
{
wdt_reset(); // reset watchdog
unsigned long diff = millis() - timer_a;
if (diff >= Timeout)
{
timer_a = millis ();
for (i=0; i < Channel_Counter; i++)
{
digitalWrite(Channels[i], HIGH);
} // end for statement
} // end if statement
} // end else statement
} // end void loop

void testSequence()
{
for (i=0; i < Channel_Counter; i++)
{
wdt_reset(); // reset watchdog
digitalWrite(Channels[i], HIGH);
delay (waitTime);
digitalWrite(Channels[i], LOW);
}
}