You have made the classic mistake with Serial.available:
if (Serial.available()>0)
{
for ( int i=0; i<=12; i++)
You have assumed that because there is something delivered by the serial port, that there are 12 characters there. That is not the case, you need to check every time before you try to read a character.
Another simple way of doing somewhat the same thing.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
String readString;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(2);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if (readString == "on") {
digitalWrite(13, HIGH);
Serial.println("Led On");
}
if (readString == "off") {
digitalWrite(13, LOW);
Serial.println("Led Off");
}
readString="";
}
}