LED / TTL flicker problem, script stops after a few seconds

Hi,

I am a researcher who is trying to send both TTL pulses to another hardware while at the same time turning on an LED. What I want is this: no LED/TTL for 2000ms, LED/TTL on for 5000ms, then a loop of LED/TTL off for 500ms followed up LED/TTL on for 1000ms, and this loop continues indefinitely.

I get everything working the way I want for a few seconds, then the LED just turns on for a very long time and then stops and never turns on again... I am quite new to Arduino programming, and I have been trying to solve this for two days now. I really need to get this working for my research, so I hope you can help me.

Best,
Nils

void loop()
{

  /* DIGITALREAD - as fast as possible, check for changes and output them to the
   * FTDI buffer using Serial.print()  */
  checkDigitalInputs();

  /* STREAMREAD - processing incoming messagse as soon as possible, while still
   * checking digital inputs.  */
  while (Firmata.available())
    Firmata.processInput();

if (timer_state == -1)  { 
timer_new=millis();
if (timer_new>(timer_old+5000)) {
 digitalWrite(LED, LOW);       // turn the LED on (HIGH is the voltage level)
 digitalWrite(TTL, LOW);
 timer_state = -2;
 timer_old=millis();
 
}
}

else if (timer_state == -2)  { 
timer_new=millis();
if (timer_new>(timer_old+5000)) {
 digitalWrite(LED, HIGH);       // turn the LED on (HIGH is the voltage level)
 digitalWrite(TTL, HIGH);
 timer_state = -3;
  timer_old=millis();
}
}

else if (timer_state == -3)  { 
timer_new=millis();
if (timer_new>(timer_old+5000)) {
 digitalWrite(LED, LOW);       // turn the LED on (HIGH is the voltage level)
 digitalWrite(TTL, LOW);
 timer_state = 0;
 timer_old=millis();
 

}
}

else if (timer_state == 0)  { 
timer_new=millis();
if (timer_new>(timer_old+200)) {
 digitalWrite(LED, HIGH);       // turn the LED on (HIGH is the voltage level)
 digitalWrite(TTL, HIGH);
 timer_state = 1;
 timer_old=millis();
 randomtime = random(300, 800);
}
}
 else if (timer_state == 1)  {
timer_new=millis();
if (timer_new>(timer_old+randomtime)) {
 digitalWrite(LED, LOW);       // turn the LED on (HIGH is the voltage level)
 digitalWrite(TTL, LOW);
 timer_state = 0;
 timer_old=millis();
}
 }

Please read the rules and post all the code. There are so many things we need to see that are not in the loop function.
How are your buttons wired and do you have a pull-up resistor?

What is this code supposed to do? Why is it calling fermata functions?

i am just guessing, since a lot of info is missing, but i recon your variable declarations (sizes) are incorrect, just show us the whole sketch.