 char inputBuffer[SERIAL_BUFFER_LENGTH];
 float gradPerc, gradPercPerTick, gradDelayPerTick;
 int gradcol1[3], gradcol2[3];
-boolean gradientLoop, recentActivity = false;
+boolean gradientLoop = false, recentActivity = false;
 
 int red,green,blue;
 int* rgb[4] = {&red, &green, &blue, 0};
 
 // -------------- THREADS ---------------
 #include <TimedAction.h>
-TimedAction   serialThread = TimedAction(1000, &processSerial);
+TimedAction   serialThread = TimedAction(2000, 70, &processSerial);
 TimedAction gradientThread = TimedAction(0,  &gradientStep); // initially disabled in setup()
-TimedAction  timeoutThread = TimedAction(TIMEOUT*1000,  &timeoutAction);
+TimedAction  timeoutThread = TimedAction(TIMEOUT*1000, TIMEOUT*1000,  &timeoutAction);
 
 void setup() {
   if (SINGLE_LED) {

    pinMode(GND,OUTPUT);
  }
  gradientThread.disable();
  Serial.begin(BAUDRATE);
  // power-up notification
  quickCycle(25);
}

void loop() {  

  } 
}

void gradientPulseSetup (const int col1[3],const int col2[3], float timev) { // timev is now in seconds 
  gradDelayPerTick = (float) timev*1000/numdivs;
  gradPercPerTick = (float) 1/numdivs;
  gradPerc = 0;
  if (debugMode) {
    Serial.println("---gradientPulse---");
    Serial.println("col1,col2:");
    for (int i=0; i<3;i++) {
      Serial.print(col1[i]);
      Serial.print(' '); 
    }
    Serial.println();
    for (int i=0; i<3;i++) {
      Serial.print(col2[i]);
      Serial.print(' '); 
    }
    Serial.print("timev = ");
    Serial.println(timev);
    Serial.print("numdivs = ");
    Serial.println(numdivs);
    Serial.print("gradDelayPerTick = ");
    Serial.println(gradDelayPerTick);
    Serial.print("gradPercPerTick = ");
    Serial.println(gradPercPerTick);
    Serial.println();
  }

  for (int x=0; x<3; x++) {
    gradcol1[x]=col1[x];
    gradcol2[x]=col2[x];
  }
  
  gradientThread.setInterval(gradDelayPerTick);
  gradientThread.reset();
  gradientThread.enable();
  if (debugMode) {
    Serial.println("Gradient setup done, gradient thread enabled.");
  }
}



void gradientStep() {
  if (debugMode) { Serial.println("Gradient processing."); }
  if (gradPerc <= 1.0) {
    Serial.print(gradPerc);
    Serial.println("/1");
    gradientValue(gradcol1, gradcol2, gradPerc, rgb);
    setOutput(rgb);
    gradPerc+=gradPercPerTick;
  }
  else {
    if (gradientLoop) {
      gradPerc = 0;
      if (debugMode) { Serial.println("Looping gradient."); }
    } else {
      if (debugMode) { Serial.println("Halting gradient."); }
      gradientThread.disable();
    }
  }
}

void gradientValue(int col0[3], int col1[3], float percentage, int *output[3]) {
  *output[0] = col1[0]*percentage + col0[0]*(1-percentage);
  *output[1] = col1[1]*percentage + col0[1]*(1-percentage);
  *output[2] = col1[2]*percentage + col0[2]*(1-percentage);
}


void decodeHex( char hexColor[], int * output[] ) { //in: #FFFFFF\0
  unsigned long number = strtol( &hexColor[1], NULL, 16);
