code only works randomly

I’m making a voice activated remote to control my tv. I have the remote hooked up to my computer via USB and I have a c# program that I have written that listens for my voice, and sends an array that holds the remote code that corresponds to the voice command to the arduino. I have done this because when I had the codes stored on the arduino there was not enough memory. So now that I have the arduino code recieving the array sometimes it will work and sometimes it won’t. For example, sometimes when I run the c# program it will perform only 1 command and then it will not perform anymore commands until I restart both the arduino and the code. Sometimes it will perform multiple commands several times and then I have to restart both the arduino and the code. I have included the arduino sketch. I can upload the c# code if needed but it is very long. Any thoughts on this issue are greatly appreciated.

#include <IRremote.h>

IRsend irsend;

unsigned int code[35];
int c = 0;
String input;
char temp[5];
int holder = 0;

void setup()
{
  Serial.begin(9600);

}

void loop() {
 
  input = "";
  c=0;
   while (Serial.available() == 0);
   
   while (Serial.available() > 0)
   {
   c = Serial.read();
   input += char(c);
   delay(2);
   }
   
   input.toCharArray(temp, 5);
   c = atoi(temp);
   code[holder] = c;
   holder++;
   
  
    
    if (holder >=35)
    {
      irsend.sendRaw(code, 35, 36);
      delay(100);
      for (int j = 0; j < 35; j++)
      {
       eight[j] = 0; 
      }
      holder=0;
    }
 
  
  if (c == '0') {
    for (int i = 0; i < 3; i++) {
      irsend.sendSony(0xa90, 12); // Sony TV power code
      delay(50);
    }
  }
  
  if (c == '2') {
    for (int i = 0; i < 3; i++) {
      irsend.sendSony(0xa90, 12); // Sony TV power code
      delay(50);
    }
  }
  
  if (c == '3'){
    for (int i=0; i<3; i++){
      irsend.sendSony(0x490, 12); // Sony TV volume up
      delay(50);
    }
  }
  if (c == '4'){
    for (int i=0; i<3; i++){
      irsend.sendSony(0xC90, 12); // Sony TV volume down
      delay(50);
    }
  }
  if (c == '5'){
    for (int i=0; i<3; i++){
      irsend.sendSony(0x290, 12); // Sony TV mute code
      delay(50);
    }
  }


}

I don't see anything in your code that helps the Arduino tell when a string of characters is starting and stopping. Are you using any ASCII code to indicate the C# program is done sending its string of characters? While the serial buffer can hold 128 characters, they may not all arrive immediately.

I'm only sending the code in a string format and then having a 10ms delay before it sends the next element in the array. I'm not quite sure how to implement sending an ASCII character like a start/stop code.

I'm not quite sure how to implement sending an ASCII character like a start/stop code.

Use hexadecimal values , can be found in almose every ASCII table.?

so I’ve added the code to take a start bit and a stop bit, but it is still working randomly, 3 and 7 are the start bit and stop bit respectively. The code works more often that it did before, but not 100%. I’m still have to restart the arduino when it stops working to get it to work again. Did I change the code improperly? Is it something else that is making it work randomly? Any thoughts are appreciated.

#include <IRremote.h>

IRsend irsend;

unsigned int code[35];
int c = 0;
String input;
char temp[5];
int holder = 0;
int x = 0;
boolean started = false;
String tempString;

void setup()
{
  Serial.begin(9600);

}

void loop() {
  tempString = "";
  input = "";
  c=0;
   while (Serial.available() == 0);
 
   while (Serial.available() > 0)
   {  
   c = Serial.read();
   if (c == 51)
  {
     started = true;
  }
  if (c == 55)
     started = false;
   if (started)
   {
     input += char(c);
     
     delay(2);
    }
   }
  
   
   tempString = input.replace('3', ' ');
   input = tempString.trim();
   input.toCharArray(temp, 5);
   
   c = atoi(temp);
   code[holder] = c;
   holder++;
   
    
    if (holder >=35)
    {
      irsend.sendRaw(code, 35, 36);
      delay(100);
      for (int j = 0; j < 35; j++)
      {
       code[j] = 0; 
      }
      holder=0;
    }
}

I really don't understand this stuff:

  while (Serial.available() > 0)
  {  
    c = Serial.read();
    if (c == 51)
    {
      started = true;
    }
    if (c == 55)
      started = false;
    if (started)
    {
      input += char(c);

      delay(2);
    }
  }


  tempString = input.replace('3', ' ');
  input = tempString.trim();
  input.toCharArray(temp, 5);

When you say:

 if (c == 51)

do you mean:

 if (c == '3')

So hopefully you won't be sending a "3" down from the PC. Or a "7". There is a big difference between "binary 3" (that is 0x03) and "ASCII 3" (that is 0x33, or 51 in decimal).

Why are you delaying 2 milliseconds before pulling out each character?

You need to start thinking "a byte at a time". You won't necessarily get all the bytes at once. So you need to have something like a buffer. When you get your start character (I wouldn't recommend "3" myself) then you clear the buffer, ready to put stuff into it. Then when you get anything except the stop character (and again I wouldn't be using "7") you then work out what to do with the characters in the buffer. If you get anything other than the start or stop character you append to the buffer. You might only get one character every time through "loop".

Imagine someone is on the phone to you slowly reading out a number (like a credit card number). They might say "5353" and pause for a moment. But you don't think "aha, 5353 is the credit card number" - you think "I'll wait until they finish, even if that is a minute later". You have to think like that.

Nick offers some good ideas. If you search the forum for "started && ended" you'll find some code I posted to show how to implement Nick's ideas.

Your loop() code is allocating memory via toCharArray() - so you run out of memory. Only allocate memory at setup-time. Basically don't use the String class!